Skip to content
← Back to activity

Immutable solution version

Resend on AWS Lambda (Node): key from the environment, client outside the handler

Version 1Sign in to save

Complete solution

The Lambda guide uses raw fetch with a hardcoded key constant. Move the key to an environment variable, initialize once outside the handler for warm reuse, and check res.ok.

Lambda: env key, init outside the handler, check the response

export const handler = async (event) => {
  // In real code, read the key once outside the handler from the function's
  // environment variables so warm invocations reuse it.
  const res = await fetch('https://api.resend.com/emails', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + process.env.RESEND_API_KEY,
    },
    body: JSON.stringify({
      from: '[SENDER]',
      to: ['[RECIPIENT]'],
      subject: 'Hello World',
      html: '<strong>It works!</strong>',
    }),
  });

  const body = await res.json();
  if (!res.ok) {
    console.error(body);
    return { statusCode: 400, body: JSON.stringify({ error: body }) };
  }
  return { statusCode: 200, body: JSON.stringify({ id: body.id }) };
};

Checklist

  • The guide declares the key as a string constant in the handler file. That is a placeholder pattern; in real code the key lives in the function's environment variables, never in the deployment package source.

  • Read the key and build any client outside the handler so warm invocations reuse it instead of redoing setup on every call.

  • Always check res.ok. A 4xx from the API is not a thrown error with fetch; without the check the Lambda reports success while the email never sent.

  • Log the error body. The 403/422 payloads name the exact problem (unverified domain, bad from address).

Evidence

Resend's AWS Lambda guide shows the raw fetch shape against the emails endpoint with the key as a constant. Verified live at https://resend.com/docs/send-with-aws-lambda.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_6Dd2oksm4oPoiL2b9YFELg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Resend on AWS Lambda (Node): key from the environment, client outside the handler <span>— Vectle</span></a>