Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.


Folders and files

Last commit message
Last commit date

Latest commit



27 Commits

Repository files navigation

heroku-env Gem Version RSS

Don't worry about the environment.

This simple gem makes it easier for you to use different Heroku addons in a plug-and-play fashion. Normally, if you decide to add a Redis provider, you have to customize your code to use that addon's environment variables. The problem with this, however, is that if you want to switch provider, you have to update your code.

What I usually ended up doing was:

$redis =

Doing this in every project started to become a little annoying, so I decided to make a gem.

The gem only supports addons with a free plan. The order that the addons are selected depends on how stable and feature-rich they are. So if you are deciding what provider you want to use, a good choice is probably the first one in the lists below.

The gem uses ||= in all the assignments, so you can easily override any variable by manually setting it.


Add to your Gemfile:

gem "heroku-env", "~> 0.1.0"

If you use dotenv-rails, then you should put this gem lower than it in the Gemfile.

If you use Rails, then you should not need to do anything more, the gem has a Railtie hook which will do the initialization.

If not, then you might have to run this manually:

require "heroku-env"

This gem is cryptographically signed, you can verify the installation with:

gem cert --add <(curl -Ls
gem install heroku-env -P HighSecurity


Nowadays Heroku is offering their own redis solution called Heroku Redis which I recommend unless you have specific requirements. It populates REDIS_URL so this gem is not required.

Supports Redis Cloud and Redis To Go.

$redis =


Supports MemCachier and Memcached Cloud.

$dc =


Supports MongoLab and MongoSoup.

$mongo = ENV["MONGO_URL"]


Supports ClearDB and JawsDB.

config = URI.parse ENV["MYSQL_URL"]
$mysql =
  host:     config.hostname,
  port:     config.port,
  username: config.user,
  password: config.password,
  database: config.path.sub(%r{^/},""),
  reconnect: true


Supports Graph Story and GrapheneDB.

$neo4j = :server_db, ENV["NEO4J_URL"]


Supports Bonsai and SearchBox.

$elasticsearch =


Supports Mandrill, SendGrid, Mailgun, Postmark and SparkPost.

Mail.defaults do
  delivery_method :smtp,
    address:              ENV["SMTP_HOST"],
    port:                 ENV["SMTP_PORT"],
    user_name:            ENV["SMTP_USERNAME"],
    password:             ENV["SMTP_PASSWORD"],
    authentication:       "plain",
    enable_starttls_auto: true

Then send an email with:

Mail.deliver do
     from "Sender <#{ENV["MAIL_FROM"]}>"
       to ""
  subject "Test email"
     body "Just testing heroku-env"

If you use Postmark, you have to manually set MAIL_FROM.


If you use Rails, you might want to use this:

ActionMailer::Base.smtp_settings = {
  address:        ENV["SMTP_HOST"],
  port:           ENV["SMTP_PORT"],
  user_name:      ENV["SMTP_USERNAME"],
  password:       ENV["SMTP_PASSWORD"],
  authentication: :plain,


Supports Airbrake, Raygun and Rollbar.

Airbrake.configure do |config| = ENV["AIRBRAKE_HOST"]
  config.api_key = ENV["AIRBRAKE_KEY"] = true

Another Airbrake-compatible provider is AppEnlight, it is free but they don't provide a Heroku addon. You can manually set APPENLIGHT_APIKEY to use it.

Misc notes

  • Postmark sends you some unwanted emails if you add the addon.