Perplexity API: curl works, but HTTP Request node on n8n Cloud returns 401 (openresty)

I am using n8n Cloud and trying to call the Perplexity API.
When I call the API from my Mac terminal with curl, it works fine:

curl “https://api.perplexity.ai/chat/completions”
-H “Authorization: Bearer pplx-XXXX”
-H “Content-Type: application/json”
-d ‘{“model”:“sonar”,“messages”:[{“role”:“user”,“content”:“test”}]}’

This returns a valid JSON response with choices etc. (so the API key and endpoint are correct).

However, when I try to do the exact same request from an HTTP Request node on n8n Cloud, I always get:

Authorization failed - please check your credentials
401 Authorization Required – openresty/1.27.4

Here is my node configuration:

  • Method: POST

  • URL: https://api.perplexity.ai/chat/completions

  • Authentication: None

  • Send Headers: ON

  • Headers:

    • Authorization: Bearer pplx-XXXX

    • Content-Type: application/json

  • Body Content Type: JSON

  • Body parameters:

    • model: sonar

    • messages[0][role]: user

    • messages[0][content]: test

So in theory, this should be identical to the curl request that works.

I am on n8n Cloud (simondupont.app.n8n.cloud).
Is there any known issue with calling Perplexity from n8n Cloud (proxy, Cloudflare, header being rewritten, etc.) that could explain why the HTTP Request node gets a 401 while curl with the same API key works?

I can share screenshots of the node configuration and the raw HTTP logs if needed.

Thanks!

hello @Simon4

Have you tried to use the Bearer Auth credentials? You will need to specify only the token (without the Bearer part).

Please, share the workflow.

You can select all nodes with Ctrl+A and copy them with Ctrl+C. Then, past the content after pressing the button </> with Ctrl+V.

Hi @Simon4, Welcome to the n8n community! From what I have seen, this usually comes down to how the request body is being sent. Even though the headers and endpoint are correct, using body parameters does not produce the exact same JSON payload as curl, and the Perplexity API is strict about that. I would switch to sending a raw JSON body that matches the curl request exactly and keep the Authorization header as is, which (I hope) should resolve the 401 error.