Skip to main content

Overview

Perplexity’s Sonar 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 Sonar 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.
Keep using your existing OpenAI SDKs to get started fast; switch to our native SDKs later as needed.

Installation

Install the SDK for your preferred language:
pip install perplexityai

Authentication

Set your API key as an environment variable. The SDK will automatically read it:
export PERPLEXITY_API_KEY="your_api_key_here"
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

from perplexity import Perplexity

client = Perplexity()

completion = client.chat.completions.create(
    model="sonar-pro",
    messages=[
        {"role": "user", "content": "What were the results of the 2025 French Open Finals?"}
    ]
)

print(completion.choices[0].message.content)

Streaming Response

from perplexity import Perplexity

client = Perplexity()

stream = client.chat.completions.create(
    model="sonar-pro",
    messages=[
        {"role": "user", "content": "What are the most popular open-source alternatives to OpenAI's GPT models?"}
    ],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
For a full guide on streaming, including parsing, error handling, citation management, and best practices, see our streaming guide.

Response Structure

Sonar API responses follow an OpenAI-compatible format:
{
    "id": "pplx-1234567890",
    "model": "sonar-pro",
    "created": 1234567890,
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "The 2025 French Open Finals results..."
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 12,
        "completion_tokens": 315,
        "total_tokens": 327
    }
}

Next Steps

Need help? Check out our community for support and discussions with other developers.