1 minute reading time
Heroku Ruby Sinatra
Integrations

Heroku with Sinatra

Learn how to make Heroku, Sinatra, and Dotenv Vault work together. This tutorial assumes you have already created a .env file and synced it.

Set up Sinatra and require dotenv-vault

Set up your Sinatra app to work with Heroku and require dotenv-vault. Require it as early as possible in your sinatra application.

# server.rb
require 'sinatra'
require 'dotenv-vault/load'

get '/' do
  "Welcome to #{ENV["HELLO"]}"
end

Example shows using environment variable example

// Add 'dotenv-vault-rails' to Gemfile
gem 'dotenv-vault-rails'

example

Run bundle install.

$ bundle install

Run dotenv-vault build

On your terminal at root of your project run npx dotenv-vault build to build your encrypted .env.vault file.

$ npx dotenv-vault build

Get DOTENV_KEY

Run npx dotenv-vault keys production.

$ npx dotenv-vault keys production
remote:   Listing .env.vault decryption keys... done

dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=production

Set DOTENV_KEY

Visit your Heroku Project’s Environment Variables under settings of your heroku application

Set DOTENV_KEY to the value returned in step 4.

Commit and push

That’s it!

Commit those changes safely to code and deploy to Heroku.

When the build runs, it will recognize the DOTENV_KEY, decrypt the .env.vault file, and load the production environment variables to ENV. If a DOTENV_KEY is not set (like during development on your local machine) it will fall back to regular dotenv.

Updated Saturday (Nov 26)