快速上手
从零到第一条回答,几分钟内完成——全程走原生 HTTP。无需安装任何 SDK:用 curl、Python、JavaScript、Go 等任意语言皆可调用。
1 · 获取项目密钥
每个请求都用 Authorization 头里的项目密钥鉴权。在控制台登录即可获取:使用 Google/GitHub 登录可获得一把项目密钥 + $2 免费额度(邮箱登录从 $0 起)。复制密钥并导出一次。当钱包余额用尽时会收到 402 INSUFFICIENT_CREDIT —— 调用 POST /v1/account/topup 并打开返回的 checkout_url 充值:
# 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_..."
# 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、零新知识。
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。发邮件、排定时任务、存文件——同一把密钥、同一套信封,只是路径不同。例如发送一封邮件:
curl -X POST https://api.infrai.cc/v1/email/send \
-H "Authorization: Bearer $INFRAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"to": "sample"}'# Zero-install REST call — no SDK required (short-term the API is REST-only).
import os, requests
resp = requests.post(
"https://api.infrai.cc/v1/email/send",
headers={
"Authorization": f"Bearer {os.environ['INFRAI_API_KEY']}",
},
json={'to': 'sample'},
)
resp.raise_for_status()
print(resp.json())// Zero-install REST call — no SDK required (short-term the API is REST-only).
const resp = await fetch(
"https://api.infrai.cc/v1/email/send",
{
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.INFRAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({"to": "sample"}),
},
);
console.log(await resp.json());// Zero-install REST call — no SDK required (short-term the API is REST-only).
const resp = await fetch(
"https://api.infrai.cc/v1/email/send",
{
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.INFRAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({"to": "sample"}),
},
);
if (!resp.ok) throw new Error(`infrai ${resp.status}`);
const data: unknown = await resp.json();
console.log(data);// Zero-install REST call — no SDK required (short-term the API is REST-only).
package main
import (
"bytes"
"fmt"
"net/http"
"os"
)
func main() {
body := []byte(`{"to": "sample"}`)
req, _ := http.NewRequest("POST", "https://api.infrai.cc/v1/email/send", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("INFRAI_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println(resp.Status)
}// Zero-install REST call — no SDK required (short-term the API is REST-only).
import java.net.URI;
import java.net.http.*;
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("https://api.infrai.cc/v1/email/send"))
.header("Authorization", "Bearer " + System.getenv("INFRAI_API_KEY"))
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"to\": \"sample\"}"))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient()
.send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(resp.body());// Zero-install REST call — no SDK required (short-term the API is REST-only).
using System;
using System.Net.Http;
var client = new HttpClient();
var req = new HttpRequestMessage(new HttpMethod("POST"), "https://api.infrai.cc/v1/email/send");
var key = Environment.GetEnvironmentVariable("INFRAI_API_KEY");
req.Headers.Add("Authorization", "Bearer " + key);
req.Content = new StringContent("{\"to\": \"sample\"}", System.Text.Encoding.UTF8, "application/json");
var resp = await client.SendAsync(req);
Console.WriteLine(await resp.Content.ReadAsStringAsync());<?php
// Zero-install REST call — no SDK required (short-term the API is REST-only).
$ch = curl_init("https://api.infrai.cc/v1/email/send");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer " . getenv("INFRAI_API_KEY"),
"Content-Type: application/json",
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"to\": \"sample\"}");
$response = curl_exec($ch);
curl_close($ch);
echo $response;# Zero-install REST call — no SDK required (short-term the API is REST-only).
require "net/http"
require "uri"
uri = URI("https://api.infrai.cc/v1/email/send")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri)
req["Authorization"] = "Bearer #{ENV['INFRAI_API_KEY']}"
req["Content-Type"] = "application/json"
req.body = '{"to": "sample"}'
res = http.request(req)
puts res.body// Zero-install REST call — no SDK required (short-term the API is REST-only). (uses the reqwest + tokio crates)
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let resp = reqwest::Client::new()
.post("https://api.infrai.cc/v1/email/send")
.header("Authorization", format!("Bearer {}", env::var("INFRAI_API_KEY")?))
.header("Content-Type", "application/json")
.body(r#"{"to": "sample"}"#)
.send()
.await?;
println!("{}", resp.text().await?);
Ok(())
}返回
{
"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 } 信封,带成本、延迟与厂商元数据。