Migrating from 4.0.5 to 4.0.6 guide: how ti use env with APP_KEYS?

System Information
  • Strapi Version: 4.0.6
  • Operating System: MacOS
  • Database: SQLite
  • Node Version: 14.8.3
  • NPM Version: 8.3
  • Yarn Version: 1.22.27

The migration guide here

mentions in the warning block to better use .env variables for app keys and provide this example:

APP_KEYS=[someSecret, anotherSecret, additionalSecrets]

However, it does NOT tell how to use those in the actual configuration, which looks like that in the docu:

// path: ./config/server.js

module.exports = ({ env }) => ({
  host: env('HOST', ''),
  port: env.int('PORT', 1337),
  app: {
    keys: env.array("APP_KEYS", ["testKey1", "testKey2"]),
  // ...

What is the proper way to use env variables in keys?

Would it be

keys: env.array("APP_KEYS", env(APP_KEYS)),


The documentation looks fine to me. I tried setting my own APP_KEYS in a .env file and it worked. Also tried without setting APP_KEYS in .env so it falls back to the hardcoded values in server.js and it works.

This wouldn’t be correct as the env.array() method takes a 2nd argument that is an Array. env(APP_KEYS) would return a String

So ["testKey1", "testKey2"] are fallback values in case APP_KEYS is not found in the env variables?

keys: env.array("APP_KEYS")

would therefore suffice?

Yes that’s right. As long as you have set APP_KEYS in your .env correctly you should be good

1 Like

Thank you for helping me understand, @jpizzle34 ! :slight_smile: