cURL 调用示例
本页面提供使用 cURL 命令行工具调用 51API 的完整示例。
适用场景
- 快速测试 API 是否正常
- 在 Shell 脚本中调用 AI
- 不想安装任何 SDK
- 调试和排查问题
基础信息
| 配置项 | 值 |
|---|---|
| Base URL | https://www.51api.org/v1 |
| 鉴权方式 | Authorization: Bearer <API Key> |
| 请求格式 | application/json |
基础对话
最简单的请求
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "你好!"}]
}'1
2
3
4
5
6
7
2
3
4
5
6
7
带系统提示词
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个专业的翻译助手,请将用户的输入翻译成英文。"},
{"role": "user", "content": "今天天气真好"}
]
}'1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
响应示例
json
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么可以帮助你的吗?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 15,
"total_tokens": 25
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
流式输出
添加 "stream": true 参数,实现实时输出。
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "讲一个简短的故事"}],
"stream": true
}'1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
流式响应格式
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"从"}}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"前"}}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"有"}}]}
data: [DONE]1
2
3
4
5
6
7
2
3
4
5
6
7
常用参数
控制输出长度
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "写一首诗"}],
"max_tokens": 100
}'1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
控制随机性
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "给我一个创意点子"}],
"temperature": 1.5
}'1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
| temperature 值 | 效果 |
|---|---|
| 0 | 最确定,适合事实性问答 |
| 0.7 | 默认,平衡创意和准确性 |
| 1.5-2 | 更随机,适合创意写作 |
多轮对话
将之前的对话历史放入 messages 数组:
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "我叫小明"},
{"role": "assistant", "content": "你好小明!很高兴认识你。"},
{"role": "user", "content": "我刚才说我叫什么?"}
]
}'1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
图片分析
使用图片 URL
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "这张图片里有什么?"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg"}
}
]
}
],
"max_tokens": 300
}'1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
使用 Base64 图片
bash
# 先将图片转换为 base64
BASE64_IMAGE=$(base64 -i image.jpg)
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d "{
\"model\": \"gpt-4o\",
\"messages\": [
{
\"role\": \"user\",
\"content\": [
{\"type\": \"text\", \"text\": \"描述这张图片\"},
{
\"type\": \"image_url\",
\"image_url\": {\"url\": \"data:image/jpeg;base64,${BASE64_IMAGE}\"}
}
]
}
],
\"max_tokens\": 300
}"1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
函数调用
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "北京今天天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}'1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
使用不同模型
Claude 模型
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "claude-sonnet-4-5-20250929",
"messages": [{"role": "user", "content": "你好!"}]
}'1
2
3
4
5
6
7
2
3
4
5
6
7
Gemini 模型
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gemini-2.5-flash",
"messages": [{"role": "user", "content": "你好!"}]
}'1
2
3
4
5
6
7
2
3
4
5
6
7
Grok 模型
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "grok-4",
"messages": [{"role": "user", "content": "你好!"}]
}'1
2
3
4
5
6
7
2
3
4
5
6
7
Shell 脚本示例
简单的聊天脚本
bash
#!/bin/bash
API_KEY="sk-你的密钥"
API_URL="https://www.51api.org/v1/chat/completions"
chat() {
local message="$1"
local model="${2:-gpt-4o}"
curl -s "$API_URL" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "{
\"model\": \"$model\",
\"messages\": [{\"role\": \"user\", \"content\": \"$message\"}]
}" | jq -r '.choices[0].message.content'
}
# 使用示例
echo "AI: $(chat '你好!')"
echo "AI: $(chat '1+1等于几?' 'gemini-2.5-flash')"1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
交互式聊天脚本
bash
#!/bin/bash
API_KEY="sk-你的密钥"
API_URL="https://www.51api.org/v1/chat/completions"
echo "51API 聊天助手 (输入 'quit' 退出)"
echo "================================"
while true; do
read -p "你: " user_input
if [ "$user_input" = "quit" ]; then
echo "再见!"
break
fi
response=$(curl -s "$API_URL" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "{
\"model\": \"gpt-4o\",
\"messages\": [{\"role\": \"user\", \"content\": \"$user_input\"}]
}" | jq -r '.choices[0].message.content')
echo "AI: $response"
echo ""
done1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
调试技巧
查看完整响应
添加 -v 参数查看详细信息:
bash
curl -v https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "你好"}]}'1
2
3
4
2
3
4
格式化 JSON 输出
使用 jq 工具格式化输出:
bash
curl -s https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "你好"}]}' \
| jq .1
2
3
4
5
2
3
4
5
只提取回复内容
bash
curl -s https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "你好"}]}' \
| jq -r '.choices[0].message.content'1
2
3
4
5
2
3
4
5
查看 Token 消耗
bash
curl -s https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "你好"}]}' \
| jq '.usage'1
2
3
4
5
2
3
4
5
常见错误
404 Not Found
bash
# ❌ 错误:缺少 /v1
curl https://www.51api.org/chat/completions ...
# ✅ 正确:包含 /v1
curl https://www.51api.org/v1/chat/completions ...1
2
3
4
5
2
3
4
5
401 Unauthorized
bash
# ❌ 错误:API Key 格式错误
-H "Authorization: sk-xxx"
# ✅ 正确:需要 Bearer 前缀
-H "Authorization: Bearer sk-xxx"1
2
3
4
5
2
3
4
5
JSON 解析错误
bash
# ❌ 错误:使用单引号包裹 JSON 中的字符串
-d "{'model': 'gpt-4o'}"
# ✅ 正确:使用双引号
-d '{"model": "gpt-4o"}'1
2
3
4
5
2
3
4
5
