快速开始
本页用一个非流式文本请求完成接入验证。示例只使用占位符,不会调用真实接口。
1. 保存令牌
建议把令牌保存为环境变量 YUNIVERSITY_API_KEY,不要写进源码或提交到代码仓库。
PowerShell:
$env:YUNIVERSITY_API_KEY = "<YOUR_API_KEY>"
Linux/macOS:
export YUNIVERSITY_API_KEY="<YOUR_API_KEY>"
2. 查看可用模型
curl https://api.yuniversity.cc/v1/models \
-H "Authorization: Bearer $YUNIVERSITY_API_KEY"
响应中的 data[].id 是可用于请求的模型标识。以下示例使用 gpt-5.6-terra;如果它不在返回列表中,请替换为列表中的模型。
3. 发起第一次文本请求
PowerShell:
$body = @{
model = "gpt-5.6-terra"
messages = @(@{ role = "user"; content = "Reply with OK" })
} | ConvertTo-Json -Depth 4
Invoke-RestMethod https://api.yuniversity.cc/v1/chat/completions -Method Post `
-Headers @{ Authorization = "Bearer $env:YUNIVERSITY_API_KEY" } `
-ContentType "application/json" -Body $body
Linux/macOS:
curl https://api.yuniversity.cc/v1/chat/completions \
-H "Authorization: Bearer $YUNIVERSITY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-5.6-terra","messages":[{"role":"user","content":"Reply with OK"}]}'
Python(标准库):
import json
import os
from urllib.request import Request, urlopen
payload = {"model": "gpt-5.6-terra", "messages": [{"role": "user", "content": "Reply with OK"}]}
request = Request(
"https://api.yuniversity.cc/v1/chat/completions",
data=json.dumps(payload).encode(),
headers={"Authorization": f"Bearer {os.environ['YUNIVERSITY_API_KEY']}", "Content-Type": "application/json"},
)
with urlopen(request) as response:
print(json.load(response))
JavaScript(Node.js 18+ 或浏览器环境):
const response = await fetch("https://api.yuniversity.cc/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.YUNIVERSITY_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-5.6-terra",
messages: [{ role: "user", content: "Reply with OK" }],
}),
});
console.log(await response.json());
成功后通常可从响应的 choices[0].message.content 读取文本。若模型不在 /v1/models 列表中,请先改用已列出的模型;令牌或权限问题请参阅身份认证。