# REST API 参考 (/docs/developers/api)
Canonical: https://arcsvg.com/docs/developers/api

ArcSVG REST API v1 与 Web 工作台、Remote MCP 共用同一账号余额与任务历史。你可以在[工作台 API & MCP](/app/developers)创建与管理 API Key，或在 [API & MCP 页](/api)直接获取接入示例。

- **基础地址**：`https://arcsvg.com/api/v1`（已启用 CORS）
- **鉴权方式**：请求头携带 `Authorization: Bearer arc_live_...`（优先）或 `X-API-Key: arc_live_...`；不支持 URL Query 传密钥（单账号最多 **10** 个活跃密钥）
- **输入限制**：PNG / JPEG / WebP，单图 ≤ **20 MB**（按文件头校验）。传入 `image_url` 时仅限公网 `http/https` 地址，拒绝本地/内网/保留 IP、裸 IPv6 与带凭据 URL，抓取超时 **15 秒**且复检重定向落点
- **计费口径**：**1 张图 = 1 credit**，提交时扣费、失败自动原路退回（`failed` 响应中 `credits_cost` 为 `0`）
- **错误结构**：所有非成功错误默认返回 `{"error": {"code": "...", "message": "..."}}`（唯一例外：`POST /api/v1/vectorize` 传 `wait > 0` 且任务在等待窗内失败时，返回 HTTP `422` 与完整任务对象，详见下文）

## 异步优先 + 可选短等

1. `POST /api/v1/vectorize` 默认（`wait=0`）受理后立即返回 `202 Accepted` 与 `task_id`，避免耗时波动导致请求超时。
2. 传入可选 `wait`（`0–20` 秒，支持 Query `?wait=`、Form 字段 `wait` 或 JSON 字段 `wait` / `wait_seconds`）：简单图在等待窗内完成时直接返回 `200 OK`；超时未完成则降级返回 `202 Accepted`。
3. 轮询 `GET /api/v1/tasks/:id?wait=10` 支持服务端轻量退避长轮询（每次最多 20 秒），减少无效空转请求。

## 端点参考

### 1. 提交转 SVG 任务：`POST /api/v1/vectorize`

支持两种请求体：

**方式 A：`multipart/form-data`（推荐，无 Base64 体积膨胀）**

字段：`file`（图片文件，必填）、`wait`（可选，`0–20`）。

```bash
curl -X POST "https://arcsvg.com/api/v1/vectorize" \
  -H "Authorization: Bearer YOUR_ARCSVG_API_KEY" \
  -F "file=@logo.png"
```

默认异步受理响应（`202 Accepted`，响应头含 `X-Task-Id` 与 `X-Credits-Cost`）：

```json
{
  "task_id": "a6f7f1f0-...",
  "status": "running",
  "credits_cost": 1,
  "poll_url": "https://arcsvg.com/api/v1/tasks/a6f7f1f0-...",
  "svg_url": "https://arcsvg.com/api/v1/tasks/a6f7f1f0-.../svg",
  "poll_after_ms": 3000
}
```

**方式 B：`application/json`（`image_url` 或 `image_base64` 二选一，不可同传）**

```bash
curl -X POST "https://arcsvg.com/api/v1/vectorize" \
  -H "Authorization: Bearer YOUR_ARCSVG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"image_url":"https://example.com/logo.png","wait":15}'
```

当传入 `wait > 0` 时，接口返回完整任务对象（结构同 `GET /api/v1/tasks/:id`），HTTP 状态随终态变化：
- `200 OK`：在等待窗内完成（`status: "succeeded"`，含 `metrics` 与 `svg`）；
- `202 Accepted`：超时仍未完成（`status: "running"`，含 `poll_after_ms: 3000`）；
- `422 Unprocessable Entity`：在等待窗内失败（任务对象，`status: "failed"`、`credits_cost: 0` 与 `error` 原因字符串）。

### 2. 查询任务状态与结果：`GET /api/v1/tasks/:id`

可选查询参数：
- `wait`：`0–20`（秒，默认 `0`），任务仍在运行时做轻量退避等待；
- `include_svg`：`true`（默认）或 `false`（仅取状态与指标，不返回 `svg` 源码）。

只要任务存在且归属当前用户，无论处于 `queued`、`running`、`succeeded` 或 `failed` 均返回 `200 OK`：

```json
{
  "task_id": "a6f7f1f0-...",
  "status": "succeeded",
  "created_at": "2026-12-02T03:00:00.000Z",
  "credits_cost": 1,
  "source": { "format": "png", "width": 512, "height": 512, "bytes": 42180 },
  "poll_url": "https://arcsvg.com/api/v1/tasks/a6f7f1f0-...",
  "svg_url": "https://arcsvg.com/api/v1/tasks/a6f7f1f0-.../svg",
  "metrics": {
    "colors": 4,
    "shapes": 18,
    "nodes": 412,
    "svg_bytes": 11264,
    "svg_width": 512,
    "svg_height": 512,
    "duration_ms": 8420,
    "truncated": false,
    "repaired": false,
    "degenerate": false
  },
  "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" ...>...</svg>"
}
```

### 3. 直接下载原始 SVG：`GET /api/v1/tasks/:id/svg`

任务成功后返回 `200 OK` 与 `Content-Type: image/svg+xml; charset=utf-8`（附 `Cache-Control: private, max-age=3600`）；仍在处理中返回 `409 task_not_ready`；失败返回 `422 task_failed`；不存在返回 `404`。

```bash
curl "https://arcsvg.com/api/v1/tasks/<TASK_ID>/svg" \
  -H "Authorization: Bearer YOUR_ARCSVG_API_KEY" \
  -o output.svg
```

### 4. 查询最近任务列表：`GET /api/v1/tasks?limit=20`

按创建时间倒序返回当前用户最近的任务摘要，`limit` 范围 `1–50`（默认 `20`，不支持游标分页）：

```json
{
  "tasks": [
    {
      "task_id": "a6f7f1f0-...",
      "status": "succeeded",
      "created_at": "2026-12-02T03:00:00.000Z",
      "source": { "format": "png", "width": 512, "height": 512, "bytes": 42180 },
      "metrics": {
        "colors": 4,
        "shapes": 18,
        "nodes": 412,
        "svg_bytes": 11264,
        "truncated": false,
        "degenerate": false
      },
      "poll_url": "https://arcsvg.com/api/v1/tasks/a6f7f1f0-...",
      "svg_url": "https://arcsvg.com/api/v1/tasks/a6f7f1f0-.../svg"
    }
  ]
}
```

### 5. 余额查询：`GET /api/v1/credits`

`usd_value` 按基准锚价 `$0.12375 / credit` 折算（最多保留 4 位小数）：

```json
{
  "credits": 100,
  "usd_value": 12.375,
  "cost_per_image": 1,
  "estimated_images_remaining": 100
}
```

## 错误码一览

| HTTP 状态 | `error.code` | 说明 |
| --- | --- | --- |
| `400` | `missing_file` / `missing_image` / `ambiguous_image` / `empty_file` / `invalid_json` / `invalid_form_data` / `invalid_image_url` / `invalid_base64` | 请求体格式有误、缺少图片、同时传入 URL 与 Base64，或 URL / Base64 非法 |
| `401` | `missing_key` / `invalid_key` | 缺少、无效或已撤销的 API Key |
| `402` | `insufficient` | credits 余额不足（至少需要 `1 credit`） |
| `403` | `account_banned` | 当前账号已被封禁 |
| `404` | `task_not_found` / `svg_not_found` | 任务不存在、不属于当前账号，或产物缺失 |
| `409` | `task_not_ready` | 调用 `/svg` 下载时任务仍在排队或处理中 |
| `413` | `file_too_large` | 上传图片或拉取的远端图片超过 **20 MB** 上限 |
| `415` | `unsupported_content_type` / `unsupported_format` / `truncated` / `malformed` | 请求 `Content-Type` 不支持，或图片非 PNG / JPEG / WebP、文件头截断/损坏 |
| `422` | `image_fetch_failed` / `task_failed` | 远端图片拉取失败或任务失败（`POST /vectorize` 带 `wait` 失败时返回 `422` 与含 `error` 字符串的任务对象） |
| `502` / `503` | `inference_*` / `callback_base_url_missing` | 上游处理服务暂不可用（若已扣费均自动退回） |