Perplexity Sonar API
The Sonar API is Perplexity's developer-facing product: an OpenAI-compatible REST API that gives any LLM application real-time web search capability. Instead of building and maintaining a web scraping + retrieval pipeline, you call Sonar and get back answers grounded in live web content, with source citations, in a single API call.
What Sonar Provides
Real-time web grounding
Every response is grounded in live web content retrieved at query time - not model training data. Eliminates hallucinations on current-events questions.
Citations in response
API response includes a citations array with URLs, titles, and snippets for every source used. Render these in your UI for transparency.
OpenAI-compatible API
Drop-in replacement for OpenAI chat completions - change the base URL and API key; keep the same code. Works with any library that targets OpenAI.
Search domain filtering
Restrict search to specific domains (e.g. search_domain_filter: ["arxiv.org", "pubmed.gov"]) or exclude domains. Useful for academic or compliance use cases.
Sonar Model Family
| Model | Context | $/million tokens | Best for |
|---|---|---|---|
| sonar | 128K | $1 in / $1 out | Fast, cheap factual queries |
| sonar-pro | 200K | $3 in / $15 out | Complex reasoning + web research |
| sonar-reasoning | 128K | $1 in / $5 out | CoT reasoning with web grounding |
| sonar-deep-research | 128K | $2 in / $8 out | Multi-step research reports via API |
Plus a $5/1000 requests search fee on top of token costs. Prices indicative - check current pricing at docs.perplexity.ai.
Quickstart
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.PERPLEXITY_API_KEY,
baseURL: "https://api.perplexity.ai",
});
const response = await client.chat.completions.create({
model: "sonar",
messages: [
{
role: "user",
content: "What are the latest developments in AI agent frameworks as of June 2026?"
}
],
// Perplexity-specific options:
search_recency_filter: "week", // "day" | "week" | "month" | "year"
return_citations: true,
});
const answer = response.choices[0].message.content;
const sources = response.citations; // Array of { url, title, snippet }When to Use Sonar API
- Current-events questions: Anything requiring information from after model training cutoff
- Fact verification: Claims that should be grounded in public sources
- Company/person research: Where web data is more complete than training data
- Replacing a custom RAG pipeline for public web content (avoids crawler + vector DB infrastructure)
Sonar API vs Raw LLM + Web Search Tool
The alternative to Sonar is building your own: an LLM + a web search tool (e.g. Bing Search API, Brave Search API) + retrieval logic. Sonar costs more per call but saves weeks of infrastructure work. For high-volume production systems, the economics may favour building your own; for most applications, Sonar is the faster path.