# 注册账号

创建邮箱密码账号，并立即建立会话。

POST `/api/account/register`

- **认证** 公开，无需登录。
- **成功状态** `201`

## 请求参数

| 参数 | 类型 | 位置 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| `email` | string | `body` | 是 | 有效的邮箱地址，去空白后最多 254 个字符。 |
| `password` | string | `body` | 是 | 新密码为 10–128 个字符。 |
| `name` | string | `body` | 否 | 显示名，1–60 个字符。省略时使用邮箱 @ 之前的部分。 |
| `locale` | zh \| en | `body` | 否 | 用于注册偏好或验证码邮件语言。 |

## 请求示例

**cURL**

```bash
curl -i -X POST 'https://social2api.com/api/account/register' \
  -H 'Content-Type: application/json' \
  --data-binary '{
  "email": "reader@example.com",
  "password": "<new password>",
  "name": "API Reader"
}'
```

**Python**

```python
from urllib.error import HTTPError
from urllib.request import Request, urlopen

request = Request(
    "https://social2api.com/api/account/register",
    method="POST",
    headers={
        "Content-Type": "application/json",
    },
    data="{\"email\":\"reader@example.com\",\"password\":\"<new password>\",\"name\":\"API Reader\"}".encode("utf-8"),
)
send = urlopen
try:
    with send(request) as response:
        print(response.status, response.read().decode("utf-8"))
except HTTPError as error:
    print(error.code, error.read().decode("utf-8"))
```

**JavaScript**

```javascript
const response = await fetch("/api/account/register", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "email": "reader@example.com",
    "password": "<new password>",
    "name": "API Reader"
  }),
});
console.log(response.status, await response.text());
```

## 响应

**201 · 示例响应**

```json
{
  "account": {
    "id": "a2508bc7-316a-4d0c-93ab-ba87f7c3545e",
    "email": "reader@example.com",
    "name": "API Reader",
    "createdAt": "2026-01-15T12:00:00.000Z",
    "emailVerified": false,
    "hasPassword": true,
    "preferences": {}
  }
}
```

**错误示例**

```json
{
  "error": "账户服务暂不可用，请稍后重试。"
}
```

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `account` | object | 当前操作涉及的账号。 |
| `account.id` | string | 账号 ID。 |
| `account.email` | string | 规范化的邮箱地址。 |
| `account.name` | string | 显示名。 |
| `account.createdAt` | ISO 8601 date-time | 账号创建时间。 |
| `account.emailVerified` | boolean | 邮箱是否通过验证。 |
| `account.hasPassword` | boolean | 该账号是否已设置密码。 |
| `account.preferences` | object | 已存储的账号偏好。 |

## 错误状态

| HTTP | 原因与处理 |
| --- | --- |
| `400` | 请求格式或字段无效。 |
| `403` | 浏览器来源校验未通过。 |
| `413` | 请求体超出 4,096 字节。 |
| `415` | 需要 application/json 请求体。 |
| `405` | 请求方法不受支持。 |
| `500` | 账户服务暂时不可用。 |
| `503` | 此部署未配置该控制台服务。 |
| `409` | 此邮箱已注册。 |
| `429` | 尝试次数过多。根据 Retry-After 的秒数再试。 |

## 使用细节

-   账户写请求来自站点同源页面。跨站请求由服务器拒绝。
-   JSON 请求体最多 4,096 字节。
-   登录成功后响应以 Set-Cookie 建立或更新会话。
-   密码注册后邮箱状态仍待验证。

## 同组接口

-   [GET 读取登录会话](<https://social2api.com/docs/api/account-session/>)
-   [POST 登录账号](<https://social2api.com/docs/api/account-login/>)
-   [POST 退出登录](<https://social2api.com/docs/api/account-logout/>)
-   [PATCH 更新账号显示名](<https://social2api.com/docs/api/account-profile/>)
-   [PATCH 更新账号语言](<https://social2api.com/docs/api/account-preferences/>)
-   [POST 设置或修改密码](<https://social2api.com/docs/api/account-password/>)
-   [POST 删除当前账号](<https://social2api.com/docs/api/account-delete/>)
-   [POST 发送邮箱验证码](<https://social2api.com/docs/api/account-otp-request/>)
-   [POST 验证邮箱验证码](<https://social2api.com/docs/api/account-otp-verify/>)
-   [GET 开始 Google 登录](<https://social2api.com/docs/api/account-google/>)
-   [GET 处理 Google 登录回调](<https://social2api.com/docs/api/account-google-callback/>)

规范页面：<https://social2api.com/docs/api/account-register/>
