You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

Learning about Environments from Code

In a typical application, the same source code will run in several different environments – a developer’s personal debug environment, then perhaps a testing environment, then production. It might need to behave differently in different environments: for example, if it is generating links to its own HTTP endpoints, those links should include the URL for the current environment. As another example, an application that connects to an external database server might connect to a testing database in debug and testing environments, and a production database in production.

In order to “ship what we tested”, we shouldn’t need to change our code between each of these stages (for example, changing the database connection string before each production deploy). Instead, our code should dynamically change its behaviour, based on the environment it is in.

Getting the app’s URL

You can get your app’s root URL programmatically using anvil.server.get_app_origin(). This will return the URL for the environment you are currently running in.

If you wish to programmatically construct URLs pointing at a different environment (for example, your app’s main production environment), hard-code a constant containing your custom domain and construct URLs from that.

Getting the current environment

The object represents the current environment. You can retrieve the environment’s name with

If your application contains configuration values (such as database connections) that should be different between environments, we recommend creating a Module (or Server Module) with global constants for all of these configuration values, then centralise the code for changing them based on environment. For example:

if == "Production":
    DB_URL = "postgres:prod_database"
    DB_PASSWORD = anvil.secrets.get_secret("db_pw_prod")
elif == "Staging":
    DB_URL = "postgres:staging_database"
    DB_PASSWORD = anvil.secrets.get_secret("db_pw_staging")
    # Probably a personal debug environment
    DB_URL = "postgres:test_database"
    DB_PASSWORD = anvil.secrets.get_secret("db_pw_test")

Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.