Deploy a Strapi API on Heroku in 5 min

@beschler,

You can add an entry to the .env file for APP_KEYS instead of hardcoding in the server.js file. Worked for me. But you only need one key for the APP_URL per documentation.

So:
config/env/production/server.js

module.exports = ({ env }) => ({
  url: env('APP_URL'),
  proxy: true,
  app: {
    keys: env.array('APP_KEYS')
  }
});

.env:
APP_KEYS=ENTER-YOUR-SINGLE-APP-KEY-VALUE

And finally, add the APP_KEYS value also to Heroku config vars

1 Like

Hey guys. I followed all your tips to have my Strapi deployed with Heroku. But each time I push a commit, all the content is lost. And I need to register my account, again and again, there’s no login page but only a sign-up one. Do I miss something? Does anyone have a solution?

for me it was missing some Config Vars

The following openssl commands will generate random new secrets (Mac and Linux only):

heroku config:set APP_KEYS=$(openssl rand -base64 32)
heroku config:set API_TOKEN_SALT=$(openssl rand -base64 32)
heroku config:set ADMIN_JWT_SECRET=$(openssl rand -base64 32)
heroku config:set JWT_SECRET=$(openssl rand -base64 32)

Hey you did not tell explicitly where to find postgres database credentials. Just go there https://data.heroku.com/

This tutorials isn’t up to date one the last part Adding Environment Variables to Strapi

After creating the file ./config/env/production/database.js you need also :

  • Create your Strapi server config for production. Create file ./config/env/production/server.js and add this code
module.exports = ({ env }) => ({
    proxy: true,
    url: env('MY_HEROKU_URL'),
    app: { 
      keys: env.array('APP_KEYS')
    },
  })
  • After that you need to add variables needed, you can do this manually or with the Heroku CLI
heroku config:set MY_HEROKU_URL=$(heroku info -s | grep web_url | cut -d= -f2)
heroku config:set APP_KEYS=$(cat .env | grep APP_KEYS | cut -d= -f2-)
heroku config:set API_TOKEN_SALT=$(cat .env | grep API_TOKEN_SALT | cut -d= -f2)
heroku config:set ADMIN_JWT_SECRET=$(cat .env | grep ADMIN_JWT_SECRET | cut -d= -f2)
heroku config:set JWT_SECRET=$(cat .env | grep -w JWT_SECRET | cut -d= -f2)
  • And don’t forget to set your production environment - same, manually or with CLI
heroku config:set NODE_ENV=production

After that enjoy !

You can find this on the Strapi doc (at the 7th bullet point - not show in summary little bug I presume) Strapi doc

it works for me, thanks.

but, there is a way to work with different .env files local and production in heroku?

Also worked for me thanks ! Had to reload all dino after and done !

Just spent couple hours on this. Sadly nothing worked. Still the exact same error.

After lots of reading…it would seem the docs just missed a key note regarding the setup of the Config Variables within Heroku. Strapi requires the variables that exist within the .env file on your localhost, and these obviously aren’t pushed to the repo due to .gitignore for security purposes.

Head into the settings on your Heroku app, and add these four variables from your local .env file to your Config Variables area with their values.

APP_KEYS
API_TOKEN_SALT
ADMIN_JWT_SECRET
JWT_SECRET

It should then compile successfully. Hope that helps anyone!

Thanks for the heads up everyone. There were some changes in a couple releases. I would recommend reading our releases for changes to deployment. We’re also much better now at updating docs when changes like this happen. Appreciate all the people who stepped up to help out with solutions.

I’ve seen a couple things come up that I want to address.

  1. Please don’t hard code your APP_KEYS to your server.js file. That’s a potential security risk. If your Heroku deployment fails because of missing config please check the docs on how to generate and push some to your Heroku app.

  2. Your local database and database on Heroku aren’t connected. So changes you make locally to content won’t reflect in production, at the moment you would have to enter those yourself. We are working something to fix that, do share your interest for such a feature on the product board.

I’ve also updated the article to reflect the changes (might take a bit or changes to reflect but they’ll come). Take care!

hi, so how to update content? Seems like first i need to create collections using my superadmin account on localhost, add some data for testing environment. but then also need to copy paste all date into another strapi database (created for production), Production database shows new collections but no data entry. Is this the case? — OK i just saw the post above and it answered by question

I am not following in regards to the SQlite/PostgresSQL issues. What do you mean we need to use PostgresSQL to successfully deploy strapi to heroku? I understand the default database is SQlite. Now you want us to use PostgresSQL instead. Does this process automatically configure the queries for PostgresSQL just like SQlite did? Or do we have to manually configure those queries in our Strapi repo? Because if so, then that is a HUGE red flag which will result us in migrating to a different headless CMS.