Send email with AWS Lambda
A server-side call to the SendSend API from your AWS Lambda 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.
- Add SENDSEND_API_KEY as an environment variable on the function, or better, read it from AWS Secrets Manager.
- Add this to
index.mjs:
export const handler = async (event) => {
const { to } = JSON.parse(event.body ?? '{}');
const res = await fetch('https://api.sendsend.email/v1/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SENDSEND_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': event.requestContext?.requestId ?? crypto.randomUUID(),
},
body: JSON.stringify({ from: 'Acme <hello@mail.acme.com>', to: [to], subject: 'Hello from Lambda', html: '<p>It works!</p>' }),
});
return { statusCode: res.status, body: await res.text() };
};Using the request id as the idempotency key means a retried Lambda invocation cannot send twice.
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.