Error: Middleware "strapi::session": App keys are required

System Information
  • Strapi Version: 4
  • Operating System: macos
  • Database:
  • Node Version: 18.12.1
  • NPM Version: 9.2.0
  • Yarn Version:

Hello everyone, I have a big problem, my hard drive crashed and I lost all my data. After changing my hard drive, I cloned my Strapi Next.js project but Strapi won’t launch with an error on the APP KEYS that I no longer have. How can I recover my Strapi admin? Thank you all.

1 Like

You will need to create your .env file if it’s a fresh project cloned down.
You just create random strings for the different values there should be an .env.example file you can look at in the project unless you deleted it

1 Like

Thank you so much ! It’s good !

Just so it’s a bit clearer for future searchers, as I ran into the same problem.
Create an .env file in the base directory of the project.
Add these values to it:
APP_KEYS={random-string-doesn’t-matter}
API_TOKEN_SALT={random-string-doesn’t-matter}

After this, everything works flawlessly.

1 Like

Hi, thanks for the solution. I have some comments that could improve the user experience though.

  • Couldn’t the .env file be automatically generated?
  • If not, it would be great to have an explanation on the terminal of what the user is supposed to do (create the .env file). Or at least a link to a strapi url that explains it. Having to copy the error, and paste it in google to find a solution is not a good user experience.
  • Also the explanation should be in the readme.md, as it will be quite common for developers working on the repo/project to run against this issue.
2 Likes

Hi @brsastre :open_hands: glad for the feedback :slight_smile:
Just to let you know, the env file is auto-generated when using the CLI tool not cloning it down. (you want to use the CLI tool to create your project). The user here had cloned down their own repo meaning the .env would not exist as it’s not to be committed to github.

Could be explained in the documentation indeed, feel free to make a PR here GitHub - strapi/documentation: Strapi Documentation and add some :slight_smile:

Should also be explained some information regarding .env and the environmental variables in our documentation.

Hi @Eventyret thank you for your feedback.
I think it would be quite common for more than one developer to be working on a strapi project. Suppose I created my strapi project with no problems, and I push the repo to github. Then developer-2 clones the repo and wants to start working on it.

What steps should developer-2 take to run strapi?

Create a .env file there should be an .env.exmaple that is committed.
And depending on your database if you are using sqlite for development then they only need to run yarn install or npm install then run it.

1 Like

@Eventyret Many thanks!

The issue should be also that the file environment name it isn’t the same;
just change .env.example → .env

1 Like

@Eventyret Are these APP_KEYS and API_TOKEN_SALT? So, should they be committed to the remote repository or not?

If you have a continuous deployment setup, you want this file to be there already, but you also don’t want to commit sensitive info.

The alternative would be to specify these environment variables in the pm2 configuration file, if you’re doing that, assuming this works (i.e. you just need to export these environment variables and it does not matter if this is done in the .env or somewhere else).

Moreover, I am in the process of migrating a Strapi 3.6.11 to Strapi 4.15. An APP_KEYS and API_TOKEN_SALT was generated at some point. Do I need to use this specific one in production or maybe this is not necessary?

so I created an .env file, and even hardcoded keys into server.js but app still asks to add them. What do I miss?

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  app: {
    keys: ['HDwsR5fCApWQcGf2J8zZ+bQRKYccLhBLsqslMORX2mQ=', 'AHXtsYcZLPeok24FTFcByH1/nMw8GDEaDssZWlHgZJI='],
  },
  webhooks: {
    populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
  },
});

Solved:
the problem was that .env file was without a dot so just env.