---
title: "Set up preview environments"
description: "Give a branch's preview build a backend of its own, with a generated auth secret, the branch's exact origin as SITE_URL and BACKEND_ENVIRONMENT set to preview, from the build or from the project's env defaults."
canonical_url: "https://nuxt-backend.dev/docs/ship/deploy/previews"
---
# Set up preview environments

> Give a branch's preview build a backend of its own, with a generated auth secret, the branch's exact origin as SITE_URL and BACKEND_ENVIRONMENT set to preview, from the build or from the project's env defaults.

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.

A preview build can have a backend of its own: a Convex **preview deployment** per branch, made by the same build command as production. The package gives each one the env its first push needs, with `BACKEND_ENVIRONMENT=preview`, so every provider stays in its test mode there. A preview never mails a real person or charges a real card.

Convex creates the preview deployment when the host's preview environment holds a *preview* deploy key. The package does not create one per pull request for you: it fills in the one Convex creates.

## The build command

Put a preview deploy key in the host's **preview** environment as `CONVEX_DEPLOY_KEY`. It comes from the Convex dashboard's project settings ([Convex](https://nuxt-backend.dev/docs/services/convex#keys-and-environment)). On Vercel, that is the project's **Preview** environment ([Vercel](https://nuxt-backend.dev/docs/services/vercel#sandbox-and-test-mode)). The production build command then creates, or reuses, a preview deployment named after the branch:

```bash
npx convex deploy --cmd 'npm run build' --cmd-url-env-var-name NUXT_PUBLIC_BACKEND_URL --preview-run 'billing:syncProducts'
```

`--preview-run` runs a function on the preview after the push, and only on a preview. Loading the products is the usual one, so the pricing page shows the sandbox catalog.

## Give each preview its env

A new preview starts with nothing but the project's preview env defaults, and its first push fails without `AUTH_SECRET` and `SITE_URL`. Two ways to have them there, which work together:

- **In the build, exactly.** `convex deploy` runs `--cmd` once the preview exists and before it pushes, so `env push --preview` can fill the env there, with the branch's own origin as `SITE_URL`. On Vercel, `VERCEL_BRANCH_URL` is the branch's stable URL, and the same build command also runs for production:```bash
npx convex deploy --cmd-url-env-var-name NUXT_PUBLIC_BACKEND_URL \
  --cmd 'if [ "$VERCEL_ENV" = preview ]; then npx nuxt-backend env push --preview --site-url "https://$VERCEL_BRANCH_URL"; fi && npm run build'
```

<br />

It generates `AUTH_SECRET` and sets `BACKEND_ENVIRONMENT=preview`. Without a name, it acts on the branch the build names, read as `convex deploy` reads it. It refuses a deploy key that is not a preview key.
- **Project env defaults, once.** Convex copies them into every preview it creates, so a preview built without that step can still push:```bash
npx nuxt-backend env push --defaults preview --site-url https://preview.example.com
```

<br />

It writes a generated `AUTH_SECRET`, the `SITE_URL` you name and `BACKEND_ENVIRONMENT=preview`. A default already set is kept unless `--force` names it. Every preview created from the defaults shares their one `AUTH_SECRET`.

`SITE_URL` is the one value neither will make up. Auth trusts only that origin, and a preview's origin is its own, one per branch on most hosts. So it has to come from the build, which knows it, or from you. A default cannot be exact for every preview: name a stable preview domain if your host gives you one, and let the build step replace it per branch. `doctor` reports the defaults as `env-defaults`.

## What a preview runs on

<table>
<thead>
  <tr>
    <th>
      Capability
    </th>
    
    <th>
      On a preview
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Auth
    </td>
    
    <td>
      Its own <code>
        AUTH_SECRET
      </code>
      
      , so no session from another deployment signs in there. Sign in with a test address, such as <code>
        delivered+you@resend.dev
      </code>
      
      , and read the code in the <a href="https://nuxt-backend.dev/docs/develop/dev-inbox">
        dev inbox
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Email
    </td>
    
    <td>
      Test mode: only Resend's test addresses receive mail, so a preview never writes to a real person
    </td>
  </tr>
  
  <tr>
    <td>
      Billing
    </td>
    
    <td>
      Polar's sandbox, with the sandbox catalog, once the preview holds a sandbox <code>
        BILLING_ACCESS_TOKEN
      </code>
      
      . Add it to the project's default environment variables for previews in the Convex dashboard, and pay with Polar's test card. Point a sandbox webhook at the preview's <code>
        /billing/events
      </code>
      
       only if the preview must see payments land
    </td>
  </tr>
  
  <tr>
    <td>
      AI
    </td>
    
    <td>
      The same models, through the Convex AI Gateway or <code>
        AI_GATEWAY_API_KEY
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Monitoring
    </td>
    
    <td>
      Reports name the <code>
        preview
      </code>
      
       environment, and its jobs never check in, so a preview deleted with its branch never reads as a missed run
    </td>
  </tr>
</tbody>
</table>

On Vercel, an app with nuxt-site-config (which `@nuxtjs/robots`, `@nuxtjs/sitemap` and `@nuxtjs/seo` install) also gets the preview's site from the module: the branch's stable URL (`VERCEL_BRANCH_URL`, else `VERCEL_URL`) for canonical links, whichever URL served the page, and `indexable: false`, so no preview is indexed. Both sit below the app's own `site` in `nuxt.config` and `NUXT_SITE_*`, which win. A production build is left as nuxt-site-config reads it: its pages name the host a visitor came in on.

Convex deletes a preview deployment, with its data, 5 days after it was created (14 days on the Professional plan and above). Nothing needs cleaning up on the package's side. A sandbox webhook you pointed at one is yours to remove.

## Related

- [Deploy to production](https://nuxt-backend.dev/docs/ship/deploy): the build command and the env split.
- [Deploy a demo or staging app](https://nuxt-backend.dev/docs/ship/deploy/staging): a long-lived deployment on the test modes.
- [Environments](https://nuxt-backend.dev/docs/start/concepts/environments): what each value of `BACKEND_ENVIRONMENT` turns on.
- [`env push`](https://nuxt-backend.dev/docs/reference/cli#preview-deployments): every flag, with the side effects of each.
- [Service map](https://nuxt-backend.dev/docs/services#provider-setup): the sandbox and production side of each service.


## Sitemap

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