How do I programmatically check which environment the app is in?

In the runtime / app editor

One way to accomplish this is to write a function in your shared JS as follows:

function sharedEnv() {
    if(journey && journey.server) {
        return journey.server  
    } else return null;
}

This will return the url of the environment, e.g. https://run-testing.journeyapps.com.

To make this useful, you can use the following function in your view to determine if you are in e.g. testing or staging.

 if(sharedEnv().indexOf('testing') > -1) {
    //do logic or show a button for testing only
 }

In CloudCode

CloudCode also exposes the environment in a variable, but directly:

export async function run() {
    //returns the evironment the function is running in; testing, staging, production
    console.log(`${this.env}`)
}

The above function returns the following:

![The environment is returned (red box added for emphasis)](upload://7FjsUcHX1EcLOJwBPXmWunUFhBe.png)

3 Likes