GitHub iconEdit on GitHub

Environment Variables

Environment variables can be managed via wrangler or in the UI, it is recommended to adhere to one method.

Text

Once a text variable is uploaded via wrangler or in the UI, the string is exposed on the global namespace as type String.

if (ENVIRONMENT === "staging") {
  // staging-specific code
} else if (ENVIRONMENT === "production") {
  // production-specific code
}

Secrets

Once a secret is uploaded via wrangler or in the UI, the string is exposed on the global namespace as type String.

In the below example, we set the secret SECRET using wrangler secret, and then use it in our script as part of the Authorization header.

Creating the secret using wrangler secret:

wrangler secret put SECRET
Enter the secret text you'd like assigned to the variable name on the script named my-worker-ENVIRONMENT_NAME: mysekret

Using the secret inside of your Workers script:

let headers = new Headers({ Authorization: SECRET })

Limits for environment variables can be seen in the about section.