Independent, unofficial guide — not affiliated with or endorsed by Perplexity AI, Inc. The official site is perplexity.ai.
Perplexity AI Open perplexity.ai ↗
Developer guide

Perplexity API: Getting Started with Sonar

Understand API access and complete a first request.

The Perplexity API lets you add Perplexity's real-time, cited web search to your own apps. Sonar now runs through the Agent API: create an API key, add credits, and send one request to get an answer with sources.

Sonar has moved to the Agent API

Sonar Chat Completions (the old /chat/completions endpoint with model: "sonar") stopped being supported on 27 September 2026. New projects should use the Agent API shown in this guide. If you have older code, see Migrating from Sonar Chat Completions.

Understand Perplexity API access

Before you write any code, here is what you need and how the Perplexity API is organized.

API key

Create a key in the Perplexity API Platform. It identifies your account on every request, so keep it secret and use it only in server-side code.

Credits and billing

The API is pay as you go with prepaid credits, separate from Perplexity Pro or Max. Cost depends on the effort level, tokens used and any tool calls.

Agent API

The main endpoint for search-grounded answers: POST https://api.perplexity.ai/v1/agent. This is where Sonar now lives.

Search API

Need ranked web results without a written answer? The Search API returns raw results you can process yourself.

Old Sonar models and their Agent API effort levels
If you usedUse effort levelBest for
SonarfastSingle facts, definitions and quick summaries
Sonar ProfastEveryday research questions with current information
Sonar Reasoning ProlowMulti-step questions across many sources
Sonar Deep ResearchhighExpert-level reasoning and exhaustive source coverage

Complete your first Perplexity API request

Five steps from no account to a cited answer in your terminal.

  1. Create an API key and add credits

    Sign in to the Perplexity API Platform, add a payment method, buy a small amount of credit and create a new API key. Copy the key somewhere safe, as you will need it in the next step.

    • Never paste your key into front-end code or a public repository.
    • Create separate keys for testing and production so you can revoke one without breaking the other.
  2. Save your key as an environment variable

    The official SDKs read the key from PERPLEXITY_API_KEY automatically, so your code never contains it.

    export PERPLEXITY_API_KEY="your-api-key-here"
  3. Install the SDK

    Use the official SDK for Python or TypeScript, or skip this step and call the API with cURL.

    pip install perplexityai
  4. Send your first request

    Ask a question with the fast effort level, the replacement for Sonar. Pick your language and run the code.

    curl https://api.perplexity.ai/v1/agent \
      -H "Authorization: Bearer $PERPLEXITY_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "effort": "fast",
        "input": "What is the tallest mountain in Africa?"
      }'

    These examples follow the request format in Perplexity's official Sonar-to-Agent-API migration guide.

  5. Read the answer and its sources

    output_text gives you the answer as plain text. The full response also has an output list: a search_results item with the sources, and a message item with the answer.

    Simplified example response
    {
      "status": "completed",
      "output": [
        {
          "type": "search_results",
          "results": [
            {
              "title": "Mount Kilimanjaro",
              "url": "https://example.com/kilimanjaro",
              "snippet": "Kilimanjaro is Africa's highest mountain...",
              "date": "2026-03-14"
            }
          ]
        },
        {
          "type": "message",
          "content": [
            { "type": "output_text", "text": "Mount Kilimanjaro in Tanzania is the tallest mountain in Africa..." }
          ]
        }
      ]
    }

    To list the sources in Python:

    Python
    for item in response.output:
        if item.type == "search_results":
            for result in item.results:
                print(result.title, result.url)

Migrating from Sonar Chat Completions

If your code still calls chat.completions with a Sonar model, swap it for responses.create and pick an effort level. Your question goes in input instead of a messages list.

Before
# Retired: Sonar Chat Completions
completion = client.chat.completions.create(
    model="sonar",
    messages=[{"role": "user", "content": "Your question"}],
)
print(completion.choices[0].message.content)
After
# Now: Agent API
response = client.responses.create(
    effort="fast",
    input="Your question",
)
print(response.output_text)

Asynchronous Sonar requests are no longer supported; Perplexity recommends background mode in the Agent API instead.

Perplexity API FAQ

Is the Perplexity API free?

No. The Perplexity API is pay as you go: you add prepaid credits and are billed for what you use. It is separate from the Perplexity Pro and Max apps, and since February 2026 a Pro subscription no longer includes monthly API credit.

Is the Sonar API still available?

Sonar Chat Completions support ended on 27 September 2026. Sonar's search-grounded answers now run through the Agent API, where the old Sonar and Sonar Pro models map to the fast effort level.

Which effort level should I use?

Start with fast for quick, everyday questions. Use low for multi-step questions that need several sources, and high when you need exhaustive, research-grade coverage.

Does the Perplexity API return citations?

Yes. Each response includes a search_results item listing the sources used, with a title, URL, snippet and date for each one.

Can I call the Perplexity API from a website's front end?

Not directly. Your API key must stay secret, so send requests from your own server or a serverless function and pass only the answer to the browser.