How to Use Perplexity API




Perplexity developer tutorial · Updated November 2026

How to Use the Perplexity API: Step-by-Step Guide

Create an API key, store it safely, install the official Python SDK and make a first Agent API or Search API request. This walkthrough also explains billing, API choices and common setup errors.

Follow the setup stepsSee the code examples

Quick path: Sign in to the Perplexity API Console, create or select a project, configure API billing, generate a key and save it securely. Set the key as an environment variable, install the SDK, then send a test request. API usage is billed separately from consumer subscriptions.
First API request workflow
API Console → project → API key → environment variable → SDK request → inspect response and usage

What you need before using the Perplexity API

  • Access to the Perplexity API Console.
  • An API project and a generated API key.
  • API billing or credits configured for the API you plan to call.
  • Python installed for the examples below. Perplexity also provides a TypeScript SDK.
  • A terminal and code editor. Avoid putting keys in browser-side code.

The API Platform is separate from consumer Perplexity plans. A Pro, Max or other web subscription does not replace API billing. For an overview of the API types, read What Is Perplexity API?

How to set up a Perplexity API account

1Open the API Platform and sign in. Use the official Perplexity API Platform and open the API Console. Console labels may change.

2Create or select a project. Create a project for your integration if prompted. A clear name such as “API testing” can help you distinguish development from production usage.

3Configure API billing. Follow the current Console billing instructions and review pricing for the API you intend to use. API Platform usage is pay-as-you-go; access and billing requirements can vary by endpoint.

4Generate an API key. In the project’s API Keys area, generate a key and copy it before leaving the page. The full key is shown only once. Store it in a password manager or secret store.

5Choose an API for the output you need. Agent API is for model-generated responses and can use tools such as web search. Search API returns ranked web results for your application to process. Other endpoints serve other developer tasks.

6Install the SDK. In a terminal, install the official Python package:

pip install perplexityai

For TypeScript, the official package is @perplexity-ai/perplexity_ai. Follow the current docs for the installation and supported versions.

Set your API key as an environment variable

Use the terminal where you will run your Python script. Replace the placeholder with your key locally; never paste the real value into a public script, article or screenshot.

Windows PowerShell:

$env:PERPLEXITY_API_KEY="your_api_key_here"

macOS or Linux:

export PERPLEXITY_API_KEY="your_api_key_here"

These commands set the key for the current shell session. If you open a new terminal, you may need to set it again or use an appropriate secure environment configuration.

Make your first Perplexity API request

Choose the example that matches the response you want. The SDK reads PERPLEXITY_API_KEY from the environment. These examples follow the current Python SDK pattern; they are for local testing and have not been run against a live account here.

Generated answer

Example 1: Agent API with Python

Use Agent API when you want a model-generated response. The example requests a low-effort response for a simple explanation.

from perplexity import Perplexity

client = Perplexity()

response = client.responses.create(
    effort="low",
    input="Explain how solar panels work in simple English."
)

print(response.output_text)

Save the code in a file such as perplexity_test.py and run it from the same terminal where the environment variable is set:

python perplexity_test.py

What it does: Perplexity() initializes the client using your environment key. client.responses.create(...) sends an Agent API request. response.output_text gives you a convenient text view of the response. The request can incur API charges; check live pricing and your project’s billing first.

Web search is opt-in in the Agent API. For web-grounded answers, use the currently documented effort or web_search tool settings and inspect the response for citations.

Ranked results

Example 2: Search API with Python

Use Search API when your application needs ranked web results and will decide how to filter, display or summarize them.

from perplexity import Perplexity

client = Perplexity()

search = client.search.create(
    query="renewable energy technologies",
    max_results=5
)

for result in search.results:
    print(f"{result.title}: {result.url}")

Search API returns result objects such as titles, URLs and relevant snippets. It does not write a complete AI answer. Some fields, such as dates or snippet text, may be absent or limited, so handle them safely in your application.

Understand the API response

Agent API responses have structured output. The SDK’s output_text property aggregates text content for convenience, while other response fields can include status, model and usage details. Use the documented response schema rather than assuming every response is a plain string.

Search API returns result objects. A snippet is query-relevant extracted text, not necessarily the full source page. Open original sources to verify important claims. Our guide explains how Perplexity citations work.

Perplexity API pricing and usage

Perplexity API Platform use is pay-as-you-go and separate from consumer subscriptions. Pricing depends on the API and model: Agent API can include model-token charges and tool fees, while Search API uses request-based pricing. Rates and billing details can change.

  • Review the official pricing page before sending requests.
  • Check the Console for credits, payment setup, usage and project billing.
  • Estimate both model usage and tool calls when using Agent API.
  • Set monitoring or usage controls before a high-volume integration.

For an overview of Search API costs, see our Perplexity Search API pricing guide. Consumer plan details are covered separately in our Perplexity subscription pricing guide.

Perplexity API key security basics

  • Store keys in environment variables or a dedicated secret manager.
  • Call the API from a protected backend, never from public browser code.
  • Keep keys out of screenshots, source control, shared messages and logs.
  • Use separate keys or projects for testing and production when practical.
  • Monitor usage and revoke or rotate a key if it is exposed or no longer needed.
If a key is exposed: Revoke it in the API Console, create a replacement and review project activity for usage you do not recognize. Do not paste the secret into a support request.

Using older Perplexity API tutorials?

Some older examples use a legacy Sonar endpoint, previous model names or a chat-completions pattern. The current documentation describes Agent API and Search API workflows. If you maintain an older integration, check Perplexity’s current migration guide before changing endpoints; do not assume old code, model IDs or prices remain supported.

Common Perplexity API errors and fixes

Issue What to check
Authentication error Confirm the key is set in the same terminal or runtime that runs the script. Do not print the secret while debugging.
Billing or credit error Review Console billing, available credits and project settings. A consumer subscription does not confirm API billing.
Rate limit response Check your current usage tier and limits. Reduce request frequency and follow the current retry guidance.
Unsupported parameter or model Compare your request with the latest endpoint reference. Older tutorials may use changed or retired options.
SDK import error Install the package in the same Python environment or virtual environment used to run your script.
Key exposed accidentally Revoke it, create a replacement and check recent project usage.

Frequently asked questions

How do I use the Perplexity API?

Open the API Console, configure billing, create a project and key, store the key securely, install the SDK and send a request using the API that fits your task.

How do I get a Perplexity API key?

Sign in to the API Console, create or select a project, open API Keys, generate a key and copy it securely. The full key may be shown only once.

Can I use the Perplexity API with Python?

Yes. Install the official Python package with pip install perplexityai and follow the current Perplexity Quickstart.

Can I use Perplexity API with JavaScript or TypeScript?

Yes. Perplexity provides a TypeScript SDK. Follow the current official docs for package installation and examples.

Which Perplexity API should I use first?

Use Agent API when you need a model-generated response, optionally with tools. Use Search API when you need ranked results that your own code will process.

Do I need Perplexity Pro to use the API?

No. Consumer subscriptions and API Platform usage are separate. API access follows its own billing and account requirements.

Can one API key work across Perplexity APIs?

A project key can be used for supported APIs, but access and billing requirements may differ by service. Check the current documentation and Console.

Why is my Perplexity API request not working?

Common causes include an invalid key, missing billing setup, rate limits, unsupported parameters or an outdated SDK. Review the error and current official documentation.

About this tutorial: PerplexityWeb is independent and is not affiliated with Perplexity. Code patterns follow the current official documentation but have not been run against a live account. API details, prices and requirements can change; confirm them before deployment.

Continue learning about Perplexity APIs

Read our API overview or review Search API pricing before planning an integration.

What Is Perplexity API?Search API pricing guide

Official Perplexity API resources

Use these official references for current setup, code, security and billing details.


Scroll to Top