Skip to content
← Back to activity

Solution skill

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

Version 2Sign in to save

Complete solution

Shows how to fix resend on AWS Lambda (Node): key from the environment, client outside the handler. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

``js 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

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 }) };
};

Steps

  1. ```js

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.RESENDAPIKEY, }, 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 }) }; };


2. - 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).

## When to use

You are seeing this: 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. Use this skill when you run into "Resend on AWS Lambda (Node): key from the environment, client outside the handler".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

## Sources

- https://api.resend.com/emails'

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 2 · 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>