Hi folks,
Might be stupid and silly question.. but that’s my first time playing with API..
I am trying to connect to API and I am getting..
“URL Called:
https://api.perplexity.ai/chat/completions
Full Response:
{
"status": 400,
"reason": "Bad Request"
}
What I am doing wrong?
I’ve copied the API key directly from the settings, so have no clue why it is not getting response back.
Kesku
2
Hey @Piotr_Krol — welcome to the API! A 400 Bad Request usually means there is a formatting issue with your request payload.
The most common causes:
-
Wrong endpoint or base URL — For Sonar models, use https://api.perplexity.ai/v1/sonar
-
Missing or malformed messages array — The payload needs to look like:
{
"model": "sonar-pro",
"messages": [
{"role": "user", "content": "Your question here"}
]
}
-
Invalid model name — Use sonar, sonar-pro, or sonar-reasoning-pro. Old model names like llama-3.1-sonar-small-128k-online are deprecated.
-
Auth header format — Should be Authorization: Bearer pplx-xxxxx (not Api-Key or other formats)
Here is a minimal working curl you can test with:
curl https://api.perplexity.ai/v1/sonar \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "sonar", "messages": [{"role": "user", "content": "Hello"}]}'
If you are still hitting issues, share your request code (with the API key redacted) and I can spot the problem. Docs: Sonar Quickstart