Search code examples
herokuheroku-cliheroku-pipelines

How can I have separate APIs for staging and production environments on Heroku?


I was just checking on how pipelines work in Heroku. I want the staging and production apps to be the same except that they should access different API endpoints.

How could I achieve that?


Solution

  • Heroku encourages getting configuration from the environment:

    A single app always runs in multiple environments, including at least on your development machine and in production on Heroku. An open-source app might be deployed to hundreds of different environments.

    Although these environments might all run the same code, they usually have environment-specific configurations. For example, an app’s staging and production environments might use different Amazon S3 buckets, meaning they also need different credentials for those buckets.

    An app’s environment-specific configuration should be stored in environment variables (not in the app’s source code). This lets you modify each environment’s configuration in isolation, and prevents secure credentials from being stored in version control. Learn more about storing config in the environment.

    On a traditional host or when working locally, you often set environment variables in your .bashrc file. On Heroku, you use config vars.

    In this instance you might use an environment variable called API_BASE that gets set to the base URL of your staging API on your staging instance and to the base URL of your production API in production.

    Exactly how you read those values depends on the technology you're using, but if you look for "environment variables" in your language's documentation you should be able to get started.