Send email with Express
A server-side call to the SendSend API from your Express app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Export SENDSEND_API_KEY in the environment that runs the server, or load it with a .env file.
- Add this to
server.js:
import express from 'express';
const app = express();
app.use(express.json());
app.post('/send', async (req, res) => {
const r = await fetch('https://api.sendsend.email/v1/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SENDSEND_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({ from: 'Acme <hello@mail.acme.com>', to: [req.body.to], subject: 'Hello from Express', text: 'It works!' }),
});
res.status(r.status).json(await r.json());
});
app.listen(3000);Good to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.