Overview
Perplexity’s Chat Completions API provides web-grounded AI responses with support for streaming, tools, search options, and more. You can use it with OpenAI-compatible client libraries or our native SDKs for type safety and enhanced features. Use the Chat Completions API when you need web search capabilities built-in, streaming responses, or Perplexity’s Sonar models. For structured outputs and third-party models, use our Agentic Research API.Installation
Install the SDK for your preferred language:Authentication
Set your API key as an environment variable. The SDK will automatically read it:- macOS/Linux
- Windows
.env file in your project:
.env
All SDK examples below automatically use the
PERPLEXITY_API_KEY environment variable. You can also pass the key explicitly if needed.Generating an API Key
Get your Perplexity API Key
Navigate to the API Keys tab in the API Portal and generate a new key.
OpenAI SDK Compatible: Perplexity’s API supports the OpenAI Chat Completions format. You can use OpenAI client libraries by pointing to our endpoint.
Basic Usage
Non-Streaming Request
Streaming Response
For a full guide on streaming, including parsing, error handling, citation management, and best practices, see our streaming guide.
Response Structure
Chat Completions responses follow the OpenAI-compatible format:Next Steps
Chat Completions Guide
Complete guide to the Chat Completions API with advanced features and examples.
Models
Explore available Sonar models and their capabilities.
API Reference
View complete endpoint documentation and parameters.
Search Controls
Learn how to control search behavior with filters and parameters.
Agentic Research API
Need structured outputs or third-party models? Check out the Agentic Research API.
Search API
Get raw search results with the Search API.
Need help? Check out our community for support and discussions with other developers.