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

In a project created with `--deploy railway` (CLI 0.22.0 or newer), `pnpm clubedge deploy` uses [Railway's CLI](https://docs.railway.com/cli) and your Railway login:

```sh
pnpm clubedge add deploy railway   # if the project deploys elsewhere today
pnpm clubedge deploy
```

## What it does [#what-it-does]

For a full-stack app (Next.js or TanStack Start), `deploy`:

1. **Signs in** with `railway login`, which opens the browser when you are signed out. With `RAILWAY_API_TOKEN` set, it skips this.
2. **Creates or reuses the Railway project**, with Postgres (and Redis with `cache redis`) unless your [env file](./index.mdx#your-own-databases) points them elsewhere, the app's service named after the package, and a `*.up.railway.app` domain.
3. **Sets the production variables** it can: `DATABASE_URL` and `REDIS_URL` (references to Railway's databases, or your own URLs), `APP_URL` and `NEXT_PUBLIC_APP_URL`, `PORT`, a generated `BETTER_AUTH_SECRET` that later runs keep, `CLIENT_IP_HEADER=x-real-ip`, where the [client's address](./index.mdx#client-addresses) is (CLI 0.42.0 or newer), and the [env file's](./index.mdx#provider-keys) values.
4. **Applies `.railway/railway.ts`**, the service's configuration as code: the migrations before each release and the `/api/health` check. It writes the file when the project has none and adds Railway's TypeScript SDK (`railway`) as a dev dependency; commit them.
5. **Uploads the project and waits** for Railway's final status: Railway builds the `Dockerfile`, runs the migrations, and checks `/api/health` before switching traffic. Then it prints the URL.

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

CLI 0.35.0 or newer. For a [frontend and its API](../frontend-and-api.mdx), `deploy` creates two services instead of one, named after the package and each stack's deployment id:

| Service | App | Domain |
| --- | --- | --- |
| `<package>-web` | The React + Vite frontend, from the root `Dockerfile` | The public `*.up.railway.app` domain |
| `<package>-api` | The Hono or FastAPI API, from `apps/api/Dockerfile` | None: the frontend reaches it over Railway's private network |

- **Databases** are added as for one app: Postgres, unless your env file points `DATABASE_URL` elsewhere.
- **API variables** go on the API service: the database URLs, `FRONTEND_URL` (the frontend's public URL), `TRUSTED_PROXY_HOPS=1` (CLI 0.42.0 or newer), since only the frontend reaches it, and the env file's values other than `VITE_*`.
- **Frontend variables** are `PORT`, `CLIENT_IP_HEADER=x-real-ip` (CLI 0.42.0 or newer), and the env file's `VITE_*` values, which its build reads.
- **`.railway/railway.ts`** describes both services and runs the migrations before each API release. `deploy` writes it again on each run; when the file has your own configuration, `deploy` stops and asks you to merge the two-service configuration by hand.
- **It waits for both deployments**, then prints the frontend's URL.

`deploy --preview` deploys one app only, so it refuses on a frontend and its API.

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

CLI 0.44.0 or newer. For a [project without a website](../without-a-website.mdx), `deploy` creates one service, the API, which the [native app](../../starter/native-app.mdx) calls:

| Service | App | Domain |
| --- | --- | --- |
| `<package>-api` | The Hono or FastAPI API, from `apps/api/Dockerfile`, on port 3001 | The public `*.up.railway.app` domain |

- **Databases** are added as for one app: Postgres (and Redis with `cache redis`), unless your env file points them elsewhere.
- **Variables** go on the API: the database URLs, `PORT=3001`, `CLIENT_IP_HEADER=x-real-ip`, since clients reach the API itself, and the env file's values. With Better Auth, which then runs in the API, also a generated `BETTER_AUTH_SECRET` that later runs keep, and the service's public URL as `BETTER_AUTH_URL`, unless the env file names another public one.
- **`.railway/railway.ts`** describes the service and runs the migrations before each release. `deploy` writes it again on each run, as for a frontend and its API.
- **It waits for the deployment**, then prints the API's URL. Set it as the native app's `EXPO_PUBLIC_API_URL` for its release builds.

`deploy --preview` refuses here too, and prints how to copy production's environment with Railway's CLI instead.

## Railway's CLI [#railways-cli]

`deploy` needs Railway CLI 5.42.1 or newer. It uses an installed `railway`, else one npx already downloaded, and only then downloads one. Installing it once makes `deploy` faster:

```sh
npm install -g @railway/cli
```

## Costs [#costs]

Railway bills the service, Postgres, and Redis by usage on your plan. With your own databases in the env file, Railway only runs the app.

## When a deploy fails [#when-a-deploy-fails]

`deploy` names the logs commands: `railway logs --build --service <name>` for the build, and `railway logs --service <name>` for the app and its health check. For a frontend and its API, it prints one per service: `railway logs --service <package>-web` and `railway logs --service <package>-api`; without a website, the API's. A build that ran out of time (`DeadlineExceeded`) usually passes on a second try. Fix the cause and run `deploy` again: it reuses what exists.

To set up Railway without this command, see [Railway](../../starter/deploy/railway.mdx) in the deploy guide.
