---
title: "轮换实时流 key"
canonical_url: "https://tweetstream.io/zh-cn/docs/api/rotate-agent-credential"
last_updated: 2026-11-05
---

# 轮换实时流 key

替换已泄露的实时流 key。旧 key 及为它签发的 setup proof 立即失效，新 key 只在响应中出现一次。仅凭 key 无法轮换：需要带上近期邮箱验证码换来的 setup proof。此请求不可重放：用旧的标准 key 重试会得到 401，需重新验证邮箱验证码才能再次读取 key。

Canonical URL: https://tweetstream.io/zh-cn/docs/api/rotate-agent-credential

## `POST /api/agent/credentials/rotate`

`https://tweetstream.io/api/agent/credentials/rotate`

## 授权

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

## 请求头

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `X-TweetStream-Setup-Proof` | `string`, 必填 | 邮箱验证返回的 setupProof，有效期 30 分钟。过期后需重新验证邮箱验证码。 |

## 请求体

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

### 请求体示例

```json
{
  "family": "BASE"
}
```

## 响应字段

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `apiKey` | `string`, 必填 |  |
| `createdAt` | `string`, 必填 | a string to be decoded into a Date |
| `credentialScope` | `enum<string>`, 必填 |  |
| `family` | `enum<string>`, 必填 |  |

## 响应

| 状态 | 说明 | 请求头 |
| --- | --- | --- |
| `200` | 成功 |  |
| `400` | 请求无效、验证码错误，或未接受服务条款 |  |
| `401` | API key 缺失或无效，或 setup proof 缺失或已过期 |  |
| `403` | 账号受到限制，或请求带有外部 Origin |  |
| `404` | 结账或链接不存在，或账号没有该产品的使用权 |  |
| `429` | 已达到速率限制或开通额度，请在 retryAfterSeconds 秒后重试 |  |
| `503` | 暂时不可用，请稍后重试 |  |

### 200

```json
{
  "apiKey": "<the account's API key, shown once>",
  "createdAt": "2026-10-29T12:02:00.000Z",
  "credentialScope": "STANDARD",
  "family": "BASE"
}
```

### 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"
  }
}
```

### 404

```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/credentials/rotate \
  --header "Authorization: Bearer $TWEETSTREAM_API_KEY" \
  --header "X-TweetStream-Setup-Proof: $TWEETSTREAM_SETUP_PROOF" \
  --header 'Content-Type: application/json' \
  --data '{
  "family": "BASE"
}'
```

### JavaScript

```javascript
const response = await fetch("https://tweetstream.io/api/agent/credentials/rotate", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.TWEETSTREAM_API_KEY}`,
    "X-TweetStream-Setup-Proof": process.env.TWEETSTREAM_SETUP_PROOF,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "family": "BASE"
  }),
});

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

### Python

```python
import os
import requests

response = requests.post(
    "https://tweetstream.io/api/agent/credentials/rotate",
    headers={"Authorization": f"Bearer {os.environ['TWEETSTREAM_API_KEY']}", "X-TweetStream-Setup-Proof": os.environ["TWEETSTREAM_SETUP_PROOF"]},
    json={
        "family": "BASE",
    },
)

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