Deployment3 min read

Environment variables

Why use environment variables?

Hardcoding secrets (API keys, database URLs, passwords) in your code is a security risk. Environment variables keep secrets out of your source code and let you use different values in development vs production.

Setting variables

  • 1.Open your project in the editor.
  • 2.Click the gear icon (Settings) in the toolbar.
  • 3.Select Environment.
  • 4.Add key-value pairs and click Save.
  • Variables are encrypted at rest and injected into your container at startup.

    Accessing variables in code

    Node.js / JavaScript:

    const apiKey = process.env.MY_API_KEY
    const dbUrl  = process.env.DATABASE_URL

    Python:

    import os
    api_key = os.environ.get('MY_API_KEY')
    db_url  = os.environ.get('DATABASE_URL')

    Next.js (public, browser-accessible):

    // Must be prefixed with NEXT_PUBLIC_
    const apiUrl = process.env.NEXT_PUBLIC_API_URL

    Changes take effect

    Changes to environment variables require a container restart to take effect. Click Restart Container in the toolbar after saving, or stop and start your process in the terminal.

    .env files

    You can also create a .env file in your project root and use dotenv:

    npm install dotenv
    require('dotenv').config()
    console.log(process.env.MY_KEY)
    Never commit .env files. Add .env to your .gitignore and use the Settings panel for secrets that need to survive across container restarts.