发起您的第一个 API 请求
设置您的 API 密钥
将您的 API 密钥配置为环境变量。这种方法通过消除在每个请求中包含您的 API 密钥的需要来简化 API 使用。此外,它通过减少意外将 API 密钥包含在代码库中的风险来增强安全性。
在您选择的终端中:
bash
export MAGISTERIUM_API_KEY=<your-api-key-here>或者,在您项目的 .env 文件中:
bash
MAGISTERIUM_API_KEY=<your-api-key-here>将 <your-api-key-here> 替换为您从 API 控制台 获得的实际 API 密钥。
发出您的首次请求
在您选择的终端中执行以下 curl 命令:
bash
curl -X POST https://www.magisterium.com/api/v1/chat/completions \
-H "Authorization: Bearer $MAGISTERIUM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "magisterium-1",
"messages": [
{
"role": "user",
"content": "What is the Magisterium?"
}
]
}'typescript
// npm install magisterium
import Magisterium from "magisterium";
const magisterium = new Magisterium({
apiKey: process.env.MAGISTERIUM_API_KEY,
});
export async function getMagisteriumAnswer() {
const results = await magisterium.chat.completions.create({
model: "magisterium-1",
messages: [
{
role: "user",
content: "What is the Magisterium?",
},
]
});
// Handle the response
console.log(results.choices[0].message);
}python
import requests
import os
api_key = os.getenv("MAGISTERIUM_API_KEY")
url = "https://www.magisterium.com/api/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": "magisterium-1",
"messages": [
{
"role": "user",
"content": "What is the Magisterium?",
}
],
"stream": False
}
chat_completion = requests.post(url, headers=headers, json=data)
print(chat_completion.json()["choices"][0]["message"])