How can I get the name of the current environment in my code?

I have two environments, one named staging and the other named production. I would like my code to behave differently when it runs in staging vs production. How can I do that?

1 Like

Officially the way to do this is to create an environment variable and give it a different value in each environment. The guide on environment variables is here: https://developer.moovweb.com/guides/environments#section_environment_variables

For example, suppose you a variable called API_SERVER which controls which API server your code should use. In the staging environment you would set it to staging.example.com via the Moovweb.app console and in the production environment you would set it to prod.example.com. Then in your server side Javascript code you could do:

const apiServer = process.env["API_SERVER"];

// the rest of your code to talk to apiServer

Unofficially, there is a predefined undocumented environment variable called XDN_ENVIRONMENT_NAME that you can use which contains the name of the current environment.

1 Like

XDN_ENVIRONMENT_NAME is now documented and considered public:

https://developer.moovweb.com/guides/environments#section_built_in_environment_variables