Perplexity provides a Node.js SDK to integrate its AI search and answer capabilities into your applications. Without a proper SDK setup, you must write raw HTTP requests and handle authentication manually. This article explains how to install the Perplexity Node.js SDK, configure it with your API key, and run a basic query. You will complete the entire installation in under five minutes.
Key Takeaways: Installing the Perplexity Node.js SDK
- npm install @perplexity/sdk: Adds the Perplexity SDK package to your Node.js project.
- process.env.PERPLEXITY_API_KEY: Stores your API key as an environment variable for secure access.
- new Perplexity({ apiKey }): Creates an SDK client instance to send queries and receive answers.
What the Perplexity Node.js SDK Does
The Perplexity Node.js SDK is an official client library that wraps the Perplexity API. It handles authentication, request formatting, and response parsing so you can focus on building features. Before installing, you need a Node.js runtime version 18 or later and a Perplexity API key from the developer dashboard. The SDK works with both CommonJS and ES Module projects.
Steps to Install and Configure the Perplexity Node.js SDK
- Create or open your Node.js project
Navigate to your project folder in the terminal. If you do not have a project, runnpm init -yto generate a package.json file. - Install the Perplexity SDK package
Run the commandnpm install @perplexity/sdkin the terminal. This downloads the SDK and its dependencies into the node_modules folder. - Create an environment variable for your API key
Copy your API key from the Perplexity developer dashboard. Create a file named .env in the project root and add the linePERPLEXITY_API_KEY=your_key_here. Replaceyour_key_herewith the actual key. - Install dotenv to load environment variables
Runnpm install dotenv. This package reads the .env file and makes the variables available in process.env. - Write a basic script to test the SDK
Create a file named index.js and add the following code:require('dotenv').config();
const { Perplexity } = require('@perplexity/sdk');
const client = new Perplexity({ apiKey: process.env.PERPLEXITY_API_KEY });
async function main() {
const response = await client.query({ query: 'What is the capital of France?' });
console.log(response.answer);
}
main(); - Run the script
Executenode index.jsin the terminal. If the setup is correct, you will see the answer printed to the console.
Common Issues During SDK Installation
npm install fails with a permission error
This usually happens when you run npm without sufficient file permissions. Use sudo npm install @perplexity/sdk on macOS or Linux. On Windows, open the terminal as Administrator.
Cannot find module @perplexity/sdk
The SDK package may not have installed correctly. Delete the node_modules folder and the package-lock.json file. Run npm install again to regenerate them.
API key is undefined
The .env file might be missing or not loaded. Ensure dotenv is imported at the very top of your script. Verify that the .env file is in the same directory as your script and contains the exact variable name PERPLEXITY_API_KEY.
TypeError: client.query is not a function
This indicates you are using an outdated version of the SDK. Run npm update @perplexity/sdk to get the latest version. Check the official documentation for any breaking changes in the API method names.
Perplexity SDK Installation Methods: npm vs Yarn vs pnpm
| Item | npm | Yarn |
|---|---|---|
| Command | npm install @perplexity/sdk | yarn add @perplexity/sdk |
| Lock file | package-lock.json | yarn.lock |
| Install speed | Moderate | Faster with caching |
| Disk usage | Larger node_modules | Smaller with flat structure |
All three package managers produce the same SDK functionality. Choose the one your project already uses. If you are starting fresh, npm is the default and requires no additional setup.
You now have the Perplexity Node.js SDK installed and configured. Try the streamQuery method for real-time answer streaming instead of waiting for the full response. For production applications, store the API key in a secrets manager like AWS Secrets Manager or Azure Key Vault rather than a .env file.