API Key Environment Variables, SDK Installation, and Bearer Authentication
How to install official SDKs, set PERPLEXITY_API_KEY safely, verify it without leaking secrets, and authenticate HTTP clients to the API platform.
This page summarizes Perplexity documentation on authenticating to the API platform: creating a key, exporting PERPLEXITY_API_KEY, installing the Python or TypeScript SDK, and using the same secret for REST calls (including Bearer headers). The same pattern appears across the Cookbook, Agent API, Search API, Embeddings API, Decisions API, CLI, and SDK overview.
Nearly every quickstart assumes you already have a key from the API Console (for example console.perplexity.ai/project/keys as referenced in cookbook examples). One key typically works across product APIs on the platform—for example, cookbook notes for the Decisions API state that one key works for both APIs involved in those recipes.
PERPLEXITY_API_KEY: what it is and when you need it
Section titled “PERPLEXITY_API_KEY: what it is and when you need it”PERPLEXITY_API_KEY is the standard environment variable name for your Perplexity API key. Official SDKs read it automatically when you construct a client without passing api_key / apiKey. The SDK overview shows the default:
client = Perplexity() # Automatically uses PERPLEXITY_API_KEYconst client = new Perplexity(); // Uses PERPLEXITY_API_KEY from .env file when loadedYou need the variable set (or the key passed explicitly) before any SDK example, CLI command, or script that calls the platform. The Examples overview lists common requirements: Python 3.9+ or Node.js 18+, a Perplexity API key, and network access for API calls.
For integrations that are not the official SDK—proxy gateways, third-party frameworks, or raw curl—you still supply the same secret, usually via environment variable or an Authorization: Bearer header.
Alternate variable names in some integrations
Section titled “Alternate variable names in some integrations”Most docs use PERPLEXITY_API_KEY exclusively. A few integrations accept a fallback:
| Variable | Where it appears | Role |
|---|---|---|
PERPLEXITY_API_KEY |
SDK, CLI, quickstarts | Primary; auto-read by official SDKs |
PPLX_API_KEY |
Mastra integration, LangGraph VC memo recipe | Fallback or alternate name for ChatPerplexity |
When you mix libraries, set the name each library documents, or pass the key in the client constructor so you are not guessing at runtime.
SDK installation: Python and TypeScript packages
Section titled “SDK installation: Python and TypeScript packages”Install one official SDK for your language before running quickstarts or cookbook recipes.
Python (perplexityai)
Section titled “Python (perplexityai)”pip install perplexityaiThe Getting started quickstart, Agent API quickstart, and SDK overview all use this package from PyPI.
TypeScript / Node.js (@perplexity-ai/perplexity_ai)
Section titled “TypeScript / Node.js (@perplexity-ai/perplexity_ai)”npm install @perplexity-ai/perplexity_aiPackage managers such as pnpm and yarn install the same package; integration guides for Cursor and Claude Code note equivalent commands.
Some recipes pin versions in requirements.txt (for example perplexityai==0.39.0 in the Model Picker example); match the pin when you run that specific project.
Set PERPLEXITY_API_KEY: export on macOS, Linux, and Windows
Section titled “Set PERPLEXITY_API_KEY: export on macOS, Linux, and Windows”Set the key in the environment for the shell or process that runs your app. Do not embed the key in source files.
macOS and Linux
Section titled “macOS and Linux”export PERPLEXITY_API_KEY="your_api_key_here"This pattern is repeated in the Search API quickstart, Embeddings quickstart, Agent API quickstart, and Perplexity CLI overview.
Windows
Section titled “Windows”setx PERPLEXITY_API_KEY "your_api_key_here"The Decisions API quickstart adds that setx applies to new terminals—you must open a new session before continuing.
Confirm the variable is set without revealing the key
Section titled “Confirm the variable is set without revealing the key”Quickstarts recommend checking presence only, not printing the value:
test -n "$PERPLEXITY_API_KEY" && echo "PERPLEXITY_API_KEY is set" || echo "PERPLEXITY_API_KEY is not set"On Windows PowerShell:
if ($env:PERPLEXITY_API_KEY) { "PERPLEXITY_API_KEY is set" } else { "PERPLEXITY_API_KEY is not set" }Never print the key or any part of it. Commands like env, echo $PERPLEXITY_API_KEY, or a prefix or length check leave the key in shell history, logs, and coding-agent transcripts. Read the key from the environment in code and tests, and don’t paste it into fixtures or commit messages.
— Perplexity, Agent API quickstart
The same warning appears in the Search API quickstart and Embeddings quickstart.
Local development: .env files and loading at startup
Section titled “Local development: .env files and loading at startup”For local work, store secrets in a .env file and keep it out of version control.
Always store API keys securely using environment variables:
Store API keys in environment variables, never in source code.
— Perplexity, SDK best practices
A typical local file (from SDK best practices):
cat > .env << 'EOF'PERPLEXITY_API_KEY=your_api_key_herePERPLEXITY_MAX_RETRIES=3PERPLEXITY_TIMEOUT=30000EOFLoad it in Python with python-dotenv or in Node with dotenv; the SDK overview shows load_dotenv() / dotenv.config() before Perplexity() with no inline key.
Integration guides for Cursor and Claude Code recommend a project-root .env, adding it to .gitignore, and loading with import "dotenv/config" in TypeScript.
Validate required variables at application startup
Section titled “Validate required variables at application startup”SDK best practices recommend failing fast if PERPLEXITY_API_KEY is missing—for example reading os.getenv("PERPLEXITY_API_KEY") and exiting with a clear error before any API call. That avoids opaque authentication errors deep in a request path.
Pass the key in code: environment variable vs explicit constructor
Section titled “Pass the key in code: environment variable vs explicit constructor”Official SDKs accept both patterns.
Preferred: read from the environment (see API key management):
Good: os.environ[“PERPLEXITY_API_KEY”]
Bad: api_key = “pplx-1234567890abcdef”
— Perplexity, API key management
In Python you may pass api_key=os.environ.get("PERPLEXITY_API_KEY"); in TypeScript, apiKey: process.env.PERPLEXITY_API_KEY. The Getting started quickstart notes you can also pass the key explicitly if needed, but environment variables remain the documented default for examples.
The Equity research brief example documents additional local options: --api-key on the command line or a .pplx_api_key file in the working directory. Those are convenience paths for a script; they still require keeping the secret out of git and shared logs.
Bearer token authentication for HTTP clients
Section titled “Bearer token authentication for HTTP clients”When you call REST endpoints without the SDK, send the same API key as a Bearer token. The Computer Analytics API documents:
curl --request GET \ --url "https://api.perplexity.ai/v1/analytics/computer/usage?dataset=credit_usage&start_time=1746057600" \ --header "Authorization: Bearer YOUR_API_KEY"Replace YOUR_API_KEY with the value stored in PERPLEXITY_API_KEY; do not commit or log the header value.
CLI and IDE integrations: same secret, different entry points
Section titled “CLI and IDE integrations: same secret, different entry points”The Perplexity CLI authenticates every command with a Perplexity API key; the documented method is the PERPLEXITY_API_KEY environment variable.
Framework and IDE guides (LangChain, LiteLLM, Haystack, Vercel AI SDK, OpenClaw) follow the same export step. OpenClaw also allows PERPLEXITY_API_KEY in ~/.openclaw/.env for daemon installs.
Proxy through Cloudflare AI Gateway
Section titled “Proxy through Cloudflare AI Gateway”Cloudflare’s Perplexity provider doc shows using the OpenAI SDK with apiKey from env.PERPLEXITY_API_KEY and a gateway baseURL. The key material is still your Perplexity API key; only the request URL changes.
Keep keys out of agent context and client-side code
Section titled “Keep keys out of agent context and client-side code”Cookbook security guidance for agents reinforces server-side-only keys:
Keep the API key server-side and out of source control, screenshots, browser JavaScript, and Slack.
— Perplexity, Incident responder cookbook
The Search as Code for coding agents article warns: keep the key in your environment; do not put it in input files, agent context, prompts, or the repository.
Cursor and Claude Code integrations say to treat PERPLEXITY_API_KEY like a password—do not paste it into chat or prompts—and rotate immediately if exposed.
Common mistakes
Section titled “Common mistakes”- Hardcoding the key in source, fixtures, or commit messages (API key management, SDK best practices).
- Echoing or inspecting the key in the shell (
echo $PERPLEXITY_API_KEY,env, prefix checks)—documented as unsafe in Agent API quickstart. - Forgetting a new Windows terminal after
setx(Decisions quickstart). - Committing
.envwithout adding it to.gitignore(SDK best practices). - Mixing env var names when using Mastra or LangChain recipes that mention
PPLX_API_KEY(Mastra, LangGraph VC memo). - Exposing the key in browser or Slack when building connectors (Incident responder).
How to check this on your site
Section titled “How to check this on your site”-
Create or locate an API key in the Perplexity API Console (linked from Examples overview as console.perplexity.ai/project/keys).
-
Install the SDK for your language following the SDK overview (
pip install perplexityaiornpm install @perplexity-ai/perplexity_ai). -
Export
PERPLEXITY_API_KEYin your shell using the Getting started quickstart, then run the presence check from the Search API quickstart without printing the secret. -
Run a minimal client from the SDK overview (
Perplexity()/new Perplexity()) against the API product you need (Agent, Search, or Embeddings quickstart). -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will correct or clarify it.