Overview
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.Quick Start
Use the OpenAI SDK with Perplexity’s Sonar API:- Python
- TypeScript
Configuration
Setting Up the OpenAI SDK
Configure OpenAI SDKs to work with Perplexity by setting thebase_url to https://api.perplexity.ai:
- Python
- TypeScript
Important: Use
base_url="https://api.perplexity.ai" for the Sonar API.Basic Usage
Perplexity’s Sonar API is fully compatible with OpenAI’s Chat Completions interface.- Python
- TypeScript
Streaming
Streaming works exactly like OpenAI’s API:- Python
- TypeScript
Perplexity-Specific Parameters
Add Perplexity-specific search parameters usingextra_body (Python) or direct parameters (TypeScript):
- Python
- TypeScript
API Compatibility
Standard OpenAI Parameters
These parameters work exactly the same as OpenAI’s API:model- Model name (use Perplexity model names likesonar-pro)messages- Chat messages arraymax_tokens- Maximum tokens in responsestream- Enable streaming responsestemperature- Response randomness (0-2)top_p- Nucleus sampling parameterresponse_format- Response format specification
Perplexity-Specific Parameters
Sonar API supports additional search and response parameters:search_domain_filter- Limit or exclude specific domainssearch_recency_filter- Filter by content recency (“day”, “week”, “month”, “year”)return_images- Include image URLs in responsereturn_related_questions- Include related questionssearch_mode- “web” (default) or “academic” mode selectorenable_search_classifier- Let AI decide when to searchdisable_search- Turn off web search completely
See Sonar API Reference for complete parameter details.
Response Structure
Perplexity responses match OpenAI’s format exactly, with additional fields:Standard OpenAI Fields
choices[0].message.content- The AI-generated responsemodel- The model name usedusage- Token consumption detailsid,created,object- Standard response metadata
Perplexity-Specific Fields
search_results- Array of web sources withtitle,url, anddatecitations- Array of citation URLs referenced in the response
Best Practices
1
Use the correct base URL
Always use
https://api.perplexity.ai for the Sonar API.2
Handle errors gracefully
Use the OpenAI SDK’s error handling:
3
Use streaming for better UX
Stream responses for real-time user experience:
4
Access search results
Use the
search_results field to get accurate source URLs:Recommended: Perplexity SDK
We recommend using Perplexity’s native SDKs for the best developer experience:- Type safety - Full TypeScript/Python type definitions for all parameters
- Enhanced features - Direct access to all Perplexity-specific features
- Better error messages - Perplexity-specific error handling
- Simpler setup - No need to configure base URLs
Next Steps
Sonar Quickstart
Get started with Sonar API using OpenAI SDKs.
Sonar API Features
Learn best practices for prompting and using the Sonar API.
API Reference
View complete API documentation for the Sonar endpoint.
Search Filters
Learn how to control search behavior with filters and parameters.