Quickstarts
Send with Vercel Functions
Send email from a Vercel serverless function with the usend SDK.
You need a verified domain and an API key.
1. Install
npm install @usend/node2. Add the key as an environment variable
vercel env add USEND_API_KEYOr add it in the project's Settings → Environment Variables. Use a sending_access key, and a separate key for Preview and Production.
3. The function
import { Usend } from '@usend/node';
const usend = new Usend(process.env.USEND_API_KEY);
export async function POST(request: Request) {
const { email } = await request.json();
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 Response.json({ error }, { status: 502 });
return Response.json({ id: data.id });
}This is a Web-standard handler: it works in a plain Vercel project (api/ folder) and in a Next.js route handler. For a Next.js app see Send with Next.js.
4. Deploy and test
vercel deploy
curl -X POST https://your-project.vercel.app/api/send \
-H 'Content-Type: application/json' -d '{"email":"[email protected]"}'Tips
- Await the send inside the function. Serverless functions can be frozen as soon as they respond, so a fire-and-forget request may never leave.
- Use an idempotency key if your function can be retried, so a retry does not send twice.
- Receive delivery events with a webhook pointing at another function.