← 返回文档首页

原生 API

直接调用 OpenAI 兼容端点。任意语言都能接。

概览

Base URL 是 https://api.beansai.dev/v1。在 Authorization 头里带 Bearer token。端点和 OpenAI 一致:

  • POST /chat/completions —— 聊天、流式、tool calling
  • GET /models —— 列出可用模型

curl

shell
curl https://api.beansai.dev/v1/chat/completions \
  -H "Authorization: Bearer sk-beans-..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-7",
    "messages": [
      {"role": "user", "content": "Hello, world!"}
    ],
    "stream": false
  }'

基础用法

直接用 OpenAI 官方 SDK,换掉 base URL 即可。

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-beans-...",
    base_url="https://api.beansai.dev/v1",
)

res = client.chat.completions.create(
    model="claude-opus-4-7",
    messages=[{"role": "user", "content": "Hello!"}],
)

print(res.choices[0].message.content)
javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "sk-beans-...",
  baseURL: "https://api.beansai.dev/v1",
});

const res = await client.chat.completions.create({
  model: "claude-opus-4-7",
  messages: [{ role: "user", content: "Hello!" }],
});

console.log(res.choices[0]?.message?.content);

Streaming

shell
curl https://api.beansai.dev/v1/chat/completions \
  -N \
  -H "Authorization: Bearer sk-beans-..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-7",
    "messages": [{"role": "user", "content": "Count to 5"}],
    "stream": true
  }'

SSE 事件遵循 data: {json}\n\n 格式;结束事件是 data: [DONE]

List models

shell
curl https://api.beansai.dev/v1/models \
  -H "Authorization: Bearer sk-beans-..."

使用技巧

  • 限速按 key 计算,配额信息在 X-RateLimit-* 响应头里。
  • 遇到 429 或 5xx 用指数退避重试。BeansAI 的负载均衡器会挑一个健康的上游。
  • 每次请求的成本在 X-Request-Cost-Micro-Usd 头里返回,方便做实时计费 UI。