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
npxruns a package from npm without adding it to your project.auth@latestis Better Auth’s command-line tool, theauthpackage, at its newest version.secretis the command that prints a new random secret. It has no options.
The reference: Secret in the Better Auth CLI documentation, and the tool’s own help, npx auth@latest secret --help.
Prerequisites
- Node.js 22.12 or later, which the
authpackage needs. This guide uses Node.js 26 LTS from nodejs.org; check yours withnode -v. - a project that uses Better Auth, such as the Next.js app from Part 2 of the Better Auth Tutorial
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>
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
- 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
.envfiles 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).
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Putting .env in Git | The secret leaks with your code | Keep .env out of Git |
Making up a short secret, such as secret123 | It’s easy to guess | Use the command: Better Auth asks for at least 32 random characters |
| Using the same secret everywhere | One leak exposes every environment | Generate one per environment |
Leaving BETTER_AUTH_SECRET unset | Development uses a built-in default; production stops with an error | Set it in .env and in your host’s settings |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
npx asks Ok to proceed? (y) | It’s the first time you run the tool | Type 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
- Secret in the Better Auth CLI documentation
- Set environment variables in the Better Auth installation guide
- The secret and secrets options
Related
- Better Auth Tutorial — Part 2: Set Up Better Auth in Next.js with PostgreSQL, which uses this secret
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.