How can I put the values of an environment variable inside a CustomAuth

I’ve been trying to put the values ​​of a credential into the env file of the self-hosted n8n. Within the env file, I put MY_CREDENTIAL=“xxxxxx.” After that, I tried creating a CustomAuth:
{
“headers”:{
“token”:“{{$env.MY_CREDENTIAL}}”,
“Content-Type”:“application/json”
}
}
Well, it just doesn’t work. Any tips?

Try:
“token”:“={{$env.MY_CREDENTIAL}}”

If that doesn’t work; please follow these full instructions:

In n8n self-hosted, environment variables must be declared before the container starts, and must not use quotes or fancy characters like smart quotes (“ ” instead of " "). Also, inside credentials, you can’t directly use $env.MY_CREDENTIAL unless n8n flags are enabled for credential environment access.

:white_check_mark: Correct way:

1. In your .env file (no quotes, no trailing period):

MY_CREDENTIAL=xxxxxx
N8N_ALLOW_ENV_VARIABLES=true

(If using docker-compose, make sure the env file is actually loaded before the container runs — restart after editing!)

2. Use this syntax in the Custom Auth:

{
  "headers": {
    "token": "={{ $env.MY_CREDENTIAL }}",
    "Content-Type": "application/json"
  }
}

:check_mark: Note the syntax: ={{ $env.MY_CREDENTIAL }} and not {{$env...}}.


:police_car_light: Common Issues to Check:

Issue Fix
You used “curly quotes” instead of " Replace them manually
You restarted n8n after editing .env Required
N8N_ALLOW_ENV_VARIABLES not set Must be true
.env not actually being read (docker-compose missing env_file) Add env_file: .env
Using wrong expression syntax Must start with ={{ ... }}

:counterclockwise_arrows_button: Quick test:

Create a Function node and do:

return [{ test: $env.MY_CREDENTIAL }];

If the output is undefined, your container is not seeing the env var.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.