# 用Next.js發送

> 從路由處理器發送，以便密鑰永遠不會到達瀏覽器。

## 前提條件

- 存儲為 `SENDTERN_API_KEY` 的 Sendtern [API 鍵](https://sendtern.com/docs/authentication)
- A Next.js 13.4+ app using the App Router

1. **加入密鑰**: 將密鑰加入 `.env.local`。不要將它前綴為 `NEXT_PUBLIC_`，否則會讓瀏覽器看到它。

```
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. **檢查一下反應**: 成功的請求返回 `200 OK` 與電子郵件 ID。使用它以後查看電子郵件。

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

> **tip:** 用您的 [verified domain](https://sendtern.com/docs/domains) 的地址替换示例发件人。
