# Node.js で送信

> 内蔵されたフェッチを使って、Node.js 18以上のプロジェクトからメールを送信します。

## 前提条件

- Sendtern [API キー](https://sendtern.com/docs/authentication) が `SENDTERN_API_KEY` として保存されます
- Node.js 18 or newer, which ships with `fetch`

1. **メールを送信**: 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. **応答をチェック**: 成功した要求は、メール id を含む `200 OK` を返します。これを使って後でメールを検索してください。

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

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

> **tip:** 例の送信者を自分のメールアドレスで置き換えます。 [確認されたドメイン](https://sendtern.com/docs/domains).
