401 Authorization Required Response in API

My credits are burning but im getting 401s… why?

Here’s a fresh, first-hand capture of what happens when this backend calls Perplexity right now:

REQUEST:
{
"model": "sonar",
"messages": [
{
"role": "system",
"content": "You are a concise news analyst. Always reply with raw JSON."
},
{
"role": "user",
"content": "Return a minimal JSON object with a hello message."
}
],
"temperature": 0.2,
"max_tokens": 200
}

RESPONSE STATUS: 401
HEADERS:
date: Wed, 12 Nov 2025 16:14:25 GMT
content-type: text/html
transfer-encoding: chunked
connection: keep-alive
cf-ray: 99d7527ecdf5e677-DEN
cf-cache-status: DYNAMIC
strict-transport-security: max-age=15552000; includeSubDomains; preload
server: cloudflare

BODY (first 400 chars):
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>openresty/1.27.4</center>
<script>(function(){function c(){var b=a.contentDocument||a.contentWindow document ...

So every request—including this simple test payload—gets intercepted by Cloudflare (note the cf-ray header and the HTML body). Perplexity counts the call as soon as it hits their edge, which is why credits go down even though the actual API never returns JSON.

This confirms the issue isn’t in our prompt or payload; the token is being blocked before it reaches the service. You’ll need to work with Perplexity to allow this key/IP (or rotate to a key with server-side access). Until then, any automated refresh will yield the same 401 HTML page while still consuming credits.

3 Likes

Having the same issue…

When you say you have to work with perplexity to resolve can you explain specifically what you mean?

I’m getting this through openrouter chat, every perplexity model

I am having the same issue. Any api call yields this error now!

1 Like

Same issue with sonar-deep-research

I guess I have the same problem accessing the API from Claude Code. See bug report below:

I’m unable to connect to the Perplexity API through the official MCP server (@perplexity-ai/mcp-server), receiving a 401 Unauthorized error with a Cloudflare challenge response.

Environment:

  • OS: Windows 11

  • Client: Claude Code (VS Code extension)

  • MCP Package: @perplexity-ai/mcp-server (also tested community perplexity-mcp)

  • API Key: pplx-p3fj9hmT... (regenerated fresh key, confirmed valid)

Configuration (.mcp.json):

{
  "perplexity": {
    "command": "npx",
    "args": ["-y", "@perplexity-ai/mcp-server"],
    "env": {
      "PERPLEXITY_API_KEY": "..."
    }
  }
}

Symptoms:

  • All MCP calls return 401 with Cloudflare HTML challenge page (not a standard API error)

  • Error contains: openresty/1.27.4 and Cloudflare challenge script

Troubleshooting completed:

  1. :white_check_mark: Verified API key works via direct curl:

    curl "https://api.perplexity.ai/chat/completions" -H "Authorization: Bearer pplx-..." -H "Content-Type: application/json" -d "{...}"
    
    

    → Returns valid JSON response

  2. :white_check_mark: Regenerated API key - same issue

  3. :white_check_mark: Cleared npm cache (npx clear-npx-cache)

  4. :white_check_mark: Tested both official and community MCP packages - same result

  5. :white_check_mark: Other MCP servers (Exa, Firecrawl) work correctly in same environment

Conclusion:
The API key and network can reach Perplexity (curl works), but requests from Node.js/npx environment are being blocked by Cloudflare bot protection.

Request:
Please advise on how to resolve the Cloudflare blocking for MCP server connections.

Thank you.

1 Like