Skip to content

Commit 3464c7a

Browse files
authored
chore: update according to the docs (#57)
1 parent adaa3a5 commit 3464c7a

File tree

5 files changed

+93
-60
lines changed

5 files changed

+93
-60
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Resend with Cloudflare Workers
2+
3+
This example shows how to use Resend with [Cloudflare Workers](https://workers.cloudflare.com).
4+
5+
## Prerequisites
6+
7+
To get the most out of this guide, you’ll need to:
8+
9+
* [Create an API key](https://resend.com/api-keys)
10+
* [Verify your domain](https://resend.com/domains)
11+
12+
## Instructions
13+
14+
### 1. Install
15+
16+
Get the Resend Node.js SDK.
17+
18+
```bash
19+
npm install resend
20+
```
21+
22+
### 2. Create an email template
23+
24+
Start by creating your email template on `src/emails/email-template.tsx`:
25+
26+
```tsx
27+
import * as React from 'react';
28+
29+
interface EmailTemplateProps {
30+
firstName: string;
31+
}
32+
33+
export const EmailTemplate: React.FC<Readonly<EmailTemplateProps>> = ({
34+
firstName,
35+
}) => (
36+
<div>
37+
<h1>Welcome, {firstName}!</h1>
38+
</div>
39+
);
40+
41+
export default EmailTemplate;
42+
```
43+
44+
### 3. Send the email using React and the SDK
45+
46+
Change the file extension of the worker's main file to `tsx` and modify your configurations.
47+
48+
After that, you can send your email using the `react` parameter:
49+
50+
```tsx
51+
import * as React from 'react';
52+
import { Resend } from 'resend';
53+
import { EmailTemplate } from './emails/email-template';
54+
55+
export default {
56+
async fetch(request, env, context): Promise<Response> {
57+
const resend = new Resend('re_123456789');
58+
59+
const data = await resend.emails.send({
60+
from: 'Acme <onboarding@resend.dev>',
61+
to: ['delivered@resend.dev'],
62+
subject: 'hello world',
63+
react: <EmailTemplate firstName="John" />,
64+
});
65+
66+
return Response.json(data);
67+
},
68+
} satisfies ExportedHandler<Env, ExecutionContext>;
69+
```
70+
71+
### 4. Deploy and send email
72+
73+
Run `wrangler deploy` and wait for it to finish. Once it's done, it will
74+
give you a URL to try out, like `https://my-worker.your_name.workers.dev`,
75+
that you can open and verify that your email has been sent.
76+
77+
## License
78+
79+
MIT License

readme.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/emails/email-template.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type * as React from 'react';
1+
import * as React from 'react';
22

33
interface EmailTemplateProps {
44
firstName: string;
55
}
66

7-
export const EmailTemplate: React.FC<Readonly<EmailTemplateProps>> = ({
8-
firstName,
9-
}) => (
10-
<div>
11-
<h1>Welcome, {firstName}!</h1>
12-
</div>
13-
);
7+
export function EmailTemplate({ firstName }: EmailTemplateProps) {
8+
return (
9+
<div>
10+
<h1>Welcome, {firstName}!</h1>
11+
</div>
12+
);
13+
}
1414

1515
export default EmailTemplate;

src/index.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
import * as React from 'react';
12
import { Resend } from 'resend';
23
import { EmailTemplate } from './emails/email-template';
34

45
export default {
5-
async fetch(
6-
request: Request,
7-
env: Env,
8-
ctx: ExecutionContext,
9-
): Promise<Response> {
10-
const resend = new Resend(env.RESEND_API_KEY);
6+
async fetch(request, env, context): Promise<Response> {
7+
const resend = new Resend('re_123456789' /* env.RESEND_API_KEY */);
118

129
const data = await resend.emails.send({
1310
from: 'Acme <onboarding@resend.dev>',
@@ -16,10 +13,6 @@ export default {
1613
react: <EmailTemplate firstName="John" />,
1714
});
1815

19-
return new Response(JSON.stringify(data), {
20-
headers: {
21-
'Content-Type': 'application/json',
22-
},
23-
});
16+
return Response.json(data);
2417
},
25-
};
18+
} satisfies ExportedHandler<Env, ExecutionContext>;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lib": [
1616
"es2021"
1717
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
18-
"jsx": "react-jsx" /* Specify what JSX code is generated. */,
18+
"jsx": "react" /* Specify what JSX code is generated. */,
1919
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
2020
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
2121
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */

0 commit comments

Comments
 (0)