# 改写（异步）

> 草案

`POST https://paperpurr.com/v1/rewrite`

提交一段文字排队改写，立即返回 `job_id`，结果用[任务状态](https://paperpurr.com/docs/jobs)或 [Webhook](https://paperpurr.com/docs/webhooks) 取。

> **说明：** 改写在首批接口之后开放，开放前调用返回 `503 coming_soon`。下面的字段是草案。

## 请求头

- `Authorization` (string，必填) — `Bearer` 加密钥
- `Idempotency-Key` (string) — 1–255 个字符。重试时带同一个值，不会重复建任务、重复扣费。

## 请求参数

- `text` (string，必填) — 要改写的文字，单次上限发布时公布
- `variants` (integer，默认 1) — 取值 1–4，返回几稿
- `webhook_url` (string) — `https://` 公网地址，任务结束时推送

## 请求示例

curl:

```bash
curl -X POST "https://paperpurr.com/v1/rewrite" \
  -H "Authorization: Bearer $PAPERPURR_API_KEY" \
  -H "Idempotency-Key: 5f1c9a7e-2d4b-4e8a-9c31-7b0e6f2a1d55" \
  -H "Content-Type: application/json" \
  -d '{"text":"Thoreau moved to the woods because he wished to live deliberately.","variants":2,"webhook_url":"https://example.com/hooks/paperpurr"}'
```

Python:

```python
import os
import requests

r = requests.post(
    "https://paperpurr.com/v1/rewrite",
    headers={
        "Authorization": f"Bearer {os.environ['PAPERPURR_API_KEY']}",
        "Idempotency-Key": "5f1c9a7e-2d4b-4e8a-9c31-7b0e6f2a1d55",
    },
    json={
        "text": "Thoreau moved to the woods because he wished to live deliberately.",
        "variants": 2,
        "webhook_url": "https://example.com/hooks/paperpurr"
    },
)
print(r.json())
```

JS:

```javascript
const r = await fetch("https://paperpurr.com/v1/rewrite", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.PAPERPURR_API_KEY}`,
    "Idempotency-Key": "5f1c9a7e-2d4b-4e8a-9c31-7b0e6f2a1d55",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "text": "Thoreau moved to the woods because he wished to live deliberately.",
    "variants": 2,
    "webhook_url": "https://example.com/hooks/paperpurr"
  }),
});
console.log(await r.json());
```

## 响应

202:

```json
{
  "job_id": "job_7Qm2xK",
  "status": "queued",
  "status_url": "/v1/jobs/job_7Qm2xK",
  "request_id": "req_51d0c8aa3e9f4b2c7d16a0e8"
}
```

503:

```json
{
  "error": {
    "type": "unavailable",
    "code": "coming_soon",
    "message": "开发者 API 即将发布",
    "param": null,
    "request_id": "req_d41a8c3e7f2b09a6e5c1d8f3"
  }
}
```

- `job_id` (string，必填) — 任务 ID
- `status` (string，必填) — 刚提交时是 `queued`
- `status_url` (string，必填) — 查询状态的路径
- `request_id` (string，必填) — 请求 ID

## 为什么是异步

改写跑在 GPU 上，空闲时要冷启动，可能排队几分钟。先返回 `job_id`，请求不用一直挂着；不想轮询就填 `webhook_url`。

## 可能的错误

| 状态码 | code | 原因 | 怎么处理 |
| --- | --- | --- | --- |
| `400` | `missing_parameter` | 缺少必填参数，`param` 指出是哪个 | 补上这个参数 |
| `400` | `invalid_parameter` | 参数格式或取值不对，`param` 指出是哪个 | 按接口文档的类型和范围改 |
| `402` | `insufficient_balance` | 本月试用额度不够这次请求要用的单位 | 查 `/v1/usage`，减少条数或等下个月重置；词数包发布时开卖 |
| `409` | `idempotency_conflict` | 同一个 `Idempotency-Key` 配了不同的请求体 | 每个新任务用新的键 |
| `413` | `text_too_long` | 超过单次上限 | 分段提交 |
| `422` | `unprocessable_content` | 内容无法处理 | 检查 `text` 是否为空白或乱码 |
| `429` | `rate_limit_exceeded` | 撞上了某一层限流（按 IP、按密钥或按账号），`message` 说明是哪一层 | 按 `Retry-After` 的秒数等待后重试；多个进程共用一把密钥时把请求排队 |
| `503` | `coming_soon` | 接口还没开放。现在调用任何 `/v1` 和 `/mcp` 都会返回它 | [加入候补](https://paperpurr.com/#api)，发布时在 Discord 通知 |

## 计量

- 按输入词数扣：一个汉字算一个词，其余按词算。实际扣数见任务结果里的 `usage.charged_words`。
- 同时运行的任务数按档位限制，见[限流与计量](https://paperpurr.com/docs/limits)。

下一页: [任务状态](https://paperpurr.com/docs/jobs.md)
