Send email with Vercel Functions
A function on the default Node runtime, with the key as an environment variable.
You need a verified sending domain and an API key from Settings › API Keys. If neither exists yet, the quickstart covers both in a couple of minutes.
Install
npm i sending-sdkSend
import { Sending } from "sending-sdk";
const sending = new Sending({ apiKey: process.env.SENDING_API_KEY! });
export async function POST(req: Request) {
const { email, userId } = await req.json();
const res = await sending.emails.send({
from: "Acme <[email protected]>",
to: email,
subject: "Welcome",
html: "<p>You're in.</p>",
idempotencyKey: `welcome-${userId}`,
});
return Response.json(res, { status: 202 });
}A successful call answers 202 with the message id: accepted and queued, not yet delivered. Delivery, opens, bounces and complaints arrive later, on webhooks or in the dashboard.
What changes in Vercel Functions
The default runtime is Node with full APIs, so nothing here needs the edge runtime. Add the variable with vercel env add for each environment you deploy.
The thing that catches people
Environment variables are read at deploy time: changing one in the dashboard does not affect the running deployment until you redeploy. A key that looks correct and answers 401 is usually this.
Next
- All the fields: cc and bcc, reply-to, attachments, templates, scheduling.
- Webhooks: delivery, bounce and complaint events, signed with HMAC.
- Domains: SPF, DKIM and MAIL FROM, and why sending is refused until they are in place.
- MCP: the same operations as tools, when the one writing the code is an agent.