# Invia con Next.js

> Invia da un Route Handler in modo che la chiave non raggiunga mai il browser.

## Prerequisiti

- Un Sendtern [chiave API](https://sendtern.com/docs/authentication) memorizzato come `SENDTERN_API_KEY`
- A Next.js 13.4+ app using the App Router

1. **Aggiungere la chiave**: Aggiungere la chiave a `.env.local`, non prefiggerla con `NEXT_PUBLIC_`, che la esporrebbe al 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. **Controlla la risposta**: Una richiesta riuscita restituisce `200 OK` con l'ID email. Usalo per cercare l'email in seguito.

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

> **tip:** Sostituisci il mittente di esempio con un indirizzo sul tuo [dominio verificato](https://sendtern.com/docs/domains).
