# Send with Next.js

> Send from a Route Handler so the key never reaches the browser.

## Prerequisites

- A Sendtern [API key](https://sendtern.com/docs/authentication) stored as `SENDTERN_API_KEY`
- A Next.js 13.4+ app using the App Router

1. **Add the key**: Add the key to `.env.local`. Don't prefix it with `NEXT_PUBLIC_`, that would expose it to the browser.

```
SENDTERN_API_KEY=st_your_api_key
```

2. **Create a Route Handler**: Create `app/api/send/route.js`. It runs on the server only.

```
// app/api/send/route.js
export async function POST(request) {
  const { email } = await request.json();

  const res = await fetch("https://sendtern.com/emails", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      from: "Acme <hello@mail.example.com>",
      to: [email],
      subject: "Welcome to Acme",
      html: "<p>Thanks for signing up.</p>",
    }),
  });

  return Response.json(await res.json(), { status: res.status });
}
```

3. **Check the response**: A successful request returns `200 OK` with the email id. Use it to look the email up later.

```
{
  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
}
```

> **tip:** Replace the example sender with an address on your [verified domain](https://sendtern.com/docs/domains).
