# Preview environments (/docs/starter/deploy/preview-environments)

Projects with `--infra neon` (Starter v0.19.0 or newer) have `.github/workflows/preview.yml`. For each pull request, it:

1. creates a [Neon branch](https://neon.com/docs/introduction/branching), `preview/pr-<number>`: a copy of the production branch made in seconds, which shares its storage until it changes. Later pushes reuse it;
2. applies the pull request's migrations to the branch;
3. comments the schema diff between the branch and production on the pull request, so a reviewer sees each table and column the pull request changes;
4. deploys a preview against the branch on Vercel or Railway, once that platform's token is set;
5. posts one comment with the app's stack (such as Next.js, or React + Vite + Hono (TypeScript)), the database branch, and the preview URLs, updated on each push;
6. deletes the branch and the Railway environment when the pull request is merged or closed.

A new push replaces the preview being made, and closing waits for it, so nothing is left behind. Pull requests from forks and from Dependabot get no preview, since GitHub gives them no secrets (Dependabot's are skipped from Starter v0.24.1 on).

## Turn it on [#turn-it-on]

In GitHub, open the repository's **Settings**, then **Secrets and variables**, then **Actions**:

| Name | Kind | Value |
| --- | --- | --- |
| `NEON_API_KEY` | Secret | An API key from the Neon console: **Account settings**, then **API keys** (or an organization's). |
| `NEON_PROJECT_ID` | **Variable** | The project's ID, from its **Settings**, then **General**. |

<Callout type="warn">
  `NEON_PROJECT_ID` goes under **Variables**, not Secrets: the workflow reads it to decide whether to run. A free database the CLI created must be [claimed](../../cli/neon.mdx) first, since only a Neon account has an API key.
</Callout>

With these two, every pull request gets its branch, migrations, and schema diff. The preview deploy waits for its platform's settings.

## The preview deploy [#the-preview-deploy]

<Tabs items={['Vercel', 'Railway']}>
  <Tab value="Vercel">
    | Name | Kind | Value |
    | --- | --- | --- |
    | `VERCEL_TOKEN` | Secret | A token from your account settings. |
    | `VERCEL_ORG_ID`, `VERCEL_PROJECT_ID` | Secrets | `orgId` and `projectId` from `.vercel/project.json`, which `pnpm clubedge deploy` or `vercel link` writes. |

    The preview is a Vercel preview deployment with the project's Preview variables, except `DATABASE_URL` and `DATABASE_URL_DIRECT`, which point at the pull request's branch. Set the Preview variables your modules need, or `pnpm check:env` stops the build. On a team, Vercel's deployment protection asks visitors to sign in to Vercel; turn it off in the project's **Settings**, then **Deployment Protection**, to share previews.
  </Tab>
  <Tab value="Railway">
    | Name | Kind | Value |
    | --- | --- | --- |
    | `RAILWAY_API_TOKEN` | Secret | An account or workspace token. A project token can't create environments. |
    | `RAILWAY_PROJECT_ID` | Variable | The project's ID, from its **Settings**. |
    | `RAILWAY_SERVICE` | Variable | The app's service name, which `pnpm clubedge deploy` names after the package. |

    Each pull request gets a Railway environment, `pr-<number>`, copied from production, with its own domain, `PORT=3000`, and the pull request's branch as its database. It is deleted with the branch. Railway bills each environment's usage while the pull request is open. Use Starter v0.19.1 or newer: earlier releases failed with a workspace token, and their previews answered 502.

    In a project [without a website](../../cli/without-a-website.mdx) (Starter v0.28.0 or newer), the environment is the API's: `RAILWAY_SERVICE` names `<package>-api`, `PORT` is 3001, and with Better Auth, `BETTER_AUTH_URL` is the preview's own domain.
  </Tab>
</Tabs>

Without either platform's settings, pull requests still get their database branch, migrations, and schema diff.

## From your machine [#from-your-machine]

[`pnpm clubedge deploy --preview`](../../cli/deploy/preview.mdx) deploys the current Git branch the same way, without a pull request. It deploys a web app only, so a project without a website gets its previews from this workflow.
