How to Access and Use Environment Variables in n8n: A Complete Guide

n8n is a powerful, open-source workflow automation tool that allows users to connect various applications, APIs, and services without writing heavy code. As businesses grow and workflows become more complex, managing sensitive data such as API keys, database credentials, or configuration settings becomes critical. This is where environment variables come into play.

In this guide, we will dive deep into accessing and using environment variables in n8n. We’ll cover everything from the basics to advanced tips and tricks that will help you maintain secure, scalable, and flexible workflows.

What Are Environment Variables?

Environment variables are key-value pairs that are stored outside your application code. They allow you to configure your application dynamically without changing the code itself. In workflow automation tools like n8n, environment variables are especially useful for storing sensitive information such as API keys, database passwords, or configuration flags.

For example, instead of hardcoding a secret API key into a workflow node, you can store it as an environment variable and reference it when needed. This approach improves security, reduces the risk of exposing sensitive data, and simplifies configuration when deploying n8n in multiple environments (development, staging, production).

Why Environment Variables Matter in n8n

Environment variables offer several advantages:

  • Security: Keep sensitive data out of workflow nodes.
  • Portability: Move workflows between environments without changing code.
  • Scalability: Easily manage different configurations for multiple deployments.
  • Flexibility: Update configurations without touching workflows.

n8n workflows can grow in complexity. Using environment variables ensures you follow the best practices in automation design, especially for enterprise-level applications where security and maintainability are non-negotiable.

Setting Up Environment Variables in n8n

There are several ways to set environment variables for n8n. We’ll cover the most common methods.

1. Using a .env File

The easiest and most common approach is to create a .env file in your n8n project directory. The .env file stores key-value pairs in the format:

VARIABLE_NAME=valueANOTHER_VARIABLE=another_value

For example, to store an API key for a workflow, your .env file might look like:

N8N_API_KEY=123456789abcdefDB_PASSWORD=mySecretPassword

Once you create a .env file, n8n automatically reads these variables when you start the service.

2. Setting Environment Variables in Docker

If you run n8n using Docker, you can set environment variables directly in your docker-compose.yml file:

version: '3'services:  n8n:    image: n8nio/n8n    ports:      - "5678:5678"    environment:      - N8N_API_KEY=123456789abcdef      - DB_PASSWORD=mySecretPassword

This method ensures that your environment variables are automatically injected when the container starts, which is ideal for cloud or containerized deployments.

3. System Environment Variables

You can also set environment variables directly on your system before starting n8n. For example, on Linux or macOS, you can use the terminal:

export N8N_API_KEY=123456789abcdefexport DB_PASSWORD=mySecretPasswordn8n

This method is helpful when you need temporary variables for testing or debugging purposes without modifying files.

Accessing Environment Variables in n8n Workflows

Once you’ve set your environment variables, the next step is accessing them in your workflows. n8n provides multiple ways to use these variables efficiently.

1. Using Expressions

n8n allows you to reference environment variables using expressions inside any node that supports dynamic values. The syntax is:

{{$env.VARIABLE_NAME}}

For example, if you want to use an API key stored in N8N_API_KEY, you would write:

{{$env.N8N_API_KEY}}

This approach works in HTTP Request nodes, Set nodes, or any node that supports expressions.

2. Using Credentials

n8n credentials allow you to securely store authentication data. You can link environment variables to credentials, ensuring that sensitive information never appears directly in your workflow nodes.

For example, when creating a new HTTP Request credential, you can set the API key field to:

{{$env.N8N_API_KEY}}

This method ensures that the API key is centrally managed and easy to update without modifying individual workflows.

Advanced Tips for Using Environment Variables

1. Dynamic Environment Variables

Sometimes, you might need to reference different variables depending on the environment (e.g., dev vs. prod). You can use a naming convention and dynamic expressions:

{{$env["API_KEY_" + $env.NODE_ENV]}}

If NODE_ENV=PROD, this expression will resolve to API_KEY_PROD. This technique allows you to switch environments seamlessly without modifying workflows.

2. Keeping Secrets Secure

While environment variables are safer than hardcoding, you should still follow security best practices:

  • Store .env files outside version control (e.g., Git) using .gitignore.
  • Use encrypted storage for sensitive environment variables when possible.
  • Limit access to credentials to authorized personnel only.
  • Consider using secret management tools like HashiCorp Vault or AWS Secrets Manager for enterprise-level workflows.

3. Logging and Debugging Environment Variables

When debugging workflows, you might need to check if an environment variable is set correctly. You can log variables using the Set node or the Function node:

return [  {    json: {      apiKey: process.env.N8N_API_KEY    }  }];

This approach helps ensure your environment variables are loaded correctly and helps identify misconfigurations early.

Common Pitfalls and How to Avoid Them

Even experienced n8n users can make mistakes with environment variables. Here are some common pitfalls and how to avoid them:

  • Hardcoding sensitive values: Avoid putting passwords or API keys directly into workflow nodes.
  • Misnamed variables: Double-check spelling and capitalization; environment variables are case-sensitive.
  • Missing .env file: Make sure your .env file exists in the correct directory and is loaded before starting n8n.
  • Not restarting n8n: Changes in environment variables often require a restart of the n8n service to take effect.
  • Exposing secrets: Do not log sensitive variables in production environments unless necessary for debugging.

Practical Example: Using Environment Variables in an API Workflow

Let’s create a practical example. Suppose you have a workflow that calls a third-party API using an API key stored as an environment variable.

1. First, set the environment variable in your .env file:

API_KEY=abcdef123456

2. Create a new workflow in n8n and add an HTTP Request node.

3. In the node settings, set the Authorization header using the environment variable expression:

Authorization: Bearer {{$env.API_KEY}}

4. Execute the node. The request will automatically use the API key stored in your environment variable, keeping your workflow clean and secure.

This same approach can be extended to databases, cloud services, and other nodes that require credentials or configuration data.

Conclusion

Environment variables are a foundational concept for building secure, maintainable, and scalable workflows in n8n. By separating configuration from code, you can protect sensitive data, streamline deployment across environments, and reduce the risk of errors.

This guide covered the basics of environment variables, how to set them in different environments, how to access them in workflows, advanced tips for security and flexibility, and common pitfalls to avoid. By following these best practices, you’ll be able to build n8n workflows that are professional, secure, and future-proof.

Whether you’re managing a small project or an enterprise-level automation system, mastering environment variables in n8n is a skill that will save you time, prevent errors, and enhance workflow security.

Start leveraging environment variables today, and watch your n8n workflows become more robust and manageable than ever before.