Perplexity CLI uses API key authentication to access Perplexity AI's Sonar models.
To use Perplexity CLI, you need a Perplexity API key:
- Get your API key from Perplexity API Settings
- Configure the key using one of the methods below
export PERPLEXITY_API_KEY="your_api_key_here"Create a .env file in your project root:
PERPLEXITY_API_KEY=your_api_key_hereTo update or reconfigure your API key during a session, use the /auth command:
# Within the CLI, type:
/authTo log out and clear your saved API key:
# Within the CLI, type:
/logoutYou can create a .perplexity/.env file in your project directory or in your home directory. Creating a plain .env file also works, but .perplexity/.env is recommended to keep Perplexity CLI variables isolated from other tools.
Important: Some environment variables (like DEBUG and DEBUG_MODE) are automatically excluded from project .env files to prevent interference with perplexity-cli behavior. Use .perplexity/.env files for perplexity-cli specific variables.
Perplexity CLI automatically loads environment variables from the first .env file it finds, using the following search order:
- Starting in the current directory and moving upward toward
/, for each directory it checks:.perplexity/.env.env
- If no file is found, it falls back to your home directory:
~/.perplexity/.env~/.env
Important: The search stops at the first file encountered—variables are not merged across multiple files.
Project-specific configuration:
mkdir -p .perplexity
cat >> .perplexity/.env <<'EOF'
PERPLEXITY_API_KEY="pplx-xxxxxxxxxxxx"
EOFUser-wide settings (available in every directory):
mkdir -p ~/.perplexity
cat >> ~/.perplexity/.env <<'EOF'
PERPLEXITY_API_KEY="pplx-xxxxxxxxxxxx"
EOFWhen running Perplexity CLI in a non-interactive environment (scripts, CI/CD pipelines, etc.), you must configure authentication using environment variables.
Set the PERPLEXITY_API_KEY environment variable:
export PERPLEXITY_API_KEY="pplx-xxxxxxxxxxxx"
perplexity -p "Your prompt here"If the API key is not set in a non-interactive session, the CLI will exit with an error.
For comprehensive guidance on using Perplexity CLI programmatically and in automation workflows, see the Headless Mode Guide.