Site icon JD Bots

How to Generate a Better Auth Secret

Better Auth needs a secret: a long, random value it uses to encrypt and sign data, such as your users’ session cookies. This guide generates a Better Auth secret with one command from Better Auth’s own command-line tool, and stores it where Better Auth finds it by itself.

The short answer: run npx auth@latest secret in your project folder, and paste the BETTER_AUTH_SECRET=… line it prints into your .env file.

Syntax

npx auth@latest secret

The reference: Secret in the Better Auth CLI documentation, and the tool’s own help, npx auth@latest secret --help.

Prerequisites

Step 1: Generate the Secret

Open Terminal (PowerShell on Windows) in your project folder, and run:

npx auth@latest secret

The first time, npx asks Ok to proceed? (y) before it downloads the tool: type y and press Return. The tool prints a line for your .env file. Every run prints a new secret, and yours is a different 64-character value, so it’s hidden here:

npm notice run bookshop@0.1.0 npx
npm notice run 'auth' secret
Add the following to your .env file:
BETTER_AUTH_SECRET=<your-secret>
npx auth@latest secret prints a new secret for your .env file.

Step 2: Put the Secret in .env

Open the .env file in your project folder, or create it, and paste the line the tool printed:

BETTER_AUTH_SECRET=<your-secret>

Better Auth reads BETTER_AUTH_SECRET by itself, so you don’t pass the secret in your code. Restart your app if it’s running, so it reads the new value.

Keep the Secret Safe

Common Mistakes

MistakeWhat happensDo this instead
Putting .env in GitThe secret leaks with your codeKeep .env out of Git
Making up a short secret, such as secret123It’s easy to guessUse the command: Better Auth asks for at least 32 random characters
Using the same secret everywhereOne leak exposes every environmentGenerate one per environment
Leaving BETTER_AUTH_SECRET unsetDevelopment uses a built-in default; production stops with an errorSet it in .env and in your host’s settings

Troubleshooting

ProblemCauseFix
npx asks Ok to proceed? (y)It’s the first time you run the toolType y and press Return

FAQ

Can I use openssl instead?

Yes. Better Auth’s documentation also suggests openssl rand -base64 32. The auth command works the same in Terminal and PowerShell, so this guide uses it.

Does the secret ever leave my computer?

It’s generated on your computer and printed in your Terminal. Only you copy it into .env and, later, into your host’s settings.

What if my secret leaks?

Generate a new one and replace the old value. Use BETTER_AUTH_SECRETS if older encrypted data must stay readable while you switch.

Conclusion

npx auth@latest secret gives you a strong Better Auth secret in one step. Put it in .env as BETTER_AUTH_SECRET, keep it out of Git, and use a different one for each environment.

Additional Resources

Related

Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.

Exit mobile version