Motia Icon
Concepts

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

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'}

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