快速开始
本指南用一个可复制的流程,把 InOneAPI 接入到你的第一个服务:创建项目、生成子密钥、选择模型、发送请求,再确认用量和 Trace。整个过程不要求绑定某一家供应商;应用只依赖 InOneAPI 的公共模型 ID。
你将完成什么
- 在控制台创建一个项目,并为开发环境生成子密钥。
- 从模型目录确认能力、价格和可用路由。
- 使用 OpenAI 兼容 SDK 发送首个文本请求。
- 从响应头读取
X-Gateway-Trace-ID,在控制台定位请求。
1. 准备项目和密钥
项目是预算、模型白名单和用量统计的边界。进入控制台的“项目与密钥”,创建 my-app-development 项目,再生成一个仅用于本地开发的子密钥。密钥只完整展示一次,请立即保存到密码管理器。
export INONEAPI_API_KEY="ioa_sk_..."
不要把密钥写入浏览器代码、Git 仓库或日志。生产环境应使用独立项目和独立密钥。
2. 选择模型
在“模型与路由”中打开模型详情,确认输入/输出模态、上下文长度、工具调用和流式能力。记录公共模型 ID,例如 openai/gpt-4.1-mini。该 ID 是稳定的逻辑名称,底层供应商切换不会要求你改代码。
3. 安装 SDK 并发送请求
npm install openai
import OpenAI from "openai";
const apiKey = process.env.INONEAPI_API_KEY;
if (!apiKey) throw new Error("INONEAPI_API_KEY is not set");
const client = new OpenAI({
apiKey,
baseURL: "https://api.inoneapi.com/v1",
});
const response = await client.chat.completions.create({
model: "openai/gpt-4.1-mini",
messages: [{ role: "user", content: "用一句话解释 TTFT。" }],
});
console.log(response.choices[0]?.message.content);
等价的 HTTP 请求如下:
curl --fail-with-body https://api.inoneapi.com/v1/chat/completions \
-H "Authorization: Bearer $INONEAPI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"openai/gpt-4.1-mini","messages":[{"role":"user","content":"用一句话解释 TTFT。"}]}'
4. 检查 Trace 和用量
每个请求都会返回 X-Gateway-Trace-ID。在生产客户端中把它作为结构化日志字段保存:
const raw = await fetch("https://api.inoneapi.com/v1/chat/completions", {
method: "POST",
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify({ model: "openai/gpt-4.1-mini", messages: [{ role: "user", content: "你好" }] }),
});
console.log({ traceId: raw.headers.get("X-Gateway-Trace-ID"), status: raw.status });
若请求失败,先用状态码和 error.code 判断处理方式,再把 Trace ID 提供给支持人员。不要上传完整提示词或密钥。