---
title: "发起结账"
canonical_url: "https://tweetstream.io/zh-cn/docs/api/create-agent-checkout"
last_updated: 2026-11-05
---

# 发起结账

发起结账，得到一个由对方打开的付款页面。默认使用加密货币；如需银行卡，发送 paymentRail CARD。账号还有试用时会使用试用。响应中的 next 说明下一步：open_payment（把 paymentUrl 交给对方，然后查询 pollUrl）、join_discord 或 wait。用相同的 Idempotency-Key 重放会返回同一个结账。

Canonical URL: https://tweetstream.io/zh-cn/docs/api/create-agent-checkout

## `POST /api/agent/checkout`

`https://tweetstream.io/api/agent/checkout`

## 授权

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `Authorization` | `string`, 必填 | 使用 TweetStream API 密钥进行 Bearer 认证：`Bearer <key>`。 |

## 请求头

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `Idempotency-Key` | `string`, 必填 | 必填。每次购买或变更生成一个 key，重试时沿用；相同的 key 返回相同的结果。 |
| `X-TweetStream-Setup-Proof` | `string` | 可选。邮箱验证返回的 setupProof；带上后，Discord 步骤会包含加入链接。 |

## 请求体

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `billingCycle` | `enum<string>` |  |
| `paymentRail` | `enum<string>` |  |
| `trial` | `enum<string>` |  |
| `family` | `enum<string>`, 必填 |  |
| `planType` | `enum<string>`, 必填 |  |

### 请求体示例

```json
{
  "billingCycle": "MONTHLY",
  "paymentRail": "CRYPTO",
  "trial": "if_eligible",
  "family": "BASE",
  "planType": "BASIC"
}
```

## 响应字段

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `checkoutId` | `string` | a string matching the pattern ^pl_[a-z0-9]{1,64}$ |
| `family` | `enum<string>`, 必填 |  |
| `next` | `object`, 必填 |  |
| `next.message` | `string` | a string at most 500 character(s) long |
| `next.action` | `enum<string>`, 必填 |  |
| `next.method` | `enum<string>`, 必填 |  |
| `next.url` | `string`, 必填 | a string matching the pattern ^(https\|wss):\/\/[^\s]+$ |
| `next.who` | `enum<string>`, 必填 |  |
| `offer` | `object` |  |
| `offer.amountDueNow` | `object`, 必填 |  |
| `offer.amountDueNow.currency` | `enum<string>`, 必填 |  |
| `offer.amountDueNow.minor` | `string`, 必填 | a string matching the pattern ^(0\|[1-9][0-9]*)$ |
| `offer.mode` | `enum<string>`, 必填 |  |
| `offer.planName` | `string`, 必填 |  |
| `offer.renewal` | `object`, 必填 |  |
| `offer.renewal.amount` | `object`, 必填 |  |
| `offer.renewal.amount.currency` | `enum<string>`, 必填 |  |
| `offer.renewal.amount.minor` | `string`, 必填 | a string matching the pattern ^(0\|[1-9][0-9]*)$ |
| `offer.renewal.at` | `string`, 必填 | a string to be decoded into a Date |
| `offer.renewal.billingCycle` | `enum<string>`, 必填 |  |
| `offer.renewal.thenAmount` | `object`, 必填 |  |
| `offer.renewal.thenAmount.currency` | `enum<string>`, 必填 |  |
| `offer.renewal.thenAmount.minor` | `string`, 必填 | a string matching the pattern ^(0\|[1-9][0-9]*)$ |
| `paymentUrl` | `string` | a string matching the pattern ^(https\|wss):\/\/[^\s]+$ |
| `reason` | `enum<string>`, 必填 |  |
| `status` | `enum<string>`, 必填 |  |

## 响应

| 状态 | 说明 | 请求头 |
| --- | --- | --- |
| `200` | 重放请求、产品已开通，或对方需要先加入 Discord |  |
| `201` | 已创建结账 |  |
| `202` | 结账正在准备中，请查询状态 |  |
| `400` | 请求无效、验证码错误，或未接受服务条款 |  |
| `401` | API key 缺失或无效，或 setup proof 缺失或已过期 |  |
| `403` | 账号受到限制，或请求带有外部 Origin |  |
| `409` | 幂等键、结账、优惠或试用冲突 |  |
| `429` | 已达到速率限制或开通额度，请在 retryAfterSeconds 秒后重试 |  |
| `503` | 暂时不可用，请稍后重试 |  |

### 200

```json
{
  "status": "community_required",
  "family": "BASE",
  "reason": "COMMUNITY_REQUIRED",
  "checkoutId": "pl_8k2m4q7x",
  "discord": {
    "url": "https://tweetstream.io/connect/discord/EXAMPLE_DISCORD_LINK",
    "expiresAt": "2026-10-29T13:00:00.000Z"
  },
  "next": {
    "action": "join_discord",
    "who": "human",
    "url": "https://tweetstream.io/connect/discord/EXAMPLE_DISCORD_LINK",
    "pollUrl": "https://tweetstream.io/api/agent/status?checkoutId=pl_8k2m4q7x",
    "afterSeconds": 5
  }
}
```

### 201

```json
{
  "status": "payment_required",
  "family": "BASE",
  "reason": "new",
  "checkoutId": "pl_8k2m4q7x",
  "offer": {
    "mode": "trial",
    "planName": "Pro",
    "amountDueNow": {
      "currency": "USD",
      "minor": "0"
    },
    "renewal": {
      "amount": {
        "currency": "USD",
        "minor": "49900"
      },
      "at": "2026-11-01T12:00:00.000Z",
      "billingCycle": "MONTHLY",
      "thenAmount": {
        "currency": "USD",
        "minor": "49900"
      }
    }
  },
  "paymentUrl": "https://tweetstream.io/pay/EXAMPLE_PAYMENT_LINK",
  "next": {
    "action": "open_payment",
    "who": "human",
    "url": "https://tweetstream.io/pay/EXAMPLE_PAYMENT_LINK",
    "pollUrl": "https://tweetstream.io/api/agent/status?family=BASE",
    "afterSeconds": 5
  }
}
```

### 202

```json
{
  "status": "processing",
  "family": "BASE",
  "checkoutId": "pl_8k2m4q7x",
  "next": {
    "action": "wait",
    "who": "agent",
    "pollUrl": "https://tweetstream.io/api/agent/status?checkoutId=pl_8k2m4q7x",
    "afterSeconds": 5
  }
}
```

### 400

```json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "<string>",
    "reason": "not_offered",
    "retryAfterSeconds": 123
  },
  "next": {
    "message": "<string>",
    "action": "call_action",
    "method": "POST",
    "name": "<string>",
    "url": "<string>",
    "who": "agent"
  }
}
```

### 401

```json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "<string>",
    "reason": "not_offered",
    "retryAfterSeconds": 123
  },
  "next": {
    "message": "<string>",
    "action": "call_action",
    "method": "POST",
    "name": "<string>",
    "url": "<string>",
    "who": "agent"
  }
}
```

### 403

```json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "<string>",
    "reason": "not_offered",
    "retryAfterSeconds": 123
  },
  "next": {
    "message": "<string>",
    "action": "call_action",
    "method": "POST",
    "name": "<string>",
    "url": "<string>",
    "who": "agent"
  }
}
```

### 409

```json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "<string>",
    "reason": "not_offered",
    "retryAfterSeconds": 123
  },
  "next": {
    "message": "<string>",
    "action": "call_action",
    "method": "POST",
    "name": "<string>",
    "url": "<string>",
    "who": "agent"
  }
}
```

### 429

```json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "<string>",
    "reason": "not_offered",
    "retryAfterSeconds": 123
  },
  "next": {
    "message": "<string>",
    "action": "call_action",
    "method": "POST",
    "name": "<string>",
    "url": "<string>",
    "who": "agent"
  }
}
```

### 503

```json
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "<string>",
    "reason": "not_offered",
    "retryAfterSeconds": 123
  },
  "next": {
    "message": "<string>",
    "action": "call_action",
    "method": "POST",
    "name": "<string>",
    "url": "<string>",
    "who": "agent"
  }
}
```

## 代码示例

### cURL

```bash
curl --request POST \
  --url https://tweetstream.io/api/agent/checkout \
  --header "Authorization: Bearer $TWEETSTREAM_API_KEY" \
  --header 'Idempotency-Key: 7c9e6679-7425-40de-944b-e07fc1f90ae7' \
  --header 'Content-Type: application/json' \
  --data '{
  "billingCycle": "MONTHLY",
  "paymentRail": "CRYPTO",
  "trial": "if_eligible",
  "family": "BASE",
  "planType": "BASIC"
}'
```

### JavaScript

```javascript
const response = await fetch("https://tweetstream.io/api/agent/checkout", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.TWEETSTREAM_API_KEY}`,
    "Idempotency-Key": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "billingCycle": "MONTHLY",
    "paymentRail": "CRYPTO",
    "trial": "if_eligible",
    "family": "BASE",
    "planType": "BASIC"
  }),
});

console.log(response.status, await response.json());
```

### Python

```python
import os
import requests

response = requests.post(
    "https://tweetstream.io/api/agent/checkout",
    headers={"Authorization": f"Bearer {os.environ['TWEETSTREAM_API_KEY']}", "Idempotency-Key": "7c9e6679-7425-40de-944b-e07fc1f90ae7"},
    json={
        "billingCycle": "MONTHLY",
        "paymentRail": "CRYPTO",
        "trial": "if_eligible",
        "family": "BASE",
        "planType": "BASIC",
    },
)

print(response.status_code, response.json())
```
