Registration
- Register your credit card to get started
This step will not charge your credit card. It just stores payment information for later API usage.
Generate an API key
- Every API call needs a valid API key
The API key is a long-lived access token that can be used until it is manually refreshed or deleted.
Send the API key as a bearer token in the Authorization header with each API request.
When you run out of credits, your API keys will be blocked until you add to your credit balance. You can avoid this by configuring “Automatic Top Up”, which refreshes your balance whenever you drop below $2.
Make your API call
- The API is conveniently OpenAI client-compatible for easy integration with existing applications.
curl --location 'https://api.perplexity.ai/chat/completions' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header 'Authorization: Bearer {API_KEY}' \
--data '{
"model": "llama-3.1-sonar-small-128k-online",
"messages": [
{
"role": "system",
"content": "Be precise and concise."
},
{
"role": "user",
"content": "How many stars are there in our galaxy?"
}
]
}'
from openai import OpenAI
YOUR_API_KEY = "INSERT API KEY HERE"
messages = [
{
"role": "system",
"content": (
"You are an artificial intelligence assistant and you need to "
"engage in a helpful, detailed, polite conversation with a user."
),
},
{
"role": "user",
"content": (
"How many stars are in the universe?"
),
},
]
client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")
response = client.chat.completions.create(
model="llama-3.1-sonar-large-128k-online",
messages=messages,
)
print(response)
response_stream = client.chat.completions.create(
model="llama-3.1-sonar-large-128k-online",
messages=messages,
stream=True,
)
for response in response_stream:
print(response)