進行您的第一次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"])