Documentation

Getting Started

Quickstart

A4F provides a unified API that gives you access to hundreds of AI models through a single endpoint. This allows you to switch between models and providers without changing your code, while automatically handling fallbacks and selecting the most cost-effective options. Get started with just a few lines of code using your preferred SDK or framework.

Using the OpenAI SDK

In the examples below, the A4F-specific headers are optional. Setting them allows your app to appear on the A4F leaderboards.

from openai import OpenAI
client = OpenAI(
base_url="https://api.a4f.co/v1",
api_key="YOUR_A4F_API_KEY",
)
completion = client.chat.completions.create(
model="provider-1/chatgpt-4o-latest",
messages=[
{
"role": "user",
"content": "What is the meaning of life?",
},
],
)
print(completion.choices[0].message.content)

Using the A4F API directly

import requests
import json
response = requests.post(
url="https://api.a4f.co/v1/chat/completions",
headers={
"Authorization": "Bearer YOUR_A4F_API_KEY",
},
data=json.dumps({
"model": "provider-1/chatgpt-4o-latest",
"messages": [
{"role": "user", "content": "What is the meaning of life?"}
]
})
)
print(response.json()['choices'][0]['message']['content'])

The API also supports streaming.

Using third-party SDKs

For information about using third-party SDKs and frameworks with A4F, please see our frameworks documentation.

Was this page helpful?