Perplexity’s Sonar API is fully compatible with OpenAI’s Chat Completions format. You can use your existing OpenAI client libraries with the Sonar API by simply changing the base URL and providing your Perplexity API key.
Endpoint Note: Perplexity’s canonical Sonar API endpoint is POST /v1/sonar. For OpenAI SDK compatibility, POST /chat/completions is also accepted as an alias — the OpenAI SDK automatically routes client.chat.completions.create() to /chat/completions, which Perplexity handles seamlessly. No SDK changes are needed beyond setting the base URL.
We recommend using the Perplexity SDK for the best experience with full type safety, enhanced features, and preset support. Use OpenAI SDKs if you’re already integrated and need drop-in compatibility.
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai")completion = client.chat.completions.create( model="sonar-pro", messages=[ {"role": "user", "content": "What breakthroughs in fusion energy have been announced this year?"} ])print(completion.choices[0].message.content)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai"});const completion = await client.chat.completions.create({ model: "sonar-pro", messages: [ { role: "user", content: "What breakthroughs in fusion energy have been announced this year?" } ]});console.log(completion.choices[0].message.content);
Perplexity’s Sonar API is fully compatible with OpenAI’s Chat Completions interface.
Python
TypeScript
Copy
Ask AI
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai")completion = client.chat.completions.create( model="sonar-pro", messages=[ {"role": "user", "content": "What breakthroughs in fusion energy have been announced this year?"} ])print(completion.choices[0].message.content)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai"});const completion = await client.chat.completions.create({ model: "sonar-pro", messages: [ { role: "user", content: "What breakthroughs in fusion energy have been announced this year?" } ]});console.log(completion.choices[0].message.content);
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai")stream = client.chat.completions.create( model="sonar-pro", messages=[ {"role": "user", "content": "What breakthroughs in fusion energy have been announced this year?"} ], stream=True)for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai"});const stream = await client.chat.completions.create({ model: "sonar-pro", messages: [ { role: "user", content: "What breakthroughs in fusion energy have been announced this year?" } ], stream: true});for await (const chunk of stream) { if (chunk.choices[0]?.delta?.content) { process.stdout.write(chunk.choices[0].delta.content); }}
Both paths accepted by Perplexity for compatibility
client.async_.chat.completions.create()
POST /v1/async/sonar
POST /async/chat/completions
Both paths accepted by Perplexity for compatibility
When using the OpenAI SDK, client.chat.completions.create() sends requests to /chat/completions. Perplexity accepts this path as an alias for /v1/sonar, so no SDK configuration changes are needed beyond base_url.