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

```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: '&lt;strong&gt;It works!&lt;/strong&gt;',
    }),
  });

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