How to set App Keys asynchronously?

System Information
  • Strapi Version: 4.2.0-beta.2
  • Operating System: Mac OS Monterey
  • Node Version: v16.14.2
  • NPM Version: 8.5.4

Hello everybody,

Does anyone know how to set the APP_KEYS asynchronously?

My code is:

// index.ts

export default {
    async register({ strapi }) {
        const credentials = await getAwsSecrets();

        strapi.config.set("server", {
        host: credentials.HOST,
        port: credentials.PORT,
        app: {
          keys: credentials.APP_KEYS,
        },
      });
   }
}

// server.ts

export default ({ env }) => ({
  host: env("HOST", "0.0.0.0"),
  port: env.int("PORT", 1337),
  app: {
    keys: env.array("APP_KEYS"), 
  },
});

But this error is occurs:

debug: ⛔️ Server wasn't able to start properly.
error: Middleware "strapi::session": App keys are required. Please set app.keys in config/server.js (ex: keys: ['myKeyA', 'myKeyB'])
	at instantiateMiddleware (node_modules/@strapi/strapi/lib/services/server/middleware.js:12:11)
	at resolveMiddlewares (node_modules/@strapi/strapi/lib/services/server/middleware.js:56:18)
	at registerApplicationMiddlewares (node_modules/@strapi/strapi/lib/services/server/register-middlewares.js:66:29)
	at processTicksAndRejections (node:internal/process/task_queues:96:5)
	at async Object.initMiddlewares (node_modules/@strapi/strapi/lib/services/server/index.js:99:7)
	at async Strapi.bootstrap (node_modules/@strapi/strapi/lib/Strapi.js:436:5)
	at async Strapi.load (node_modules/@strapi/strapi/lib/Strapi.js:448:5)
	at async Strapi.start (node_modules/@strapi/strapi/lib/Strapi.js:196:9)

Did you have a solution for this yet? I ran into the same problem.

After messing with it for way longer than I hoped, you can set it directly on the object.

export default {
    async register ({ strapi }) {
        strapi.server.app.keys = ['your async keys'];
    }
}

for some reason you can’t set host/port through it, it updates the object but it seems to already be set or something? not sure. Regardless, you just need the server config file with the host and port set, and delete the default app_keys and everything works correctly.