How to Get a Perplexity API Key: Setup, Storage, and First Request

Getting a Perplexity key takes about two minutes. Keeping it working -- and knowing whether a failure is your key, your quota, or Perplexity itself -- is the part that actually costs teams time.

8 min read
Staff Pick

📡 Monitor your APIs — know when they go down before your users do

Better Stack checks uptime every 30 seconds with instant Slack, email & SMS alerts. Free tier available.

Start Free →

Affiliate link — we may earn a commission at no extra cost to you

A Perplexity API key is a bearer credential. Anything holding it can spend your account's money, which is why the interesting part of this guide is not the four clicks that create the key -- it is where the key lives afterwards and how you diagnose it when requests start failing.

Perplexity's Sonar models return web citations alongside the completion, so responses carry fields that an OpenAI-shaped client will silently drop unless you read them explicitly.

Before you regenerate anything: 401 means the key string is wrong. 403 means the key is real but not entitled. 429 means your quota. 5xx and timeouts mean Perplexity -- check live Perplexity status first.

Create the Key: Five Steps

  1. Sign up. Create an account at perplexity.ai/settings/api and complete whatever verification the console requires.
  2. Open the API keys screen. In the Perplexity API settings, find the API keys section.
  3. Create a key and name it. Name it after the thing that will use it -- prod-api, staging-worker, local-dev. Per-surface keys mean you can revoke one without an outage everywhere else.
  4. Copy the secret immediately. It is displayed once and starts with pplx-. There is no reveal-later button.
  5. Store it as PERPLEXITY_API_KEY in a secret manager or a git-ignored .env file, then verify it before you build anything on top of it.

A valid key with zero credits still fails. This is the single most reported Perplexity setup problem, and it does not look like a billing error -- it looks like the key is broken. Buy credits before you debug the key. Note also that a Perplexity Pro consumer subscription and API credits are separate products; being a Pro subscriber does not by itself make the API callable beyond whatever monthly credit the plan includes.

Verify It With One Request

Do this before writing any application code. A single cURL against the live endpoint separates "the key is wrong" from "my framework is not loading the environment variable" -- two failures that look identical from inside an app.

curl https://api.perplexity.ai/chat/completions \
  -H "Authorization: Bearer $PERPLEXITY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"sonar","messages":[{"role":"user","content":"ping"}]}'

A 200 with a completion body means the key is good. Perplexity's chat endpoint is OpenAI-compatible, so an existing OpenAI client works once you change the base URL and the key.

ResponseWhat it actually meansFix
200Key valid, model accessible, account fundedNothing -- wire it up
401Key string wrong, truncated, rotated or deletedRe-copy from the console; check for whitespace and newlines
403Key real, but not entitled to that model or endpointCheck plan, workspace and model access -- not the key
404Model identifier wrong, or wrong base URLConfirm the exact model string against the console
429Your quota, not a key problemBack off with jitter; see the rate-limit guide below
5xx / timeoutPerplexity side. Your key is fine.Fail over; do not rotate keys mid-incident

Where the Key Should Live

Leaked AI API keys are not a hypothetical. Public repositories and shipped front-end bundles are scraped continuously, and an exposed key on a funded account can burn a serious bill before the first alert fires.

Server-side only, always

Never call Perplexity from a browser or a mobile app. Anything shipped to a client is readable, including values inlined by your bundler. Proxy through a route you control so the key never leaves your infrastructure.

One key per environment and per service

Separate keys for local, staging and production mean a leak is contained and revocation is surgical. A single shared key means every rotation is a coordinated outage.

Git-ignore the .env file, and check history

Adding .env to .gitignore does nothing about a key already committed. Scan history, and treat any key that ever touched a commit as compromised -- rotate rather than hope.

Rotate on a schedule, not on an incident

Create the replacement, deploy it alongside the old one, confirm traffic has moved, then delete the old key. Rotation that starts with deletion is an outage with extra steps.

🔐
Recommended

Stop Pasting Perplexity Keys Into .env Files

A shared vault gives every service its own Perplexity credential, keeps rotation auditable, and means a departing teammate does not take your only copy of the key with them.

Try 1Password Free →

Load the Key Without Leaking It

Most "my key is broken" reports are actually the process never seeing the value. Fail loudly at startup instead of discovering it on the first user request.

const apiKey = process.env.PERPLEXITY_API_KEY;

if (!apiKey) {
  // Fail at boot, not on the first user request
  throw new Error('PERPLEXITY_API_KEY is not set');
}

// Safe to log: proves the value loaded without exposing it
console.log('perplexity key loaded:', apiKey.slice(0, 4) + '...' + apiKey.slice(-4), 'len', apiKey.length);

export async function callPerplexity(body) {
  const res = await fetch('https://api.perplexity.ai/chat/completions', {
    method: 'POST',
    headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
    body: JSON.stringify(body),
  });

  if (res.status === 401) throw new Error('Perplexity: key rejected - check the value, not the network');
  if (res.status === 403) throw new Error('Perplexity: key valid but not entitled to this model');
  if (res.status === 429) throw new Error('Perplexity: rate limited - back off and retry');
  if (res.status >= 500) throw new Error('Perplexity: provider outage - fail over');

  return res.json();
}

Logging the length and the first and last four characters is the fastest way to catch a truncated paste or a trailing newline, and it exposes nothing usable.

Billing: What the Key Can Actually Do

Perplexity is the strictest of the five about payment: the API is credit-based and a key will not authenticate usable requests until credits exist on the account. A card on file is effectively a prerequisite, not an optional upgrade.

Set a spend alert the same day you create the key. The failure mode nobody plans for is not an outage -- it is a runaway retry loop or a leaked key quietly spending for a week. Full cost mechanics are in our Perplexity API pricing guide.

📡
Recommended

Catch a Broken Perplexity Key in Seconds

Continuous checks on your Perplexity endpoint surface a 401 after a botched rotation immediately, instead of leaving it to a customer to report.

Try Better Stack Free →

Key Problem or Perplexity Outage? A 60-Second Check

  1. Read the status code. 401 and 403 are yours. 429 is quota. 5xx and timeouts are Perplexity.
  2. Print the key length. A value shorter than expected is a truncated copy, not a revoked key.
  3. Try the cURL above. If cURL works and your app does not, the problem is environment loading.
  4. Try a second key. If a freshly minted key fails identically, it is not the key.
  5. Check status.perplexity.com and live Perplexity monitoring before touching anything in production.

The full outage-side playbook is in our Is Perplexity Down? guide.

Keep a Second Key Warm

A key that gets revoked, leaked or rate limited is an outage if it is your only path to an LLM. Provisioning a second provider's key on day one costs nothing until you use it.

OpenAI

Independent key and quota. Keep a second provider configured so a key or entitlement problem on Perplexity is a reroute, not an outage.

Check OpenAI status →

Groq

Independent key and quota. Keep a second provider configured so a key or entitlement problem on Perplexity is a reroute, not an outage.

Check Groq status →

Cohere

Independent key and quota. Keep a second provider configured so a key or entitlement problem on Perplexity is a reroute, not an outage.

Check Cohere status →

Frequently Asked Questions

How do I get a Perplexity API key?

Create a Perplexity account, open the Perplexity API settings at perplexity.ai/settings/api, and create a new key from the API keys screen. Copy the secret immediately -- it is shown once. Store it in a secret manager or a local .env file as PERPLEXITY_API_KEY, never in source control, then verify it with a single chat completion request before wiring it into an application.

Is the Perplexity API key free?

Perplexity is the strictest of the five about payment: the API is credit-based and a key will not authenticate usable requests until credits exist on the account. A card on file is effectively a prerequisite, not an optional upgrade. Either way, creating the key itself costs nothing; what varies between providers is whether the key can do useful work before a payment method exists.

Why is my Perplexity API key returning 401?

A 401 from Perplexity almost always means the key string itself is wrong: a truncated copy-paste, a stray newline from a shell heredoc, the wrong environment loaded, or a key that was rotated or deleted in the console. Check the raw value your process actually sees before assuming the key is bad -- printing the length and first four characters is usually enough to spot a mangled copy. A 403 is different: the key is real but not entitled to that model or endpoint.

Where should I store my Perplexity API key?

In a secret manager or your platform's environment-variable store, injected at runtime as PERPLEXITY_API_KEY. Never commit it, never ship it to a browser bundle, and never put it in a mobile app -- keys extracted from client-side code are the most common way AI API bills get hijacked. All calls to Perplexity should originate from a server you control.

My Perplexity key worked yesterday and fails today -- is Perplexity down?

Check the status code before you regenerate anything. 401 and 403 are key or entitlement problems. 429 is your own quota. 500, 502, 503 and connection timeouts point at Perplexity itself -- confirm on status.perplexity.com or with live Perplexity monitoring, because rotating a perfectly good key during an outage just adds a second variable to the incident.

Related Perplexity Guides

Know Instantly Whether It Is Your Key or Perplexity

API Status Check monitors Perplexity and every other provider in your stack, so a bad rotation and a real outage never look the same again.

Start Your Free Trial →

Alert Pro

14-day free trial

Stop checking — get alerted instantly

Next time Perplexity goes down, you'll know in under 60 seconds — not when your users start complaining.

  • Email alerts for Perplexity + 9 more APIs
  • $0 due today for trial
  • Cancel anytime — $9/mo after trial

🌐 Can't Access Perplexity?

If Perplexity is working for others but not for you, it might be an ISP or regional issue. A VPN can help bypass network-level blocks and routing problems.

🔒

Troubleshoot with a VPN

Connect from a different region to test if the issue is local to your network. Also protects your connection on public Wi-Fi.

Try NordVPN — 30-Day Money-Back Guarantee
🔑

Secure Your Perplexity Account

Service outages are a common time for phishing attacks. Use a password manager to keep unique, strong passwords for every account.

Try NordPass — Free Password Manager
Quick ISP test: Try accessing Perplexity on mobile data (Wi-Fi off). If it works, the issue is with your ISP or local network.

⏳ While You Wait — Try These Alternatives

🛠 Tools We Use & Recommend

Tested across our own infrastructure monitoring 200+ APIs daily

SEMrushBest for SEO

SEO & Site Performance Monitoring

Used by 10M+ marketers

Track your site health, uptime, search rankings, and competitor movements from one dashboard.

We use SEMrush to track how our API status pages rank and catch site health issues early.

From $129.95/moTry SEMrush Free
View full comparison & more tools →Affiliate links — we earn a commission at no extra cost to you