# Invia con Node.js

> Invia email da qualsiasi progetto Node.js 18+ utilizzando il fetch integrato.

## Prerequisiti

- Un Sendtern [chiave API](https://sendtern.com/docs/authentication) memorizzato come `SENDTERN_API_KEY`
- Node.js 18 or newer, which ships with `fetch`

1. **Invia un messaggio**: Create `send.mjs`. No package is needed.

```
// send.mjs
const res = await fetch("https://sendtern.com/emails", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": crypto.randomUUID(),
  },
  body: JSON.stringify({
    from: "Acme <hello@mail.example.com>",
    to: ["you@example.com"],
    subject: "Hello from Node.js",
    html: "<p>It works!</p>",
  }),
});

if (!res.ok) throw new Error((await res.json()).message);
console.log(await res.json());
```

2. **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"
}
```

```
node --env-file=.env send.mjs
```

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