Quickstart
This guide will help you make your first request to the CompactifAI API in minutes.
Prerequisites
Section titled “Prerequisites”Before you begin, you’ll need:
- A CompactifAI API key. Please see our authentication guide for more information.
- We expose a REST API, so you can use any language that can make HTTP requests. We provide examples in cURL, Python, and JavaScript but feel free to use whatever you’re comfortable with.
Step 1: Authentication
Section titled “Step 1: Authentication”All requests to the CompactifAI API require an API key for authentication. If you don’t have an API key yet, see our authentication guide.
Include your API key in the header of all requests:
Authorization: Bearer YOUR_API_KEY
Step 2: Make your first API request
Section titled “Step 2: Make your first API request”Let’s make a simple request to the Chat Completions API:
curl https://api.compactif.ai/v1/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer YOUR_API_KEY" \-d '{ "model": "cai-llama-3-1-8b-slim", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello, how are you?"} ]}'
import requests
api_key = "YOUR_API_KEY"url = "https://api.compactif.ai/v1/chat/completions"
headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
data = { "model": "cai-llama-3-1-8b-slim", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello, how are you?"} ]}
response = requests.post(url, headers=headers, json=data)print(response.json())
async function callCompactifAI() {const response = await fetch('https://api.compactif.ai/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ model: 'cai-llama-3-1-8b-slim', messages: [ {role: 'system', content: 'You are a helpful assistant.'}, {role: 'user', content: 'Hello, how are you?'} ] })});
const data = await response.json();console.log(data);}
callCompactifAI();
Step 3: Understanding the response
Section titled “Step 3: Understanding the response”The API will return a response like this:
{"id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7","object": "chat.completion","created": 1749600000,"model": "cai-llama-3-1-8b-slim","choices": [ { "message": { "role": "assistant", "content": "Hello! I'm doing well, thank you for asking. I'm an AI assistant created by CompactifAI. How can I help you today?" }, "finish_reason": "stop", "index": 0 }],"usage": { "prompt_tokens": 23, "completion_tokens": 28, "total_tokens": 51}}
Next Steps
Section titled “Next Steps”Now that you’ve made your first API call, you can:
- Check out available Models
- Learn more about the Chat Completions endpoint
- Explore the Completions endpoint
- Read our API Reference for detailed information about all endpoints