# Node.js로 보내기

> 내장된 페치를 사용하여 Node.js 18+ 프로젝트에서 이메일을 보냅니다.

## 전제 조건

- A 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)의 주소로 바꿉니다.
