Skip to main content

Overview

The Sonar API provides web-grounded chat completions using Perplexity’s Sonar models through an OpenAI-compatible interface. You can use it with OpenAI-compatible client libraries or our native SDKs for type safety and enhanced features. The Sonar API is now in maintenance mode. For new integrations we recommend the Agent API as the default. It covers the same web-grounded use cases (web search, streaming, and structured outputs) and adds third-party models, presets, and tools. When you are ready to move, see the migration guide.
Keep using your existing OpenAI SDKs to get started fast; switch to our native SDKs later as needed.

Pricing

Pay-as-you-go pricing for all APIs. No subscription required.

Installation

Install the SDK for your preferred language:

Authentication

Set your API key as an environment variable. The SDK will automatically read it:
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 Agent API streaming guide.

Response Structure

Sonar API responses follow an OpenAI-compatible format:

Next Steps

Agent API

Need structured outputs or third-party models? Check out the Agent API.

Search API

Get raw search results with the Search API.

Sonar API Features

Complete guide to the Sonar API with advanced features and examples.

Models

Explore available Sonar models and their capabilities.

API Reference

View complete endpoint documentation and parameters.

Search Filters

Learn how to control search behavior with filters and parameters.
Need help? Check out our community for support and discussions with other developers.