开发者
API 快速入门
兼容 OpenAI 的 Cosvexa API:创建密钥、发出第一个请求、流式输出、工具调用和 Auto 路由。
Cosvexa API 的对话补全和模型列表接口兼容 OpenAI:只需在 OpenAI SDK 中更改 base URL 即可使用。在 Pro、Team 和 Enterprise 上,密钥可使用你的套餐包含的所有模型;在 Free 上,密钥可在每天 50 次免费提问的额度内使用 Cosvexa-1。
https://cosvexa.ai/api/v1API 密钥
- 1
打开开发者控制台
前往开发者页面的密钥标签页。你需要是该工作区的管理员或所有者。
- 2
创建密钥
密钥以
cvx_开头,且只显示一次;请立即妥善保存。我们只保存其哈希值。 - 3
使用密钥
在每个请求中发送
Authorization: Bearer cvx_...。你可以在同一页面撤销密钥。
切勿把密钥放在浏览器代码或公开仓库中。API 不允许跨域的浏览器调用(CORS);请从你的服务器调用。
第一个请求
curl https://cosvexa.ai/api/v1/chat/completions \
-H "Authorization: Bearer $COSVEXA_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"cosvexa-auto","messages":[{"role":"user","content":"Hello"}]}'使用 OpenAI SDK(Python)
from openai import OpenAI
client = OpenAI(base_url="https://cosvexa.ai/api/v1", api_key="cvx_...")
res = client.chat.completions.create(
model="cosvexa-auto",
messages=[{"role": "user", "content": "Hello"}],
)
print(res.choices[0].message.content)使用 OpenAI SDK(JavaScript)
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://cosvexa.ai/api/v1", apiKey: process.env.COSVEXA_API_KEY });
const res = await client.chat.completions.create({
model: "claude-sonnet-5-5",
messages: [{ role: "user", content: "Hello" }],
});
console.log(res.choices[0].message.content);选择模型
cosvexa-auto(或auto):Cosvexa 会对任务进行分类并挑选合适的模型。响应中的model字段会显示由哪个模型回答。- 如需指定模型,请使用 `GET /models` 返回的
id(如claude-opus-5-5、gpt-6-astra)。指定的模型绝不会被悄悄替换。 - Council 和 Project 模式无法通过 API 使用。
流式输出
curl -N https://cosvexa.ai/api/v1/chat/completions \
-H "Authorization: Bearer $COSVEXA_API_KEY" -H "Content-Type: application/json" \
-d '{"model":"cosvexa-auto","stream":true,"stream_options":{"include_usage":true},
"messages":[{"role":"user","content":"Write a haiku"}]}'响应为 text/event-stream:每个数据块是一行 data: {...},流以 data: [DONE] 结束。设置 include_usage 后,最后一个数据块包含 token 用量和扣除的积分;不设置时,流不会报告积分。如果流式输出途中出错,会发送带有错误 status 和 code 的 data: {"error":{...}},随后是 [DONE];详见 API 参考。
工具调用(函数调用)
{
"model": "cosvexa-auto",
"messages": [{ "role": "user", "content": "What's the weather in Istanbul?" }],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"parameters": { "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] }
}
}],
"tool_choice": "auto"
}当模型需要调用工具时,finish_reason 为 tool_calls,并返回 message.tool_calls。请自行运行该工具,再用 role: "tool" 和 tool_call_id 把结果发回。在 Auto 中,带工具的请求会按代码任务路由;选择不支持工具的模型会返回 400。
图像输入
在消息内容中使用 {"type":"image_url","image_url":{"url":"data:image/png;base64,..."}}。只接受 base64 data: URL(PNG、JPEG、WebP、GIF);出于安全考虑,远程 URL 会被拒绝。
所有端点、错误码和限制:API 参考。