# Express: env key, and handle both failure channels
```ts
import express, { Request, Response } from 'express';
import { Resend } from 'resend';
const app = express();
const resend = new Resend(process.env.RESEND_API_KEY);
app.post('/send', async (req: Request, res: Response) => {
try {
const { data, error } = await resend.emails.send({
from: '[SENDER]',
to: ['[RECIPIENT]'],
subject: 'Hello World',
html: '<strong>It works!</strong>',
});
if (error) {
return res.status(400).json({ error: error.message });
}
return res.json({ id: data.id });
} catch (err) {
return res.status(502).json({ error: 'email provider unreachable' });
}
});
```
## Checklist
- The guide writes `new Resend("re_xxxxxxxxx")`. That literal is a placeholder.
In real code it must be `process.env.RESEND_API_KEY`, loaded from the
environment, never committed.
- Two failure channels: `error` for API rejections, thrown exceptions for
transport problems. Handle both or one of them becomes an unhandled rejection
that crashes the process.
- Do not echo the raw provider error object to the HTTP client. Map it to a
status and a short message.