Environment Variables
Configure environment variables for local development and production.
Environment Variables
Environment variables let you pass configuration and secrets to your functions without hardcoding them.
Local Development
Automatic .env Loading
ccp dev automatically loads a .env file from your project root:
# .env
API_KEY=sk-abc123
DATABASE_URL=postgres://localhost:5432/mydbccp dev
# Automatically loaded .env file...Custom Env File
Point to a different file with --env:
ccp dev --env .env.stagingAccessing in Code
Use process.env to read variables:
export function handler(request: Request): Response {
const apiKey = process.env.API_KEY;
if (!apiKey) {
return new Response("API_KEY not set", { status: 500 });
}
return Response.json({ configured: true });
}Production
Production environment variables are stored per function and injected into your deployment at runtime. Set them by PATCH-ing the function via the serverless API:
curl -X PATCH https://api.clusterbase.dev/api/v1/serverless/functions/<FUNCTION_ID> \
-H "Authorization: Bearer $(ccp auth token)" \
-H "Content-Type: application/json" \
-d '{
"environment_variables": {
"API_KEY": "sk-abc123",
"DATABASE_URL": "postgres://..."
}
}'The function ID is in your .cluster/config.json. A UI for managing these in the console is coming soon.
.env in .gitignore
The ccp init command adds .env to .gitignore by default. Never commit secrets to your repository.