# 开开邮箱提取 API

## 基础信息

- 基础地址：`http://23.148.180.244:28017`
- 用户入口：`http://23.148.180.244:28017/user/`
- API 前缀：`/user/api/v1`
- 请求和响应格式：`application/json`
- 母号邮箱：每个扣除 `2` 积分
- 子号邮箱：每个扣除 `1` 积分
- 单次提取范围：`1` 到 `20000`
- 导出文件格式：`邮箱----取码URL`，一行一个
- 下载链接和文件保留：任务完成后 `2` 天

提取任务采用异步处理。提交成功只代表任务进入队列，客户端应轮询任务状态；只有状态变为 `completed` 才能下载。

## API Key 鉴权

在用户后台“生成 API Key”菜单创建密钥。完整密钥只显示一次。

使用标准 Bearer Header：

```http
Authorization: Bearer uk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

也支持：

```http
X-API-Key: uk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

API Key 不得放在 URL 查询参数中。

## 查询实时库存

`GET /user/api/v1/inventory`

```bash
curl -sS \
  -H "Authorization: Bearer API_KEY" \
  "http://23.148.180.244:28017/user/api/v1/inventory"
```

响应：

```json
{
  "ok": true,
  "inventory": {
    "mother": 12543,
    "child": 61,
    "mother_cost_cents": 200,
    "child_cost_cents": 100
  }
}
```

`*_cost_cents` 使用百分之一积分为单位，`200` 表示 `2` 积分。

## 提交提取任务

`POST /user/api/v1/exports`

请求 Header：

```http
Authorization: Bearer API_KEY
Content-Type: application/json
Idempotency-Key: order_20261128_000001
```

请求 Body：

```json
{
  "account_type": "child",
  "count": 100
}
```

字段说明：

| 字段 | 类型 | 必填 | 说明 |
|---|---:|---:|---|
| `account_type` | string | 是 | `child` 子号，`mother` 母号 |
| `count` | integer | 是 | 提取数量，范围 `1..20000` |

`Idempotency-Key` 用于防止网络重试造成重复任务和重复预占积分。相同用户使用相同 Key 重试时，服务端返回原任务。建议每个业务订单生成一个稳定且唯一的 Key，仅使用字母、数字、点、下划线和短横线，最长128字符。

```bash
curl -sS -X POST \
  -H "Authorization: Bearer API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order_20261128_000001" \
  -d '{"account_type":"child","count":100}' \
  "http://23.148.180.244:28017/user/api/v1/exports"
```

响应：

```json
{
  "ok": true,
  "already_exists": false,
  "export": {
    "id": "uex_0123456789abcdef",
    "status": "queued",
    "source": "api",
    "account_type": "child",
    "requested_count": 100,
    "actual_count": 0,
    "unit_cost_cents": 100,
    "charged_cents": 0,
    "created_at": "2026-11-28T08:00:00Z"
  }
}
```

## 查询单个任务

`GET /user/api/v1/exports/{id}`

```bash
curl -sS \
  -H "Authorization: Bearer API_KEY" \
  "http://23.148.180.244:28017/user/api/v1/exports/uex_0123456789abcdef"
```

完成响应：

```json
{
  "ok": true,
  "export": {
    "id": "uex_0123456789abcdef",
    "status": "completed",
    "source": "api",
    "account_type": "child",
    "requested_count": 100,
    "actual_count": 100,
    "unit_cost_cents": 100,
    "charged_cents": 10000,
    "error": "",
    "created_at": "2026-11-28T08:00:00Z",
    "completed_at": "2026-11-28T08:00:03Z",
    "download_url": "/user/api/v1/exports/uex_0123456789abcdef/download"
  }
}
```

任务状态：

| 状态 | 说明 |
|---|---|
| `queued` | 已进入队列 |
| `running` | 正在领取未导出邮箱 |
| `writing` | 正在生成文件并结算积分 |
| `completed` | 成功，可下载 |
| `failed` | 失败，已释放预占积分并恢复邮箱 |
| `reversed` | 已冲正 |

推荐轮询间隔为 `1.5` 到 `3` 秒。进入终态后停止轮询。

## 查询提取记录

`GET /user/api/v1/exports`

返回当前用户最近300条提取记录。

```bash
curl -sS \
  -H "Authorization: Bearer API_KEY" \
  "http://23.148.180.244:28017/user/api/v1/exports"
```

## 下载 TXT 文件

`GET /user/api/v1/exports/{id}/download`

```bash
curl -fL \
  -H "Authorization: Bearer API_KEY" \
  -o "kaikai_export.txt" \
  "http://23.148.180.244:28017/user/api/v1/exports/uex_0123456789abcdef/download"
```

文件示例：

```text
alias-one@icloud.com----http://23.148.180.244:28017/api/plus/v4/getcode?id=alias-one@icloud.com
alias-two@icloud.com----http://23.148.180.244:28017/api/plus/v4/getcode?id=alias-two@icloud.com
```

## 错误响应

```json
{
  "ok": false,
  "code": "INSUFFICIENT_POINTS",
  "error": "可用积分不足，本次最多预占100.00积分"
}
```

常见错误码：

| HTTP | code | 说明 |
|---:|---|---|
| 400 | `INVALID_ACCOUNT_TYPE` | 邮箱类型无效 |
| 400 | `INVALID_COUNT` | 数量不在允许范围 |
| 400 | `INVALID_IDEMPOTENCY_KEY` | 幂等键格式无效 |
| 401 | `AUTH_REQUIRED` | API Key 无效、已撤销或用户已禁用 |
| 409 | `INSUFFICIENT_POINTS` | 可用积分不足 |
| 404 | `EXPORT_NOT_FOUND` | 任务不存在或不属于当前用户 |
| 404 | `FILE_NOT_FOUND` | 文件已过期或不存在 |

## 完整调用流程

```bash
BASE="http://23.148.180.244:28017"
KEY="API_KEY"
IDEMPOTENCY_KEY="order_$(date +%s)"

RESPONSE=$(curl -sS -X POST \
  -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
  -d '{"account_type":"child","count":100}' \
  "$BASE/user/api/v1/exports")

EXPORT_ID=$(printf '%s' "$RESPONSE" | jq -r '.export.id')

while true; do
  RESULT=$(curl -sS -H "Authorization: Bearer $KEY" \
    "$BASE/user/api/v1/exports/$EXPORT_ID")
  STATUS=$(printf '%s' "$RESULT" | jq -r '.export.status')
  [ "$STATUS" = "completed" ] && break
  [ "$STATUS" = "failed" ] && { printf '%s\n' "$RESULT"; exit 1; }
  sleep 2
done

curl -fL -H "Authorization: Bearer $KEY" \
  -o "${EXPORT_ID}.txt" \
  "$BASE/user/api/v1/exports/$EXPORT_ID/download"
```
