以下是使用 Python 和 Node.js 调用 OpenClaw API 的示例代码。
Python 示例
import requests
api_url = "https://your-platform.com/api/proxy/v1/chat/completions"
api_key = "your-api-key-here"
response = requests.post(
api_url,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"model": "qwen-plus",
"messages": [
{"role": "user", "content": "你好,请介绍一下自己"}
]
}
)
result = response.json()
print(result["choices"][0]["message"]["content"])
Node.js 示例
const axios = require('axios');
const apiUrl = 'https://your-platform.com/api/proxy/v1/chat/completions';
const apiKey = 'your-api-key-here';
async function chat(message) {
const response = await axios.post(apiUrl, {
model: 'qwen-plus',
messages: [
{ role: 'user', content: message }
]
}, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
return response.data.choices[0].message.content;
}
chat('你好,请介绍一下自己').then(console.log);
cURL 示例
curl -X POST "https://your-platform.com/api/proxy/v1/chat/completions" \
-H "Authorization: Bearer your-api-key-here" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages": [{"role": "user", "content": "你好"}]
}'