# Railway (/docs/starter/deploy/railway)

`.railway/railway.ts` is the service's configuration on [Railway](https://railway.com), as code. A project with a frontend and its API runs two services: see [below](#a-frontend-and-its-api). A project without a website runs its API alone: see [Without a website](#without-a-website).

<Callout>
  [`pnpm clubedge deploy`](../../cli/deploy/railway.mdx) does these steps for you: the project, Postgres, the service, its domain and variables.
</Callout>

## What .railway/railway.ts sets [#what-railwayrailwayts-sets]

- **Build:** Railway builds the root `Dockerfile`.
- **Pre-deploy:** `node /app/migrate.mjs` runs before each release, so a failed migration stops it.
- **Health check:** `/api/health`, checked before Railway switches traffic, with a 60-second timeout.
- **Variables** stay managed by `deploy`, `railway variable set`, or the dashboard. Set `CLIENT_IP_HEADER=x-real-ip` on the public service, where Railway's edge puts each [client's address](../before-deploying.mdx#client-addresses); `deploy` sets it for you (CLI 0.42.0 or newer).

It replaced `railway.json`, which Railway stops reading on 2026-12-01. The file names the service after the package; if yours has another name, change it there.

## Deploy by hand [#deploy-by-hand]

Add Railway's TypeScript SDK, which the file imports, then apply it once with Railway CLI 5.42.1 or newer:

```sh
pnpm add -D -w railway
railway login
railway link                  # or railway init for a new project
railway config apply
```

Add Postgres (and Redis with `cache redis`) to the project, or use your own, then set the [production variables](./index.mdx#production-variables) on the service and deploy with `railway up`. Give the service a public domain in its settings, and set `NEXT_PUBLIC_APP_URL` to it: Railway passes the service's variables to the Docker build.

## Background jobs [#background-jobs]

Railway runs the app as a long-lived server, so the [jobs](../modules/jobs.mdx) worker runs with it.

## A frontend and its API [#a-frontend-and-its-api]

A project with a [frontend and its API](../../cli/frontend-and-api.mdx) runs as two Railway services in one project. [`pnpm clubedge deploy`](../../cli/deploy/railway.mdx) creates both, and rewrites `.railway/railway.ts` to describe them:

| Service | Builds | Domain | Health check | Before each release |
| --- | --- | --- | --- | --- |
| `<package>-web`, the frontend | `Dockerfile`, port 3000 | A public `*.up.railway.app` domain | `/` | Nothing |
| `<package>-api`, the API | `apps/api/Dockerfile`, port 3001 | None: reached over Railway's private network | `/api/health` | `node /app/migrate.mjs` |

Their variables:

- **The frontend:** `API_URL`, which `.railway/railway.ts` sets to the API service's private domain on port 3001, so `/api` reaches the API privately. `PORT=3000`, and your auth provider's `VITE_*` values: Railway passes the service's variables to the Docker build, which compiles them in. With Better Auth, also `DATABASE_URL`, `BETTER_AUTH_SECRET`, and `BETTER_AUTH_URL` (the frontend's domain), since Better Auth runs in the frontend's server.
- **The API:** `DATABASE_URL` (a reference to Railway's Postgres, or your own), `FRONTEND_URL` set to the frontend's domain, your auth provider's server variables, and the variables of the other modules, such as `REDIS_URL` or `STRIPE_SECRET_KEY`.

Stripe's webhook endpoint and Inngest's app URL use the frontend's domain, which forwards them to the API over the private network. With pg-boss or BullMQ, the worker runs inside the API service.

The migrations run in the API's pre-deploy step, so a failed migration stops the API's release. See [Production variables](./index.mdx#with-a-separate-api) for each provider's.

## Without a website [#without-a-website]

A project [without a website](../../cli/without-a-website.mdx) (Starter v0.28.0 or newer) runs as one Railway service, the API, which clients reach directly. [`pnpm clubedge deploy`](../../cli/deploy/railway.mdx#without-a-website) creates it, and writes `.railway/railway.ts` so that Railway builds the API's image:

| Service | Builds | Domain | Health check | Before each release |
| --- | --- | --- | --- | --- |
| `<package>-api`, the API | `apps/api/Dockerfile`, port 3001 | A public `*.up.railway.app` domain | `/api/health` | `node /app/migrate.mjs` |

Its variables are the API's: `DATABASE_URL`, your auth provider's server variables, the other modules' variables, and `CLIENT_IP_HEADER=x-real-ip`. With Better Auth, which runs in the API, also `BETTER_AUTH_SECRET` and `BETTER_AUTH_URL`, the service's public URL. With pg-boss or BullMQ, the worker runs inside it. For the deploy workflow, set `RAILWAY_SERVICE` to `<package>-api`.

## The deploy workflow [#the-deploy-workflow]

The Deploy job uploads the checkout with `railway up --ci`: Railway builds the image and runs the pre-deploy migrations. It needs the secret `RAILWAY_TOKEN`, a project token from the project's settings, and the variable `RAILWAY_SERVICE`, the service's name. Railway only lets verified (paid) accounts create project tokens; `pnpm clubedge deploy` works without one, with your Railway login.

With a frontend and its API, the job uploads only the service `RAILWAY_SERVICE` names; `pnpm clubedge deploy` releases both.
