--- title: Perplexity Search id: perplexity-adapter order: 10 description: "Give a TanStack AI agent live web results with the Perplexity Search API and @tanstack/ai-perplexity, or chat with Sonar." keywords: - tanstack ai - perplexity - search api - web search - grounding --- `@tanstack/ai-perplexity` exposes the Perplexity Search API as a tool. Pair it with a chat adapter that calls tools, such as `openaiText` or `anthropicText`. Want Sonar, the Perplexity chat model, instead? See [Chat with Sonar](#chat-with-sonar). ## 1. Install This page uses OpenAI for the chat. react: @tanstack/ai @tanstack/ai-react @tanstack/ai-openai @tanstack/ai-perplexity vue: @tanstack/ai @tanstack/ai-vue @tanstack/ai-openai @tanstack/ai-perplexity solid: @tanstack/ai @tanstack/ai-solid @tanstack/ai-openai @tanstack/ai-perplexity svelte: @tanstack/ai @tanstack/ai-svelte @tanstack/ai-openai @tanstack/ai-perplexity preact: @tanstack/ai @tanstack/ai-preact @tanstack/ai-openai @tanstack/ai-perplexity angular: @tanstack/ai @tanstack/ai-angular @tanstack/ai-openai @tanstack/ai-perplexity vanilla: @tanstack/ai @tanstack/ai-client @tanstack/ai-openai @tanstack/ai-perplexity octane: @tanstack/ai @tanstack/ai-octane @tanstack/ai-openai @tanstack/ai-perplexity octane ## 2. Set the API key Get a key in the [Perplexity console](https://console.perplexity.ai/group/keys). Put it in the environment of your server, next to the key of your chat provider: ```bash PERPLEXITY_API_KEY=pplx-... OPENAI_API_KEY=sk-... ``` The tool reads `PERPLEXITY_API_KEY` first, then `PPLX_API_KEY`. ## 3. Stream a chat with web search from the server Add the search tool to `tools`. The model calls it when it needs fresh facts: ```typescript import { chat, chatParamsFromRequest, toServerSentEventsResponse, } from "@tanstack/ai"; import { openaiText } from "@tanstack/ai-openai"; import { perplexitySearchTool } from "@tanstack/ai-perplexity"; const search = perplexitySearchTool({ defaultMaxResults: 5 }); export async function POST(request: Request) { const { messages, threadId, runId } = await chatParamsFromRequest(request); const stream = chat({ adapter: openaiText("gpt-6.1-sol"), tools: [search], messages, threadId, runId, }); return toServerSentEventsResponse(stream); } ``` To use Claude, swap `openaiText` for `anthropicText`. Any adapter that calls tools works the same way. ## 4. Render the chat The client is the same for every provider. Point `useChat` at your route: ```tsx import { useState } from "react"; import { useChat, fetchServerSentEvents } from "@tanstack/ai-react"; export function Chat() { const [input, setInput] = useState(""); const { messages, sendMessage } = useChat({ connection: fetchServerSentEvents("/api/chat"), threadId: "support-chat", }); return (
); } ``` For Vue, Solid, Svelte, Angular, and the other frameworks, see [Quick Start](../getting-started/quick-start#3-render-the-chat). Ask "What were the top AI papers this week?" and the answer now uses live web results. For more on tools, see [Tools](../tools/tools). ## Search without an agent To call the Search API in your own code, use `PerplexitySearchClient`: ```ts import { PerplexitySearchClient } from "@tanstack/ai-perplexity"; const client = new PerplexitySearchClient(); const { results } = await client.search({ query: "mars sample return mission", max_results: 5, search_recency_filter: "month", }); ``` The client also takes these, which the tool does not: - `max_tokens_per_page`: the most content tokens for each page. - `query` as an array of up to 5 queries. ## Chat with Sonar Sonar is the Perplexity chat model. It searches the web by itself and does not take custom tools, so do not give it `perplexitySearchTool`. This package has no Sonar adapter. Use [`openaiCompatible`](./openai-compatible.md): ```ts import { chat } from "@tanstack/ai"; import { openaiCompatible } from "@tanstack/ai-openai/compatible"; import { getPerplexityIntegrationHeaders } from "@tanstack/ai-perplexity"; const perplexity = openaiCompatible({ name: "perplexity", baseURL: "https://api.perplexity.ai", apiKey: process.env.PERPLEXITY_API_KEY!, models: ["sonar", "sonar-pro"], defaultHeaders: getPerplexityIntegrationHeaders(), }); const stream = chat({ adapter: perplexity("sonar"), messages: [{ role: "user", content: "What is the latest on the Mars rover?" }], threadId: "thread-1", }); ``` The OpenAI SDK then calls `POST https://api.perplexity.ai/chat/completions`, the OpenAI-compatible path of Sonar. `getPerplexityIntegrationHeaders()` is optional. It adds the Perplexity attribution header `X-Pplx-Integration` (`tanstack/