1. Chat Completions (Recommended)
AI-powered Q&A with web search and citations. OpenAI SDK compatible.
Basic query with sonar model:
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "user", "content": "What is the current price of Bitcoin?"}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
With system prompt:
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "system", "content": "Be precise and concise. Answer in bullet points."},
{"role": "user", "content": "What are the latest developments in AI?"}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
Advanced query with sonar-pro:
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar-pro",
"messages": [
{"role": "user", "content": "Compare the latest iPhone and Samsung Galaxy flagship phones"}
],
"temperature": 0.2,
"web_search_options": {
"search_context_size": "high"
}
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
2. Search with Domain Filter
Limit search to specific domains.
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "user", "content": "Latest research on transformer architectures"}
],
"search_domain_filter": ["arxiv.org", "openai.com", "anthropic.com"]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
Exclude domains (add - prefix):
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "user", "content": "Best programming practices"}
],
"search_domain_filter": ["-reddit.com", "-quora.com"]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
3. Search with Time Filter
Filter by recency.
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "user", "content": "Major tech news"}
],
"search_recency_filter": "week"
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
Filter by date range:
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "user", "content": "AI announcements"}
],
"search_after_date_filter": "12/01/2024",
"search_before_date_filter": "12/31/2024"
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
4. Academic Search
Search academic sources.
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar",
"messages": [
{"role": "user", "content": "Recent papers on large language model alignment"}
],
"search_mode": "academic"
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
5. Raw Search API
Get raw search results without AI synthesis.
Write to /tmp/perplexity_request.json:
```json
{
"query": "Claude AI Anthropic",
"max_results": 5
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/search" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
With domain and time filters:
Write to /tmp/perplexity_request.json:
```json
{
"query": "machine learning tutorials",
"max_results": 10,
"search_domain_filter": ["github.com", "medium.com"],
"search_recency_filter": "month",
"country": "US"
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/search" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
6. Deep Research (Long-form)
For comprehensive research reports.
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar-deep-research",
"messages": [
{"role": "user", "content": "Write a comprehensive analysis of the electric vehicle market in 2024"}
],
"reasoning_effort": "high"
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
7. Reasoning Model
For complex problem-solving.
Write to /tmp/perplexity_request.json:
```json
{
"model": "sonar-reasoning-pro",
"messages": [
{"role": "user", "content": "Analyze the pros and cons of microservices vs monolithic architecture for a startup"}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://api.perplexity.ai/chat/completions" --header "Authorization: Bearer ${PERPLEXITY_API_KEY}" --header "Content-Type: application/json" -d @/tmp/perplexity_request.json'
```
---