跳到正文

快速上手

从零到第一条回答,几分钟内完成——全程走原生 HTTP。无需安装任何 SDK:用 curl、Python、JavaScript、Go 等任意语言皆可调用。

1 · 获取项目密钥

每个请求都用 Authorization 头里的项目密钥鉴权。在控制台登录即可获取:使用 Google/GitHub 登录可获得一把项目密钥 + $2 免费额度(邮箱登录从 $0 起)。复制密钥并导出一次。当钱包余额用尽时会收到 402 INSUFFICIENT_CREDIT —— 调用 POST /v1/account/topup 并打开返回的 checkout_url 充值:

bash
# Get a project key: sign in with Google/GitHub at the console for your
# project key + $2 free credit (email sign-in starts at $0). Copy the key,
# then export it once:
export INFRAI_API_KEY="ifr_pk_proj_..."

# When the wallet runs out you get 402 INSUFFICIENT_CREDIT — top up and open
# the returned checkout_url to add funds:
curl -X POST https://api.infrai.cc/v1/account/topup \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"amount": 20, "currency": "USD", "payment_method": "stripe"}'

2 · AI 推理 —— 把 OpenAI SDK 指向 infrai

AI 推理采用 OpenAI API 兼容接口:你早已熟悉它。只需把 base URL 改成 https://api.infrai.cc/v1,即可照常使用 chat/completions、embeddings、images/generations、audio/speech、audio/transcriptions、moderations 与 models。零新 SDK、零新知识。

python
from openai import OpenAI

client = OpenAI(base_url="https://api.infrai.cc/v1", api_key=INFRAI_API_KEY)
resp = client.chat.completions.create(
    model="auto",  # infrai picks the best/cheapest live vendor; or pin "gpt-4o-mini"
    messages=[{"role": "user", "content": "Summarize the latest news"}],
)
print(resp.choices[0].message.content)
print(resp.infrai["cost_usd"], resp.infrai["vendor"])  # infrai cost/vendor extension

智能路由穿过 model 字段:model="auto" 让 infrai 选最优/最便宜的在线 vendor(用 extra_body 传 task/prefer),或锁定 "gpt-4o-mini" / "deepseek-chat" 等真实模型。成本与 vendor 通过顶层额外的 infrai 对象 + X-Infrai-* 响应头返回——OpenAI 客户端会忽略这个多出来的字段。(旧的自定义 /v1/ai/chat 形态已退役。)

3 · 任意其它能力

所有非 AI 能力都是对 https://api.infrai.cc/v1/... 的普通 HTTP 调用——带 Bearer 密钥 POST JSON。中国区 vendor 0% 加价、欧美 5%。发邮件、排定时任务、存文件——同一把密钥、同一套信封,只是路径不同。例如发送一封邮件:

bash
curl -X POST https://api.infrai.cc/v1/email/send \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"to": "alice@example.com", "subject": "Welcome", "html": "<p>Hi!</p>"}'

返回

json
{
  "ok": true,
  "data": { /* capability-specific result */ },
  "error": null,
  "metadata": {
    "cost_usd": 0.00012,
    "latency_ms": 412,
    "vendor": "deepseek",
    "cache_hit": false,
    "request_id": "01HXY..."   // UUID v7
  }
}

每次调用都返回同一个信封:`data` 是能力结果,成功时 `error` 为 null,`metadata` 携带成本、vendor、延迟与请求 id。完整规范见「约定」页。

任意语言

它只是 HTTP,所以任何带 HTTP 客户端的语言都能用——Go、Rust、Java、C#/.NET、Ruby、PHP 等等。每次响应都返回相同的 { ok, data, error, metadata } 信封,带成本、延迟与厂商元数据。

下一步