Last updated: 2026-04-20

Perplexity Integration

Route Perplexity API calls through SpendLil.

SpendLil proxies Perplexity's API. Swap api.perplexity.ai for perplexity.gateway.spendlil.ai and add X-SpendLil-Key. Perplexity is OpenAI-compatible — use the OpenAI SDK with the Perplexity gateway.

URL Pattern

diff Before → After
- POST https://api.perplexity.ai/chat/completions
+ POST https://perplexity.gateway.spendlil.ai/chat/completions

OpenAI SDK (Python)

python Use the OpenAI SDK with the Perplexity gateway
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ['PERPLEXITY_API_KEY'],
    base_url='https://perplexity.gateway.spendlil.ai',
    default_headers={'X-SpendLil-Key': os.environ['SPENDLIL_KEY']},
)

response = client.chat.completions.create(
    model='sonar',
    messages=[{'role': 'user', 'content': 'What is SpendLil?'}],
)
print(response.choices[0].message.content)

cURL

bash Direct HTTP request
curl https://perplexity.gateway.spendlil.ai/chat/completions \
  -H "Authorization: Bearer $PERPLEXITY_API_KEY" \
  -H "X-SpendLil-Key: $SPENDLIL_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "sonar",
    "messages": [{"role": "user", "content": "What is SpendLil?"}]
  }'
OpenAI-compatible

Perplexity's chat completions API mirrors OpenAI's schema, with additional fields for citations. Any OpenAI SDK or tool that accepts a base_url override will work unchanged.