WHAT YOU'LL LEARN
- How to use environment variables in Webiny - Application-specific variable prefixes - Best practices for sensitive data

Overview
anchor

Environment variables in Webiny are optional for most scenarios. When needed, they follow a prefix-based system that ensures variables are only available to the appropriate application. This prevents accidental exposure of sensitive data between applications.

Application Prefixes
anchor

Webiny uses prefixes to route variables to specific applications:

  • WEBINY_ADMIN_ - Variables for the Admin application
  • WEBINY_API_ - Variables for the API application
  • No prefix - Available to all applications (use sparingly)

Usage
anchor

Setting Variables
anchor

Create a .env file in your project root:

.env

Accessing Variables
anchor

In your extensions, access variables through process.env:

extensions/api/email.ts
extensions/admin/analytics.tsx

TypeScript Support
anchor

Add type definitions for your environment variables:

webiny-env.d.ts

Environment-Specific Variables
anchor

Local Development
anchor

Variables in .env are loaded during:

  • yarn webiny watch admin
  • yarn webiny watch api
  • yarn webiny deploy

Deployed Environments
anchor

For deployed environments, set variables before deployment:

CI/CD Integration
anchor

In CI/CD pipelines, set environment variables:

.github/workflows/deploy.yml

Best Practices
anchor

Security
anchor

  1. Never commit .env - Add to .gitignore
  2. Use prefixes - Isolate sensitive data by application
  3. Rotate secrets - Change production secrets regularly
  4. Minimal exposure - Only set variables that are needed

Documentation
anchor

Create .env.example for team reference:

.env.example

Variable Naming
anchor

Follow consistent naming conventions:

Common Use Cases
anchor

Third-Party Services
anchor

.env

Feature Flags
anchor

.env

API Configuration
anchor

.env

Debugging
anchor

Verify Variables
anchor

Check loaded variables during development:

Common Issues
anchor

Variable not available:

  • Check prefix matches application (ADMIN vs API)
  • Ensure .env file is in project root
  • Restart watch command after changing .env
  • Verify no typos in variable names

Variables in wrong application:

  • Admin extensions can’t access WEBINY_API_ variables
  • API extensions can’t access WEBINY_ADMIN_ variables
  • Use correct prefix for the target application

Infrastructure Variables
anchor

For infrastructure extensions, access variables directly:

extensions/infrastructure/api.ts

Migration From V5
anchor

If upgrading from Webiny v5:

  1. Add prefixes to existing variables
  2. Update code to use prefixed names
  3. Test thoroughly in development