Skip to main content
0 reactions

How to Generate a Better Auth Secret

A Terminal window showing npx auth@latest secret printing Add the following to your .env file, followed by the line BETTER_AUTH_SECRET= with the secret hidden.
npx auth@latest secret prints a new secret for your .env file.

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

Bash
npx auth@latest secret
  • npx runs a package from npm without adding it to your project.
  • auth@latest is Better Auth’s command-line tool, the auth package, at its newest version.
  • secret is the command that prints a new random secret. It has no options.

The reference: Secret (opens in a new tab) 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:

Bash
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:

Plain text
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>
A Terminal window showing npx auth@latest secret printing Add the following to your .env file, followed by the line BETTER_AUTH_SECRET= with the secret hidden.
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:

Plain text
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

  • Keep it out of Git. Anyone with your secret can attack what it protects. A Next.js project made with create-next-app already lists .env files in .gitignore.
  • Use one secret per environment. Your computer and your live site each get their own, so a leak in one doesn’t expose the other.
  • Always set it. Without a secret, Better Auth falls back to a built-in default in development, and stops with an error in production.
  • Change it carefully. To move to a new secret without making older encrypted data unreadable, Better Auth supports several versioned secrets in BETTER_AUTH_SECRETS (the secrets option (opens in a new tab)).

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

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.

0 comments