---
title: "Rotate secrets"
description: "`nuxt-backend env rotate` replaces the auth secret, the session signing keys and the webhook secrets without signing anyone out or dropping a delivery: the new value beside the old, then the old retired."
canonical_url: "https://nuxt-backend.dev/docs/ship/security/secrets"
---
# Rotate secrets

> `nuxt-backend env rotate` replaces the auth secret, the session signing keys and the webhook secrets without signing anyone out or dropping a delivery: the new value beside the old, then the old retired.

Version: nuxt-backend 0.6.x · Tab: Ship · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/cli/rotate.ts)

A secret that leaked, or one that got old, has to change without taking the app down. `npx nuxt-backend env rotate <name>` changes every secret the package reads the same way: the new value goes in beside the old, both are accepted for a while, then the old one is retired. Nobody is signed out, no emailed link breaks while both values are accepted, and no webhook delivery is lost: the one billing delivery that can be refused in between is retried.

```bash
npx nuxt-backend env rotate AUTH_SECRET                  # the dev deployment
npx nuxt-backend env rotate EMAIL_WEBHOOK_SECRET --prod  # production
npx nuxt-backend env rotate BILLING_WEBHOOK_SECRET --prod --dry-run
```

It writes the deployment and, for the webhooks, your provider accounts. It names each step as it takes it, and `--dry-run` names them and changes nothing. No value is ever printed: each one reaches `convex env set` on its stdin. Every flag is on the [CLI page](https://nuxt-backend.dev/docs/reference/cli#env-rotate).

## What rotates, and how

<table>
<thead>
  <tr>
    <th>
      Secret
    </th>
    
    <th>
      How it rotates
    </th>
    
    <th>
      Who notices
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        AUTH_SECRET
      </code>
    </td>
    
    <td>
      Versioned: the new value goes in beside the old and signs everything from then on. The old one keeps verifying for 30 days, then a second run retires it. Unsubscribe links already in inboxes keep verifying through those 30 days, as long as the law asks such a link to work
    </td>
    
    <td>
      Nobody: sessions, codes in flight and emailed links stay valid. A device that signs in during the 30 days stays known; one unseen for the whole window sends one new-device alert on its next sign-in
    </td>
  </tr>
  
  <tr>
    <td>
      The session signing keys
    </td>
    
    <td>
      Rotate with <code>
        AUTH_SECRET
      </code>
      
      , with no downtime: the next session token is signed with a new key, encrypted with the new secret, and the old key stays published until the tokens it signed expire
    </td>
    
    <td>
      Nobody
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        EMAIL_WEBHOOK_SECRET
      </code>
    </td>
    
    <td>
      Guided. The command rotates the endpoint's signing secret through Resend's API, from the deployment with its own key, and stores the new one there. For 24 hours, Resend signs every delivery with both the new and the old secret, so the deployment's copy can change at any moment of that day
    </td>
    
    <td>
      Nobody: no delivery is refused
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        BILLING_WEBHOOK_SECRET
      </code>
    </td>
    
    <td>
      Guided. The command resets the endpoint's secret through Polar's API, which returns the new one and signs with it at once, and writes <code>
        new,old
      </code>
      
       on the deployment right away. A delivery refused in between is retried by Polar. The command then drops the old value
    </td>
    
    <td>
      Nobody: a refused delivery is retried
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        PROTECTION_PROOF_SECRET
      </code>
    </td>
    
    <td>
      Guided. <code>
        new,old
      </code>
      
       on the deployment, then the new value in the app's env and a redeploy of the app, then the new value alone on the deployment (<a href="https://nuxt-backend.dev/docs/services/bot-protection#go-to-production">
        Bot protection
      </a>
      
      )
    </td>
    
    <td>
      Nobody: no request is refused in between
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        PUSH_PUBLIC_KEY
      </code>
      
      , <code>
        PUSH_PRIVATE_KEY
      </code>
    </td>
    
    <td>
      Only after a leak. A new key pair ends every push subscription, and each browser subscribes again on its next visit. The push keys are never derived from <code>
        AUTH_SECRET
      </code>
      
      , so rotating that never touches them
    </td>
    
    <td>
      Everyone with push on: notifications stop until their next visit
    </td>
  </tr>
</tbody>
</table>

The provider tokens (`BILLING_ACCESS_TOKEN`, `EMAIL_API_KEY`, `AI_GATEWAY_API_KEY`) need none of this. Make a new one in the provider's dashboard, set it on the deployment with `npx nuxt-backend env push --force <NAME>`, and revoke the old. Each [service page](https://nuxt-backend.dev/docs/services) says where its keys live. The functions read the env at call time, so the backend needs no redeploy. `env rotate` with one of these names, or with `PROTECTION_PROOF_SECRET` or a push key, refuses and names these steps.

## Rotate `AUTH_SECRET`

```bash
npx nuxt-backend env rotate AUTH_SECRET --prod
```

1. **The new value goes in beside the old.** The command reads the deployment's `AUTH_SECRET` and writes a versioned value, current first: `<version>:<new>,<version>:<old>`. A version is the Unix second its secret began signing, and the plain secret an app starts with becomes version `0`. The auth library takes the list as its own versioned secrets, and every link and keyed value the package signs verifies with either.
2. **The session signing keys rotate.** Every key that still signs is marked expired. The next token is signed with a new key, encrypted with the new secret; the old keys stay published for 30 days, so a token signed a moment before stays valid until it expires.
3. **Thirty days later, the old value retires.** Run the same command again. It removes the expired signing keys first, so none is left encrypted with the secret about to go, then writes the current value alone. A run before the window ends refuses and says when it ends.

A session cookie signed with the old value is re-signed on its way in while the old value verifies, and the next session refresh writes a cookie signed with the new one. A sign-in code is hashed with the oldest value the deployment holds, so a code sent a minute before the rotation still works.

The old value retires early with `--retire`, after a leak, when it should stop working at once rather than after the window. The command then rotates and retires in one run: sessions, links and codes the old value signed stop working with it, and everyone signs in again.

A passkey recovery code is stored as a plain SHA-256 hash, keyed by nothing, so no rotation touches it: a code issued years ago still recovers its account. An invite code checked during the window moves to the new value, so it keeps working after the retirement. An invite code or access link unused through the whole window stops working at the retirement, and its holder asks for a new one: it is kept only as a hash keyed with the old value. Before it retires the old value, with or without `--retire`, the command counts the open codes and links still under it and warns with that number. An unsubscribe link the old value signed stops with it too, at least 30 days after it was sent, as long as the law asks such a link to work.

If `.env.local` still holds the old plain `AUTH_SECRET`, nothing pushes it over the deployment's versioned value, but `env push --force AUTH_SECRET` would. The command says so: remove it from `.env.local`.

## Rotate a webhook secret

```bash
npx nuxt-backend env rotate EMAIL_WEBHOOK_SECRET --prod
npx nuxt-backend env rotate BILLING_WEBHOOK_SECRET --prod
```

The email webhook's secret rotates at the provider through the deployment's own `email:syncEmail`, with the deployment's `EMAIL_API_KEY`, so the key never leaves it. It needs a full-access key and the webhook `email sync` made.

The billing endpoint's secret is reset with the `BILLING_ACCESS_TOKEN` visible where the command runs: `.env.local` for the dev deployment, the process env alone under `--prod` (a CI secret, or `vercel env pull`). The endpoint is the one `billing sync` made for the deployment's `/billing/events`, and the deployment must already hold its secret.

## Why it is safe

A test in the package holds the promise for what `AUTH_SECRET` signs. After a rotation against the real auth library, a session cookie, a session token and a signing key stored before it, and an unsubscribe token issued before it, all still verify. A change that would sign people out fails that test before it can ship.

## Related

- [Security model](https://nuxt-backend.dev/docs/ship/security#secret-rotation): what each secret protects.
- [Respond to incidents](https://nuxt-backend.dev/docs/ship/operate/runbooks#a-secret-leaked): what to do first when a secret leaks.
- [Troubleshooting](https://nuxt-backend.dev/docs/ship/operate/troubleshooting#sessions-fail-after-changing-auth_secret-by-hand): sessions after an `AUTH_SECRET` changed by hand.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
