Configure Perplexity across development, staging, and production environments.
Use when setting up multi-environment deployments, configuring per-environment secrets,
or implementing environment-specific Perplexity configurations.
Trigger with phrases like "perplexity environments", "perplexity staging",
"perplexity dev prod", "perplexity environment setup", "perplexity config by env".
Configure Perplexity Sonar API across dev/staging/prod. Key decisions per environment: which models are allowed (sonar vs sonar-pro), rate limits, and cost caps. All environments use the same base URL (https://api.perplexity.ai) but different API keys with different budget limits.
Environment Strategy
Environment
Model
Rate Limit
Key Source
Monthly Budget
Development
sonar only
5 RPM self-imposed
.env.local
$10
Staging
sonar only
20 RPM
CI secrets
$50
Production
sonar + sonar-pro
50 RPM
Secret manager
$500+
Prerequisites
Separate Perplexity API keys per environment
openai package installed
Secret management (env files for dev, vault/KMS for prod)
// config/perplexity/base.ts
import OpenAI from "openai";
export const PERPLEXITY_BASE_URL = "https://api.perplexity.ai";
export function createPerplexityClient(apiKey: string): OpenAI {
if (!apiKey) throw new Error("Perplexity API key is required");
if (!apiKey.startsWith("pplx-")) throw new Error("Invalid Perplexity API key format");
return new OpenAI({ apiKey, baseURL: PERPLEXITY_BASE_URL });
}
Step 3: Environment Configs
// config/perplexity/development.ts
export const devConfig = {
apiKey: process.env.PERPLEXITY_API_KEY!,
defaultModel: "sonar" as const,
deepModel: "sonar" as const, // No sonar-pro in dev (cost)
maxTokens: 512,
maxConcurrentRequests: 1,
cacheTTLMs: 24 * 3600_000, // Long cache in dev
};
// config/perplexity/staging.ts
export const stagingConfig = {
apiKey: process.env.PERPLEXITY_API_KEY_STAGING!,
defaultModel: "sonar" as const,
deepModel: "sonar" as const, // Keep sonar in staging
maxTokens: 1024,
maxConcurrentRequests: 2,
cacheTTLMs: 4 * 3600_000,
};
// config/perplexity/production.ts
export const productionConfig = {
apiKey: process.env.PERPLEXITY_API_KEY_PROD!,
defaultModel: "sonar" as const, // Fast queries use sonar
deepModel: "sonar-pro" as const, // Research queries use sonar-pro
maxTokens: 4096,
maxConcurrentRequests: 10,
cacheTTLMs: 1 * 3600_000, // Shorter cache in prod (freshness)
};
Step 4: Environment Resolver
// config/perplexity/index.ts
import { createPerplexityClient } from "./base";
import { devConfig } from "./development";
import { stagingConfig } from "./staging";
import { productionConfig } from "./production";
type SearchDepth = "quick" | "deep";
const configs = {
development: devConfig,
staging: stagingConfig,
production: productionConfig,
};
export function getConfig() {
const env = process.env.NODE_ENV || "development";
const config = configs[env as keyof typeof configs];
if (!config) throw new Error(`Unknown environment: ${env}`);
if (!config.apiKey) throw new Error(`PERPLEXITY_API_KEY not set for ${env}`);
return config;
}
export function getClient() {
return createPerplexityClient(getConfig().apiKey);
}
export function getModelForDepth(depth: SearchDepth): string {
const cfg = getConfig();
return depth === "deep" ? cfg.deepModel : cfg.defaultModel;
}