Perplexity
The @mastra/perplexity package wraps the Perplexity Search API as a Mastra-compatible tool. It exposes a factory function that returns a tool created with createTool() and full Zod input/output schemas.
For chat completions or agentic workflows powered by Perplexity, use Mastra's built-in Perplexity model provider or Perplexity Agent provider. Those are separate from this Search tool.
InstallationInstallation的直接链接
Install the package alongside Zod:
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/perplexity zod
pnpm add @mastra/perplexity zod
yarn add @mastra/perplexity zod
bun add @mastra/perplexity zod
Usage exampleUsage example的直接链接
The following example creates the search tool with the default configuration. By default, the tool reads PERPLEXITY_API_KEY (with PPLX_API_KEY as a fallback) from the environment. Pass { apiKey } explicitly to override.
import { createPerplexitySearchTool } from '@mastra/perplexity'
const searchTool = createPerplexitySearchTool()
To pass an API key explicitly:
import { createPerplexitySearchTool } from '@mastra/perplexity'
const searchTool = createPerplexitySearchTool({ apiKey: 'pplx-...' })
ConfigurationConfiguration的直接链接
All factory functions accept a PerplexityClientOptions object:
apiKey?:
PERPLEXITY_API_KEY then PPLX_API_KEY environment variables.baseUrl?:
fetch?:
fetch implementation. Useful for tests, retries, or instrumentation.MethodsMethods的直接链接
Factory functionsFactory functions的直接链接
createPerplexityTools(config?)createperplexitytoolsconfig的直接链接
Returns an object containing all Perplexity tools that share the supplied configuration.
import { createPerplexityTools } from '@mastra/perplexity'
const tools = createPerplexityTools({ apiKey: 'pplx-...' })
// tools.perplexitySearch
Returns: { perplexitySearch }
createPerplexitySearchTool(config?)createperplexitysearchtoolconfig的直接链接
Creates a tool that searches the web using the Perplexity Search API. Returns ranked results with titles, URLs, snippets, and optional publication dates.
The tool is registered with the ID perplexity-search.
import { createPerplexitySearchTool } from '@mastra/perplexity'
const searchTool = createPerplexitySearchTool()
InputInput的直接链接
query:
maxResults?:
searchDomainFilter?:
- to exclude it (for example, -pinterest.com). Do not mix allow- and deny-list entries in the same call.searchRecencyFilter?:
searchAfterDateFilter?:
searchBeforeDateFilter?:
OutputOutput的直接链接
query:
results:
title:
url:
snippet:
date?:
Agent exampleAgent example的直接链接
The following example registers the search tool on an agent so it can fetch fresh web results before answering.
import { Agent } from '@mastra/core/agent'
import { createPerplexitySearchTool } from '@mastra/perplexity'
const agent = new Agent({
id: 'research-agent',
name: 'Research Agent',
model: 'anthropic/claude-sonnet-4-6',
instructions:
'You are a research assistant. Use the perplexity-search tool to find up-to-date information from the web before answering.',
tools: {
search: createPerplexitySearchTool(),
},
})
Environment variablesEnvironment variables的直接链接
| Variable | Description |
|---|---|
PERPLEXITY_API_KEY | Your Perplexity API key. Used as the default when apiKey isn't passed to a factory function. |
PPLX_API_KEY | Fallback used when PERPLEXITY_API_KEY is unset. |