cURL
使用 cURL 命令行直接调用 WLON API,适合快速测试和脚本集成。
前提条件:已安装 cURL(大多数系统自带),已在 控制台 创建 API Key。
基本请求
发送一个 Chat Completions 请求到 /v1/chat/completions:
curl https://api2everything.xyz/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": "你好!"}
]
}'
请求头说明
| Header | 值 | 说明 |
|---|---|---|
Authorization | Bearer sk-your-api-key | 认证密钥,必须 |
Content-Type | application/json | 请求体格式,必须 |
JSON 请求体
完整参数示例:
curl https://api2everything.xyz/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个翻译助手。"},
{"role": "user", "content": "翻译成英文:今天天气真好"}
],
"temperature": 0.7,
"max_tokens": 2048,
"top_p": 1.0
}'
流式输出
在请求体中添加 "stream": true,并使用 -N 禁用缓冲,实时显示输出:
curl -N https://api2everything.xyz/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "写一首关于春天的诗"}
],
"stream": true
}'
提示:
-N(--no-buffer)参数确保流式数据实时显示,不会被缓冲。使用 jq 解析响应
使用 jq 从 JSON 响应中提取内容:
# 提取回复文本
curl -s https://api2everything.xyz/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "1+1=?"}]
}' | jq -r '.choices[0].message.content'
# 提取 token 用量信息
curl -s https://api2everything.xyz/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "你好"}]
}' | jq '.usage'
查询可用模型
curl https://api2everything.xyz/v1/models \
-H "Authorization: Bearer sk-your-api-key" | jq '.data[].id'
使用环境变量
将 API Key 存入环境变量,简化命令:
# 设置环境变量
export OPENAI_API_KEY="sk-your-api-key"
# 之后的请求中引用变量
curl https://api2everything.xyz/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "你好!"}]
}'
安全提醒:避免在 shell 历史记录中暴露 API Key。使用
export 或从文件中读取。