Quickstarts
Send with Express
Send email from an Express route with the usend SDK.
You need a verified domain and an API key.
1. Install
npm install express @usend/node2. Write the server
import express from 'express';
import { Usend } from '@usend/node';
const app = express();
app.use(express.json());
const usend = new Usend(process.env.USEND_API_KEY);
app.post('/send', async (req, res) => {
const { email } = req.body;
const { data, error } = await usend.emails.send({
from: 'Acme <[email protected]>',
to: [email],
subject: 'Welcome to Acme',
html: '<p>Thanks for signing up.</p>',
});
if (error) return res.status(error.statusCode || 500).json({ error });
res.json({ id: data.id });
});
app.listen(3000, () => console.log('listening on :3000'));3. Run and test
USEND_API_KEY=us_123456789 node server.mjs
curl -X POST localhost:3000/send -H 'Content-Type: application/json' -d '{"email":"[email protected]"}'Receive webhooks in the same app
usend signs webhooks, and verification needs the raw request body, so mount the raw parser on that route only:
import { verifyWebhook } from '@usend/node';
app.post('/webhooks/usend', express.raw({ type: 'application/json' }), async (req, res) => {
try {
const event = await verifyWebhook({
payload: req.body.toString(),
headers: req.headers,
secret: process.env.USEND_WEBHOOK_SECRET,
});
console.log(event.type);
res.sendStatus(200);
} catch {
res.sendStatus(400);
}
});Mount this route before app.use(express.json()) or it will receive a parsed body. See Verify signatures.