TensaHub › Perplexity

Perplexity · sonar

API for the model sonar at Perplexity, pricing at the provider. curl, Python and JavaScript samples, live testing.

Test and rate in the catalog

Paid; answers with web search.

Type
Model
Provider
Perplexity
Model ID
sonar
Pricing
Price not disclosed
Keyless access
No
Key variable
PERPLEXITY_API_KEY
Base URL (OpenAI-compatible)
https://api.perplexity.ai
Inputs
Text
Developer rating
Not rated yet
Documentation
https://docs.perplexity.ai

Code samples

cURL

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

Python

# pip install openai
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.perplexity.ai", api_key=os.environ["PERPLEXITY_API_KEY"])
r = client.chat.completions.create(
    model="sonar",
    messages=[{"role": "user", "content": "Hello, introduce yourself in one sentence."}],
)
print(r.choices[0].message.content)

JavaScript

// npm install openai
import OpenAI from 'openai'

const client = new OpenAI({ baseURL: 'https://api.perplexity.ai', apiKey: process.env.PERPLEXITY_API_KEY })
const r = await client.chat.completions.create({
  model: 'sonar',
  messages: [{ role: 'user', content: 'Hello, introduce yourself in one sentence.' }],
})
console.log(r.choices[0].message.content)

Generate the integration code →

Popular searches

Frequently asked questions

Is the Perplexity · sonar API free?

Pricing is not published in a standard form: check the provider’s page.

How do I call Perplexity · sonar from my code?

Perplexity · sonar exposes an OpenAI-compatible API at https://api.perplexity.ai: use any OpenAI SDK (Python, JavaScript…) with this base URL. Model identifier: sonar. Ready-to-use code (streaming, JSON, retries) is available in the developer toolkit.

What are the alternatives to Perplexity · sonar?

Similar APIs listed in the catalog: Perplexity: Sonar.

Alternatives