Skip to main content
Perplexity

Search documentation

Type to search this documentation.

On this pageOverview

Perplexity with Claude Code

Claude Code is Anthropic's agentic coding tool that lives in your terminal, IDE, and CI. It can edit files, run commands, and call Model Context Protocol (MCP) servers as part of its workflow. This guide covers three integration paths for Perplexity:

Official TypeScript SDK

Let Claude Code write and run application code that calls the Perplexity API directly. Recommended.

OpenAI-Compatible SDK

Reuse an existing OpenAI client by pointing baseURL at https://api.perplexity.ai/v1.

Claude Code MCP

Register the hosted Perplexity Docs MCP with claude mcp add so Claude Code can look up Perplexity docs while you work.

  • Claude Code installed — see the Claude Code quickstart
  • Node.js 18+ for the TypeScript examples
  • A Perplexity API key

Get API Key

Generate a key from the Perplexity API Portal.

Get Key

Never hardcode your API key in source files or commit it to a repository. Store it in an environment variable and read it at runtime:

Bash
export PERPLEXITY_API_KEY="your_api_key_here"
powershell
setx PERPLEXITY_API_KEY "your_api_key_here"

Create a .env file in your project root and add it to .gitignore:

Bash
# .env
PERPLEXITY_API_KEY=your_api_key_here

Load it at startup (for example with dotenv):

TypeScript
import "dotenv/config";

Section titled “Path 1: Official TypeScript SDK (recommended)”

Have Claude Code scaffold and edit code that calls the Perplexity API using the official SDK. This is the most reliable path and gives you full type safety, preset support, and access to every API feature.

Bash
npm install @perplexity-ai/perplexity_ai

The Agent API is the recommended surface for most applications. Use the low preset for web-grounded responses with sensible defaults:

TypeScript
import Perplexity from "@perplexity-ai/perplexity_ai";

const client = new Perplexity(); // reads PERPLEXITY_API_KEY from the environment

const response = await client.responses.create({
  preset: "low",
  input: "Summarize the latest changes to the Perplexity Agent API.",
});

console.log(`Model used: ${response.model}`);
console.log(response.output_text);
Python equivalent
Python
from perplexity import Perplexity

client = Perplexity()  # reads PERPLEXITY_API_KEY from the environment

response = client.responses.create(
    preset="low",
    input="Summarize the latest changes to the Perplexity Agent API.",
)

print(f"Model used: {response.model}")
print(response.output_text)

Enable the web_search tool for explicit control over when search is used:

TypeScript
import Perplexity from "@perplexity-ai/perplexity_ai";

const client = new Perplexity();

const response = await client.responses.create({
  model: "openai/gpt-5.6-terra",
  input: "What are the latest developments in AI inference hardware?",
  tools: [{ type: "web_search" }],
  instructions:
    "You have access to a web_search tool. Use it for questions about current events, news, or recent developments.",
});

if (response.status === "completed") {
  console.log(response.output_text);
}

For more presets, tools, and configuration options see the Agent API quickstart and the SDK overview.


If your project already uses an OpenAI client, you can reuse it by pointing the baseURL at https://api.perplexity.ai/v1. Perplexity accepts POST /v1/responses as an alias for the Agent API.

Bash
npm install openai
TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.PERPLEXITY_API_KEY,
  baseURL: "https://api.perplexity.ai/v1",
});

const response = await client.responses.create({
  model: "openai/gpt-5.6-terra",
  input: "Explain the key differences between REST and GraphQL APIs.",
});

console.log(response.output_text);
Python equivalent
Python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("PERPLEXITY_API_KEY"),
    base_url="https://api.perplexity.ai/v1",
)

response = client.responses.create(
    model="openai/gpt-5.6-terra",
    input="Explain the key differences between REST and GraphQL APIs.",
)

print(response.output_text)

Claude Code supports the Model Context Protocol (MCP). The hosted Perplexity Docs MCP at https://docs.perplexity.ai/mcp lets Claude Code search and read Perplexity's documentation directly — useful for grounding code suggestions in canonical docs without context-switching to a browser.

Claude Code can register a remote MCP server over HTTP using the claude mcp add command with the --transport http flag:

Add the server only for the current project (writes to .mcp.json in your repo root):

Bash
claude mcp add --transport http --scope project perplexity-docs https://docs.perplexity.ai/mcp

Add the server for all projects on this machine:

Bash
claude mcp add --transport http --scope user perplexity-docs https://docs.perplexity.ai/mcp
  1. Confirm the server is registered

    Run claude mcp list and check that perplexity-docs appears in the output.

  2. Restart Claude Code

    Quit and relaunch Claude Code (or run /mcp from inside a session) so the new server is loaded.

  3. Use it from a prompt

    Ask Claude Code something like "Using the Perplexity docs MCP, show me how to enable low in the Agent API." Claude Code will call the MCP server and ground its answer in current documentation.


Section titled “Replacing Claude Code's model (not recommended)”

Claude Code is designed to run on Anthropic's Claude models, and the agent's tool use, prompting, and editing behavior are tuned to those models. Pointing the Claude Code CLI at the Perplexity API as an alternative model provider is not an officially supported configuration.

Instead:

  • Use Perplexity from your project code (Path 1 or Path 2). Claude Code will happily write, edit, and run code that calls Perplexity for web-grounded answers, research, and search.
  • Add the Perplexity Docs MCP (Path 3) so Claude Code can look up Perplexity documentation in-session.

This combination gives you Claude Code's editing and orchestration strengths alongside Perplexity's search-grounded responses, without depending on undocumented model-override behavior.


Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu