API-Mesh 提供完全兼容 OpenAI API 格式的接口,您可以在不修改现有代码的情况下,无缝切换到我们的平台,享受更优的价格和更稳定的服务。
https://api.api-mesh.io/v1API-Mesh 使用 Bearer Token 认证,请求时在 HTTP Header 中携带您的 API Key。
请求示例 Header:
Authorization: Bearer sk-am-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
在 控制台 → API Keys 页面可以创建和管理您的密钥。
与 OpenAI 完全一致的对话补全接口。支持所有主流模型。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
model | string | 是 | 模型 ID,参见模型列表 |
messages | array | 是 | 对话消息列表,格式同 OpenAI |
temperature | number | 否 | 采样温度,0-2,默认 1.0 |
max_tokens | integer | 否 | 最大生成 Token 数,默认 4096 |
top_p | number | 否 | 核采样参数,默认 1.0 |
frequency_penalty | number | 否 | 频率惩罚,-2 到 2 |
presence_penalty | number | 否 | 存在惩罚,-2 到 2 |
stream | boolean | 否 | 是否流式返回,默认 false |
curl https://api.api-mesh.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-am-your-api-key" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个有用的助手"},
{"role": "user", "content": "介绍一下API-Mesh"}
],
"temperature": 0.7,
"max_tokens": 1024
}'
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1721030400,
"model": "gpt-4o",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "API-Mesh 是一个智能API聚合平台..."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 128,
"total_tokens": 153
}
}
我们支持以下主流模型,价格实时优化。单价以美元计(每百万 Token)。
| 模型 ID | 提供商 | 输入价 $/1M | 输出价 $/1M | 上下文 |
|---|---|---|---|---|
gpt-4o | OpenAI | $2.50 | $7.50 | 128K |
gpt-4o-mini | OpenAI | $0.08 | $0.30 | 128K |
claude-3.5-sonnet | Anthropic | $2.00 | $8.00 | 200K |
claude-3-haiku | Anthropic | $0.15 | $0.80 | 200K |
deepseek-chat | DeepSeek | $0.14 | $0.55 | 128K |
qwen-max | 阿里云 | $1.20 | $3.60 | 32K |
glm-4-plus | 智谱AI | $3.00 | $3.00 | 128K |
ernie-4.0-turbo | 百度 | $1.80 | $3.60 | 8K |
* 价格可能随官方调整变动,以控制台实时显示为准
API 错误返回标准的 HTTP 状态码和 JSON 错误体。
| HTTP 状态码 | 错误类型 | 说明 |
|---|---|---|
| 400 | invalid_request_error | 请求参数格式错误或缺少必填参数 |
| 401 | authentication_error | API Key 无效、过期或未提供 |
| 402 | insufficient_quota | 账户余额不足,请充值 |
| 404 | not_found | 请求的资源或模型不存在 |
| 429 | rate_limit_error | 请求速率超过限制,请稍后重试 |
| 500 | api_error | 服务器内部错误,请稍后重试 |
| 503 | service_unavailable | 服务暂时不可用,故障转移中 |
{
"error": {
"message": "Insufficient quota. Your account balance is $0.00. Please top up.",
"type": "insufficient_quota",
"code": 402
}
}
为了保护服务稳定性,不同套餐有不同的速率限制。
| 套餐 | RPM (每分钟请求数) | TPM (每分钟Token数) | 并发数 |
|---|---|---|---|
| 免费版 | 60 | 100K | 5 |
| 开发者 (Pro) | 500 | 1M | 20 |
| 企业版 | 3000 | 10M | 100 |
| 定制版 | 自定义 | 自定义 | 自定义 |
当超过速率限制时,API 返回 429 状态码。建议在客户端实现指数退避重试策略。
以下示例展示如何用不同语言接入 API-Mesh。
import openai
client = openai.OpenAI(
api_key="sk-am-your-api-key",
base_url="https://api.api-mesh.io/v1"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "你是一个有用的助手"},
{"role": "user", "content": "介绍一下API-Mesh"}
],
temperature=0.7,
max_tokens=1024
)
print(response.choices[0].message.content)
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: 'sk-am-your-api-key',
baseURL: 'https://api.api-mesh.io/v1',
});
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: '你是一个有用的助手' },
{ role: 'user', content: '介绍一下API-Mesh' },
],
temperature: 0.7,
max_tokens: 1024,
});
console.log(response.choices[0].message.content);
curl https://api.api-mesh.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-am-your-api-key" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个有用的助手"},
{"role": "user", "content": "介绍一下API-Mesh"}
]
}'
package main
import (
"context"
"fmt"
openai "github.com/sashabaranov/go-openai"
)
func main() {
client := openai.NewClientWithConfig(openai.ClientConfig{
BaseURL: "https://api.api-mesh.io/v1",
APIType: openai.APITypeOpenAI,
})
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "gpt-4o",
Messages: []openai.ChatCompletionMessage{
{Role: "system", Content: "你是一个有用的助手"},
{Role: "user", Content: "介绍一下API-Mesh"},
},
},
)
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}