Perplexity tools
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.
InstallationDirect link to Installation
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 exampleDirect link to Usage 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-...' })
ConfigurationDirect link to Configuration
All factory functions accept a PerplexityClientOptions object:
apiKey?:
baseUrl?:
fetch?:
MethodsDirect link to Methods
Factory functionsDirect link to Factory functions
createPerplexityTools(config?)Direct link to 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?)Direct link to 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()
InputDirect link to Input
query:
maxResults?:
searchDomainFilter?:
searchRecencyFilter?:
searchAfterDateFilter?:
searchBeforeDateFilter?:
OutputDirect link to Output
query:
results:
title:
url:
snippet:
date?:
Agent exampleDirect link to Agent 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 variablesDirect link to Environment variables
| Variable | Description |
|---|---|
PERPLEXITY_API_KEY | Your Perplexity API key. Used as the default when apiKey is not passed to a factory function. |
PPLX_API_KEY | Fallback used when PERPLEXITY_API_KEY is unset. |