Skip to content

Commit 37ab58a

Browse files
committed
feat: Add Resend with Express example
1 parent 60b05c5 commit 37ab58a

File tree

6 files changed

+559
-6
lines changed

6 files changed

+559
-6
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RESEND_API_KEY=""

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Resend with Example
1+
# Resend with Express
22

3-
This example shows how to use Resend with [Example](https://example.com).
3+
This example shows how to use Resend with [Express](https://expressjs.com).
44

55
## Prerequisites
66

@@ -11,18 +11,26 @@ To get the most out of this guide, you’ll need to:
1111

1212
## Instructions
1313

14-
1. Replace `re_123456789` on `example.ts` with your API key.
14+
1. Define environment variables in `.env` file.
1515

1616
2. Install dependencies:
1717

1818
```sh
19-
TBD
19+
npm install
20+
# or
21+
yarn
2022
```
2123

22-
3. Execute the following command:
24+
3. Run Express locally:
2325

2426
```sh
25-
TBD
27+
npm run dev
28+
```
29+
30+
4. Open URL in the browser:
31+
32+
```
33+
http://localhost:3000
2634
```
2735

2836
## License

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require('dotenv').config()
2+
3+
const { Resend } = require('resend');
4+
const resend = new Resend(process.env.RESEND_API_KEY);
5+
6+
const express = require('express');
7+
const app = express();
8+
9+
app.get('/', async (req, res) => {
10+
try {
11+
const data = await resend.sendEmail({
12+
from: 'onboarding@resend.dev',
13+
to: 'delivered@resend.dev',
14+
subject: "Hello world",
15+
html: "<strong>It works!</strong>",
16+
});
17+
18+
res.status(200).json(data);
19+
}
20+
catch(e) {
21+
res.status(400).json(e);
22+
}
23+
})
24+
25+
app.listen(3000, () => {
26+
if (!process.env.RESEND_API_KEY) {
27+
throw `Abort: You need to define RESEND_API_KEY in the .env file.`;
28+
}
29+
30+
console.log('Listening on http://localhost:3000')
31+
})

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "resend-express-example",
3+
"private": true,
4+
"scripts": {
5+
"dev": "node index.js"
6+
},
7+
"dependencies": {
8+
"dotenv": "^16.0.1",
9+
"express": "^4.18.1",
10+
"resend": "^0.8.0"
11+
}
12+
}

0 commit comments

Comments
 (0)