uSend Docs
Quickstarts

Send with AWS Lambda

Send email from an AWS Lambda function with the usend SDK.

You need a verified domain and an API key. This uses the Node.js 20 runtime or newer.

1. Package the function

Shell
mkdir usend-lambda && cd usend-lambda
npm init -y
npm install @usend/node
index.mjs
import { Usend } from '@usend/node';

const usend = new Usend(process.env.USEND_API_KEY);

export const handler = async (event) => {
  const { email } = JSON.parse(event.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>',
  });

  return {
    statusCode: error ? 502 : 200,
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(error ?? { id: data.id }),
  };
};

The client is created outside the handler, so warm invocations reuse it.

2. Deploy

Shell
zip -r function.zip index.mjs node_modules package.json
aws lambda create-function --function-name usend-send \
  --runtime nodejs20.x --handler index.handler \
  --role arn:aws:iam::123456789012:role/lambda-basic \
  --zip-file fileb://function.zip \
  --environment "Variables={USEND_API_KEY=us_123456789}"

For anything beyond a test, store the key in AWS Secrets Manager or SSM Parameter Store and read it at cold start, instead of a plain environment variable.

3. Invoke

Shell
aws lambda invoke --function-name usend-send \
  --cli-binary-format raw-in-base64-out \
  --payload '{"body":"{\"email\":\"[email protected]\"}"}' out.json && cat out.json

Tips

  • Await the call before returning: Lambda freezes the environment when the handler returns.
  • If the function is triggered by a queue (SQS, EventBridge), pass an idempotency key built from the message ID so retries never send twice.
  • The function needs outbound internet access to api.usend.et (a NAT gateway if it runs in a VPC).