API key
Create a key in the Perplexity API Platform. It identifies your account on every request, so keep it secret and use it only in server-side code.
Understand API access and complete a first request.
The Perplexity API lets you add Perplexity's real-time, cited web search to your own apps. Sonar now runs through the Agent API: create an API key, add credits, and send one request to get an answer with sources.
Sonar Chat Completions (the old /chat/completions endpoint with model: "sonar") stopped being supported on 27 September 2026. New projects should use the Agent API shown in this guide. If you have older code, see Migrating from Sonar Chat Completions.
Before you write any code, here is what you need and how the Perplexity API is organized.
Create a key in the Perplexity API Platform. It identifies your account on every request, so keep it secret and use it only in server-side code.
The API is pay as you go with prepaid credits, separate from Perplexity Pro or Max. Cost depends on the effort level, tokens used and any tool calls.
The main endpoint for search-grounded answers: POST https://api.perplexity.ai/v1/agent. This is where Sonar now lives.
Need ranked web results without a written answer? The Search API returns raw results you can process yourself.
| If you used | Use effort level | Best for |
|---|---|---|
| Sonar | fast | Single facts, definitions and quick summaries |
| Sonar Pro | fast | Everyday research questions with current information |
| Sonar Reasoning Pro | low | Multi-step questions across many sources |
| Sonar Deep Research | high | Expert-level reasoning and exhaustive source coverage |
Five steps from no account to a cited answer in your terminal.
Sign in to the Perplexity API Platform, add a payment method, buy a small amount of credit and create a new API key. Copy the key somewhere safe, as you will need it in the next step.
The official SDKs read the key from PERPLEXITY_API_KEY automatically, so your code never contains it.
export PERPLEXITY_API_KEY="your-api-key-here"$env:PERPLEXITY_API_KEY="your-api-key-here"Use the official SDK for Python or TypeScript, or skip this step and call the API with cURL.
pip install perplexityainpm install @perplexity-ai/perplexity_aiAsk a question with the fast effort level, the replacement for Sonar. Pick your language and run the code.
curl https://api.perplexity.ai/v1/agent \
-H "Authorization: Bearer $PERPLEXITY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"effort": "fast",
"input": "What is the tallest mountain in Africa?"
}'from perplexity import Perplexity
client = Perplexity() # reads PERPLEXITY_API_KEY from your environment
response = client.responses.create(
effort="fast",
input="What is the tallest mountain in Africa?",
)
print(response.output_text)import Perplexity from '@perplexity-ai/perplexity_ai';
const client = new Perplexity(); // reads PERPLEXITY_API_KEY from your environment
const response = await client.responses.create({
effort: 'fast',
input: 'What is the tallest mountain in Africa?',
});
console.log(response.output_text);These examples follow the request format in Perplexity's official Sonar-to-Agent-API migration guide.
output_text gives you the answer as plain text. The full response also has an output list: a search_results item with the sources, and a message item with the answer.
{
"status": "completed",
"output": [
{
"type": "search_results",
"results": [
{
"title": "Mount Kilimanjaro",
"url": "https://example.com/kilimanjaro",
"snippet": "Kilimanjaro is Africa's highest mountain...",
"date": "2026-03-14"
}
]
},
{
"type": "message",
"content": [
{ "type": "output_text", "text": "Mount Kilimanjaro in Tanzania is the tallest mountain in Africa..." }
]
}
]
}To list the sources in Python:
for item in response.output:
if item.type == "search_results":
for result in item.results:
print(result.title, result.url)If your code still calls chat.completions with a Sonar model, swap it for responses.create and pick an effort level. Your question goes in input instead of a messages list.
# Retired: Sonar Chat Completions
completion = client.chat.completions.create(
model="sonar",
messages=[{"role": "user", "content": "Your question"}],
)
print(completion.choices[0].message.content)# Now: Agent API
response = client.responses.create(
effort="fast",
input="Your question",
)
print(response.output_text)Asynchronous Sonar requests are no longer supported; Perplexity recommends background mode in the Agent API instead.
No. The Perplexity API is pay as you go: you add prepaid credits and are billed for what you use. It is separate from the Perplexity Pro and Max apps, and since February 2026 a Pro subscription no longer includes monthly API credit.
Sonar Chat Completions support ended on 27 September 2026. Sonar's search-grounded answers now run through the Agent API, where the old Sonar and Sonar Pro models map to the fast effort level.
Start with fast for quick, everyday questions. Use low for multi-step questions that need several sources, and high when you need exhaustive, research-grade coverage.
Yes. Each response includes a search_results item listing the sources used, with a title, URL, snippet and date for each one.
Not directly. Your API key must stay secret, so send requests from your own server or a serverless function and pass only the answer to the browser.