API 概览
MOMO AI 提供强大的 RESTful API,让你可以轻松集成智能体到你的应用中。
Base URL
所有 API 请求的基础 URL:
https://api.momoai.pro/v1
认证
使用 Bearer Token 进行身份验证:
bash
curl -H "Authorization: Bearer YOUR_API_KEY" \
https://api.momoai.pro/v1/agents
请求格式
所有请求都使用 JSON 格式:
json
{
"prompt": "你的问题",
"model": "gpt5.2",
"temperature": 0.7,
"max_tokens": 1000
}
响应格式
成功响应:
json
{
"success": true,
"data": {
"id": "req_123456",
"content": "智能体的回答",
"tokens_used": 150,
"cost": 0.015
}
}
错误响应:
json
{
"success": false,
"error": {
"code": "INSUFFICIENT_BALANCE",
"message": "余额不足",
"details": "当前余额: 0.5 元,本次请求需要: 1.2 元"
}
}
核心端点
1. 列出所有智能体
http
GET /agents
响应示例:
json
{
"success": true,
"data": {
"agents": [
{
"id": 159,
"name": "gpt5.2",
"description": "最新的 GPT 模型",
"price_per_1k_tokens": 0.01,
"rating": 4.8
}
],
"total": 100,
"page": 1,
"page_size": 20
}
}
2. 调用智能体
http
POST /agents/{agent_id}
请求参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| prompt | string | 是 | 用户输入 |
| temperature | float | 否 | 温度参数 (0-1) |
| max_tokens | integer | 否 | 最大 token 数 |
| stream | boolean | 否 | 是否流式输出 |
请求示例:
javascript
const response = await fetch('https://api.momoai.pro/v1/agents/159', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: '请解释什么是递归',
temperature: 0.7,
max_tokens: 500
})
});
const data = await response.json();
console.log(data.data.content);
3. 流式输出
javascript
const response = await fetch('https://api.momoai.pro/v1/agents/159', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: '写一首诗',
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
process.stdout.write(data.delta);
}
}
}
4. 查询余额
http
GET /account/balance
响应示例:
json
{
"success": true,
"data": {
"balance": 98.5,
"currency": "CNY",
"agents": [
{
"agent_id": 159,
"remaining_tokens": 50000
}
]
}
}
错误代码
| 代码 | 说明 | 处理方式 |
|---|---|---|
INVALID_API_KEY | API Key 无效 | 检查 API Key 是否正确 |
INSUFFICIENT_BALANCE | 余额不足 | 充值或购买更多额度 |
RATE_LIMIT | 请求过于频繁 | 稍后重试,遵守限流规则 |
AGENT_NOT_FOUND | 智能体不存在 | 检查智能体 ID |
INVALID_PARAMETERS | 参数错误 | 检查请求参数 |
速率限制
| 等级 | 限制 |
|---|---|
| 免费用户 | 10 请求/分钟 |
| 基础版 | 60 请求/分钟 |
| 专业版 | 300 请求/分钟 |
| 企业版 | 无限制 |
SDK 支持
我们提供多种语言的 SDK:
JavaScript/TypeScript
bash
npm install @momoai/sdk
typescript
import { MomoAI } from '@momoai/sdk';
const client = new MomoAI({ apiKey: 'YOUR_API_KEY' });
const response = await client.agents.call(159, {
prompt: '你好,世界!'
});
Python
bash
pip install momoai
python
from momoai import MomoAI
client = MomoAI(api_key='YOUR_API_KEY')
response = client.agents.call(
agent_id=159,
prompt='Hello, World!'
)
print(response.content)
