# 使用 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) 上的地址。
