如何取得 Anthropic API key:console.anthropic.com 註冊、Billing 與 Messages API curl
按 Anthropic 官方文件梳理 Console 帳號、Billing、建立 API key、首條 Messages curl,以及速率與 Spend 限制。
要在代码里调用 Claude Messages API,通常需要先在 console.anthropic.com(Claude Console)注册组织、开通计费,再创建 Anthropic API key——这与 claude.ai 的 Claude Pro 订阅是分开计费的。
本文只对照 Anthropic 官方文档(docs.anthropic.com 的 Get started、API overview、Rate limits)与帮助中心,梳理 Console 账号、Billing、创建 API 密钥,以及第一条 Messages API curl 与速率/Spend 限制。
Console 账号与 claude.ai 的区别
官方 API overview 写:Claude API 是位于 https://api.anthropic.com 的 REST API。使用前提包括 Claude Console 账号 与 API key(或 Workload Identity Federation)。Console 用于创建 API 密钥、管理团队、设置 billing,并在 Playground 试验;API 访问受 Commercial Terms of Service 约束(见 support.anthropic.com 帮助文章)。
Claude 帮助中心写:想在 claude.ai 用 Opus 是 Pro 订阅;Console API 按 token 另计费。不要用共享 Console 或转卖密钥绕过官方计费。
| 入口 | 用途 | 官方说明 |
|---|---|---|
| console.anthropic.com | API 密钥、Billing、Rate limits | docs.anthropic.com — Getting started |
| claude.ai | 聊天、Projects、Artifacts | 与 Console API 计费分离 |
| Messages API | POST /v1/messages | docs.anthropic.com — API overview |
在 Console 开通 Billing
- 1
创建 Claude Console 组织
打开 console.anthropic.com,用本人邮箱注册并创建组织。帮助中心写 Console 是创建 API keys、添加成员、设置 billing 的地方。部分地区可能需等待审核或补充信息,以页面提示为准。
- 2
进入 Settings → Billing
Rate limits 文档写:在 Claude Console 打开
Settings > Billing查看组织 monthly spend cap,并在 Spend limits 区点击 Adjust limit / Set limit 设置低于档位上限的自订 spend limit。 - 3
绑定合法支付方式
按 Billing 页引导添加支付方式。API 按用量计费;各 Usage tier 有 monthly spend cap(Rate limits 文档列 Start 档 $500 USD、Build $1,000、Scale $200,000,Custom 档与账户团队另行安排)。达到 tier spend cap 后 API 暂停至 UTC 次月 1 日 00:00,除非申请更高限额。
- 4
核对 Rate limits 与 Usage
Console 的 Rate limits 页显示组织 tier 与 RPM/ITPM/OTPM;Usage 页可查看 token 与缓存命中。需要更高限额可在 Rate limits 页使用 Request rate limit increase(文档说明)。
创建 Anthropic API key
- 1
打开 API keys
Getting started 写:在 Console 的 Account Settings 生成 API keys(文档小节 Get your Claude API key)。也可在 Console 导航进入 API keys 管理密钥。
- 2
选择密钥类型与过期时间
API overview 写:创建时可选择 key type 与 expiration。可为不同环境使用 workspaces 隔离;多 workspace 密钥请求需带
anthropic-workspace-id头(Authentication 文档)。 - 3
复制并只显示一次
创建后完整 secret 通常只显示一次。存入密码管理器或密钥库,不要提交到 Git、截图或共享。本地开发用
export ANTHROPIC_API_KEY=...(Get started 示例)。 - 4
区分 Console 密钥与 Admin API
Admin API 的
GET /v1/organizations/api_keys/{id}用于组织级程序化密钥管理,需要 Admin API key,且不会返回 secret。日常开发在 Console 的 API keys 页面创建普通密钥即可。
第一条 Messages API curl
Get started 官方示例:设置环境变量后向 Messages API 发送 POST。必填头包括 content-type: application/json、x-api-key(或 Authorization Bearer)、anthropic-version: 2023-06-01。
export ANTHROPIC_API_KEY="your-api-key-here"
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-5",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "What should I search for to find the latest developments in renewable energy?"
}
]
}'成功时返回 JSON,role: assistant 的 content 含文本块,usage 字段给出 input/output tokens(见 Get started 响应示例)。模型 ID 以 Console 与 Models API 当前列表为准。
401 / 403
检查密钥是否有效、是否绑定了正确 workspace,以及 Billing 是否已启用。Authentication 文档列必填头。
400 invalid_request
常见为 JSON 字段缺失或 model 名称错误。Messages API reference 列必填参数。
413 request_too_large
API overview 写 Messages 请求体上限 32 MB。长上下文可拆批或使用 Message Batches API。
速率限制与 Spend 上限
Rate limits 文档区分两类限制:Spend limits(组织每月 API 费用上限)与 Rate limits(RPM、ITPM、OTPM)。组织按 Usage tier(Start / Build / Scale / Custom)自动分级;新组织或用量历史少的可能先进入 Evaluation tier,限额低于页面标准表。
| 限制类型 | 含义 | 触顶时 |
|---|---|---|
| RPM / ITPM / OTPM | 每分钟请求与 token | HTTP 429,带 retry-after |
| Tier spend cap | 每月组织 API 费用上限 | HTTP 429,enforced_spend_limit_reached,无 retry-after |
| 自订 spend limit | Billing 页低于 cap 的自设值 | HTTP 400 invalid_request_error |
文档写 API 使用 token bucket 算法;短突发可能触发 429。Messages 对各模型分别计限;Start tier 下 Claude Opus 5 标准表为 1,000 RPM、2,000,000 ITPM、400,000 OTPM(以 Console Rate limits 页实时值为准)。多数模型下 prompt caching 的 cache read tokens 不计入 ITPM,可提高有效吞吐。
把 API 原型拿去团队白板评审时,回 wbstorm 用房间号即可,另见 建立或加入房間。Claude Pro 聊天订阅见 Claude Pro 訂閱指南;Cursor 规则见 如何撰寫 Cursor rules。
Claude Pro 包含 API 吗?
不包含。Claude Pro 是 claude.ai 订阅;Console API 按 token 单独计费。Anthropic 定价与帮助页面分别说明两者。
429 是速率还是账单封顶?
两者都可能返回 429。Rate limits 文档写:超 RPM/ITPM/OTPM 时有 retry-after;触达 tier monthly spend cap 时 error_code 为 enforced_spend_limit_reached 且无 retry-after。
密钥可以放进前端网页吗?
不要。API key 应只在服务器或本机安全环境使用。泄露后立即在 Console 轮换或吊销密钥。
这是 wbstorm 的功能吗?
不是。本文只讲 Anthropic Console 与 Claude API。wbstorm 是浏览器头脑风暴房间,与 API 密钥无关。