Skip to main content

Overview

The Perplexity API provides three core APIs for different use cases: Chat Completions for web-grounded AI responses with Sonar models, Agentic Research for accessing OpenAI, Anthropic, Google, and xAI models with unified search tools and transparent pricing, and Search for ranked web search results. All APIs support both REST and SDK access with streaming, filtering, and advanced controls.

Available APIs

Choosing the Right API

  • You want Perplexity’s Sonar models optimized for research and Q&A
  • You need built-in citations and conversation context
  • You prefer simplicity—just send a message and get a researched answer
Best for: AI assistants, research tools, Q&A applications
  • You need multi-provider access to OpenAI, Anthropic, Google, and more models through one API
  • You want granular control over model selection, reasoning, token budgets, and tools
  • You want presets for common use configurations or full customization for advanced workflows
Best for: Agentic workflows, custom AI applications, multi-model experimentation
  • You need raw search results without LLM processing
  • You want to build custom AI workflows with your own models
  • You need search data for indexing, analysis, or training
Best for: Custom AI pipelines, data collection, search integration

Generating an API Key

Get your Perplexity API Key

Navigate to the API Keys tab in the API Portal and generate a new key.
See the API Groups page to set up an API group.

Installation

Install the SDK for your preferred language:
pip install perplexityai

Authentication

Set your API key as an environment variable:
export PERPLEXITY_API_KEY="your_api_key_here"
Or use a .env file in your project:
.env
PERPLEXITY_API_KEY=your_api_key_here
OpenAI SDK Compatible: Perplexity’s API supports the OpenAI Chat Completions format. You can use OpenAI client libraries by pointing to our endpoint. See our OpenAI Compatibility Guide for examples.

Making Your First API Call

Choose your API based on your use case:
Use for third-party models with web search tools and presets:
from perplexity import Perplexity

# Initialize the client (uses PERPLEXITY_API_KEY environment variable)
client = Perplexity()

# Make the API call with a preset
response = client.responses.create(
    preset="pro-search",
    input="What are the latest developments in AI?"
)

# Print the AI's response
print(response.output_text)
The response includes structured output with tool usage and citations:
{
  "id": "resp_1234567890",
  "object": "response",
  "created_at": 1756485272,
  "model": "openai/gpt-5.1",
  "status": "completed",
  "output": [
    {
      "type": "message",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "Recent developments in AI include...",
          "annotations": [
            {
              "type": "citation",
              "url": "https://example.com/article1"
            }
          ]
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 20,
    "output_tokens": 250,
    "total_tokens": 270
  }
}

Streaming Responses

Enable streaming for real-time output with either API:
from perplexity import Perplexity

client = Perplexity()

# Make the streaming API call
stream = client.responses.create(
    preset="pro-search",
    input="Explain quantum computing",
    stream=True
)

# Process the streaming response
for chunk in stream:
    if chunk.type == "response.output_text.delta":
        print(chunk.delta, end="", flush=True)
For a full guide on streaming, including parsing, error handling, citation management, and best practices, see our streaming guide.

Next Steps

Now that you’ve made your first API call, explore each API in depth:
Need help? Check out our community for support and discussions with other developers.