Skip to main content

Overview

Environment variables allow you to store secrets, API keys, and configuration outside your code. Supabase provides both built-in environment variables and custom secrets management.
Never commit secrets to version control. Always use environment variables for sensitive data.

Built-in Environment Variables

Supabase automatically provides these environment variables to all functions:

Access Built-in Variables

Built-in variables are automatically configured and don’t need to be set manually.

Custom Environment Variables

Set Secrets

Set custom secrets using the CLI:
Set multiple secrets:

Set from File

Set multiple secrets from a file:
Don’t commit .env.production or any secrets file to git. Add them to .gitignore.

List Secrets

View all configured secrets (values are hidden):
Output:

Unset Secrets

Remove a secret:

Using Environment Variables

In Your Functions

Access environment variables with Deno.env.get():

With Type Safety

Create a helper for type-safe environment variables:
Use in your function:

Local Development

Create Local Environment File

Create .env.local for local development:
Add to .gitignore:

Use Local Environment Variables

Serve functions with local environment:
Or for all commands:

Example Local Setup

Create a complete local environment:

Real-World Examples

OpenAI Integration

Securely use OpenAI API:
Set the secret:

Stripe Webhooks

Verify Stripe webhook signatures:
Set secrets:

Email with Resend

Send emails securely:

Database Connection

Connect directly to PostgreSQL:

CI/CD Integration

GitHub Actions

Set secrets in GitHub Actions:
Configure GitHub secrets:
  1. Go to Settings > Secrets and variables > Actions
  2. Add each secret:
    • SUPABASE_ACCESS_TOKEN
    • PROJECT_ID
    • OPENAI_API_KEY
    • STRIPE_SECRET

Environment-Specific Secrets

Use different secrets for staging and production:

Best Practices

Never Hardcode Secrets

Validate Environment Variables

Check variables at startup:

Use .env.example

Provide a template for developers:
Developers copy and fill in their values:

Rotate Secrets Regularly

Update secrets periodically:

Troubleshooting

Secret Not Available

If a secret isn’t available:
  1. Check if set:
  2. Set the secret:
  3. Redeploy the function:

Local vs Production Differences

If behavior differs between local and production:
  1. Compare secrets:
  2. Ensure consistency between environments
  3. Test with production values locally when debugging

Next Steps

Debugging

Debug and monitor your Edge Functions

Deploy Functions

Learn deployment strategies