Motia Icon
Development Guide

Environment Variables

Store API keys and configuration safely using .env files in your Motia apps.

Environment Variables

Environment variables let you store API keys, database URLs, and other configuration outside your code. This keeps sensitive information secure and makes it easy to use different settings for development and production.

Quick Setup

1. Create a .env File

Create a .env file in your project root:

.env
# API Keys
OPENAI_API_KEY=sk-your-api-key-here
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/your-webhook
 
# Database  
DATABASE_URL=postgresql://user:password@localhost:5432/myapp
 
# App Settings
NODE_ENV=development
PORT=3000
 
# Redis (optional - only needed if using external Redis)
MOTIA_REDIS_HOST=localhost
MOTIA_REDIS_PORT=6379

2. Add to .gitignore

Make sure you never commit your .env file:

.gitignore
.env
.env.local

3. Create Template for Your Team

.env.example
# Copy this to .env and add your actual values
OPENAI_API_KEY=your-api-key-here
DATABASE_URL=postgresql://user:password@localhost:5432/myapp

Using Environment Variables in Steps

TypeScript/JavaScript

my-step.step.ts
export const config = {
  type: 'api',
  name: 'chat-with-ai',
  path: '/chat',
  method: 'POST'
}
 
export const handler = async (req, { logger }) => {
  // Use environment variables with process.env
  const apiKey = process.env.OPENAI_API_KEY
  const webhookUrl = process.env.DISCORD_WEBHOOK_URL
  
  if (!apiKey) {
    return { status: 400, body: { error: 'Missing API key' } }
  }
  
  logger.info('Using OpenAI API', { hasKey: !!apiKey })
  
  // Your logic here...
  return { status: 200, body: { message: 'Success!' } }
}

Python

my-step.step.py
import os
 
config = {
    'type': 'event', 
    'name': 'process-data',
    'subscribes': ['data.received']
}
 
async def handler(input_data, ctx):
    # Use environment variables with os.environ
    api_key = os.environ.get('OPENAI_API_KEY')
    database_url = os.environ.get('DATABASE_URL')
    
    if not api_key:
        raise ValueError('Missing OPENAI_API_KEY')
    
    ctx.logger.info('Processing with API key', {'has_key': bool(api_key)})
    
    # Your logic here...
    return {'status': 'processed'}

Redis Configuration

Motia uses Redis for internal coordination. By default, it includes an embedded in-memory Redis server for development, so you don't need to install or configure Redis separately.

Environment Variables

If you want to use your own Redis instance (created with --skip-redis flag or for production), you can configure it using these environment variables:

VariableDescriptionDefault
MOTIA_REDIS_HOSTRedis host127.0.0.1
MOTIA_REDIS_PORTRedis port6379
MOTIA_REDIS_PASSWORDRedis password-
MOTIA_REDIS_USERNAMERedis username (Redis 6.0+)-
MOTIA_REDIS_DBRedis database number0
MOTIA_DISABLE_MEMORY_SERVERDisable embedded Redisfalse

Example: Using External Redis

.env
# Connect to your own Redis instance
MOTIA_REDIS_HOST=redis.example.com
MOTIA_REDIS_PORT=6379
MOTIA_REDIS_PASSWORD=your-redis-password
MOTIA_REDIS_DB=0

Alternatively, you can configure Redis in your motia.config.ts file. See the CLI documentation for more details.

Deployment

When you deploy your app, set environment variables through your hosting platform:

Motia Cloud

motia env set OPENAI_API_KEY=sk-your-production-key
motia env set NODE_ENV=production

Important Security Tips

🔒 Keep Your Keys Safe

  • Never commit .env files to git
  • Use different API keys for development and production
  • Don't share API keys in code or messages

That's it! Environment variables are simple - just put them in .env and use process.env.VARIABLE_NAME in your code.

Need help? See our Community Resources for questions, examples, and discussions.

On this page

Environment Variables | motia