首次调用 API
APIGATE 提供 OpenAI 与 Anthropic 兼容接口。已有 SDK 或兼容客户端通常只需要替换 API 地址、密钥和模型名称。
| 配置项 | 配置值 |
|---|---|
| OpenAI Base URL | https://apigate.online/v1 |
| Anthropic Base URL | https://apigate.online |
| API Key | 在 APIGATE 控制台的令牌管理中创建 |
| Model | YOUR_MODEL,请填写控制台中可用的模型名称 |
不要在浏览器代码、公开仓库或截图中暴露 API Key。建议通过环境变量读取密钥。
接入 Agent 工具
APIGATE 可以作为兼容后端接入支持自定义 API 地址的 Agent 与编程助手。配置方式请查看接入 Agent 工具。
调用对话 API
创建 API Key 后,可以使用 OpenAI 兼容格式调用对话接口。下面均为非流式示例;将 stream 改为 true 即可接收流式响应。
curl
export APIGATE_API_KEY="YOUR_API_KEY"
curl https://apigate.online/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $APIGATE_API_KEY" \
-d '{
"model": "YOUR_MODEL",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好,请介绍一下你自己。"}
],
"stream": false
}'
Python
先安装 SDK:pip install openai
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["APIGATE_API_KEY"],
base_url="https://apigate.online/v1",
)
response = client.chat.completions.create(
model="YOUR_MODEL",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好,请介绍一下你自己。"},
],
stream=False,
)
print(response.choices[0].message.content)
Node.js
先安装 SDK:npm install openai
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.APIGATE_API_KEY,
baseURL: "https://apigate.online/v1",
});
const response = await client.chat.completions.create({
model: "YOUR_MODEL",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "你好,请介绍一下你自己。" },
],
stream: false,
});
console.log(response.choices[0].message.content);
常见响应
- 401:API Key 缺失、格式错误或已失效。
- 429:请求过于频繁、并发受限或额度不足。
- 400:模型名或请求参数不正确。
- 5xx:服务或上游暂时不可用,可使用指数退避进行有限重试。