Making Your First API Request
Setting Up Your API Key
Configure your API key as an environment variable. This approach streamlines your API usage by eliminating the need to include your API key in each request. Moreover, it enhances security by minimizing the risk of inadvertently including your API key in your codebase.
In your terminal of choice:
bash
export MAGISTERIUM_API_KEY=<your-api-key-here>Or, in your project’s .env file:
bash
MAGISTERIUM_API_KEY=<your-api-key-here>Replace <your-api-key-here> with your actual API key obtained from the API Console.
Making Your First Request
Execute this curl command in the terminal of your choice:
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"])