---
name: railway-iac
description: Define and manage Railway projects with Infrastructure as Code in .railway/railway.ts. Use when authoring or editing Railway IaC files, running railway config init/pull/plan/apply, declaring services, databases, volumes, buckets, domains, or env vars in code, setting up multi-repo partials, or wiring railway config into CI. Triggers on "railway iac", "railway.ts", "infrastructure as code railway", "railway config plan", "define railway services in code". For migrating railway.json/railway.toml, use the railway-iac-migration skill instead.
license: MIT
metadata:
  version: "1.0.0"
---

# Railway Infrastructure as Code

Define a Railway project's services, databases, volumes, buckets, custom domains, environment variables, replicas, and canvas groups in **one authoring file**. The Railway CLI evaluates the file against the linked environment, prints a diff, and applies only after confirmation.

```txt
.railway/railway.ts   # generally available (the documented default)
.railway/railway.py   # beta
.railway/railway.go   # beta
```

Keep exactly **one** authoring file, never two languages in `.railway/`. This skill documents TypeScript (`import { ... } from "railway/iac"`); Python and Go mirror the same graph.

**Key mental model:** Railway does not read `.railway/` during deploys. `railway config plan` / `apply` run wherever you run them (locally or in CI) and reconcile the file with the live environment. Deleting a resource from the file deletes it on apply (within your ownership scope; see partials).

## Step 1: Prerequisites

```bash
# Railway CLI, then auth + link this directory to a project/environment
railway login
railway link

# TypeScript authoring package (use the repo's package manager)
npm install railway
# Python (beta): pip install railway-sdk
# Go (beta): go get github.com/railwayapp/railway-go-sdk@v0.2.0
```

If the directory isn't linked, `railway config plan` / `apply` / `pull` prompt for a project and environment. `RAILWAY_TOKEN` (an environment-scoped project token) also works for CI.

## Step 2: Get an authoring file

Pick one:

```bash
railway config init    # new project; scaffolds .railway/railway.ts + .railway/README.md
railway config pull    # existing project; imports live config into the authoring file
```

`railway config pull` generates human-editable code: keeps names, omits platform defaults and generated Railway domains, and renders existing variable values as `preserve()` so secrets stay on Railway. **Never pass `--include-variables` casually.** It decrypts non-sealed variables (including secrets) into source.

After a pull, verify the import is clean:

```bash
railway config plan
# expect: "Your Railway configuration is already up to date."
```

## Step 3: Author

```ts
import { defineRailway, project, service } from "railway/iac";

export default defineRailway((ctx) => {
  const web = service("web", {
    build: "pnpm build",
    start: "pnpm start",
    healthcheck: "/health",
    replicas: ctx.environment === "production" ? 3 : 1,
  });

  return project("my-project", { resources: [web] });
});
```

Full DSL: sources (`github`, `image`), `env` / `preserve()` / `ctx.shared`, `postgres`/`redis`/`mysql`/`mongo`, `volume` + `volumeMounts`, `bucket`, `domains`, `group`, and every `ctx` field: **[references/dsl-reference.md](references/dsl-reference.md)**.

## Step 4: Plan, review, apply

```bash
railway config plan      # read-only diff; secrets shown as «hidden»
railway config apply     # fresh plan + interactive confirm
```

Useful flags:

```bash
railway config plan --json                  # machine-readable
railway config plan --show-values           # reveal variable values (careful with secrets)
railway config plan --detailed-exit-code    # 0 = clean, 2 = changes pending (CI drift gate)
railway config apply --yes                  # non-interactive

# Pin a reviewed plan for later/CI apply (recommended over re-planning on merge)
railway config plan --out railway-plan.json
railway config apply --plan railway-plan.json --yes --confirm-destructive
```

`plan` and `apply` find the nearest `.railway/railway.{ts,py,go}` walking up from the current directory; `--file` overrides.

## Step 5 (optional): Multi-repo partials

One repo holding every service: one file, no `partial` export. Services split across repos: each repo's file exports `export const partial = "<name>"` and manages only what it declares. The CLI records per-resource ownership; a partial can only delete resources it owns.

Partials commands (`partials list`, `transfer`, `release`) and the GitHub Actions recipe (`railwayapp/config@v1` plan-on-PR / apply-on-merge): **[references/workflows.md](references/workflows.md)**.

## Safety rules: read before applying

- **Destructive changes are marked in the plan.** Deleting services, variables, volumes, or buckets requires `--confirm-destructive` in non-interactive runs; a stray `--yes` cannot remove resources. Review marked lines carefully.
- **A whole-project apply deletes resources omitted from the file.** In a partial, only owned-and-omitted resources are deleted.
- **Never inline secrets.** Use `preserve()` for values that should stay on Railway, `ctx.shared.NAME` for existing shared variables. Plan output redacts values by default; keep it that way in CI logs.
- **Stale-plan protection exists. Respect it.** Apply commits against the environment state it just read. If the environment drifted (concurrent apply, dashboard edit), apply is rejected; re-plan rather than forcing.
- **Services still managed by `railway.json`/`railway.toml` block `plan`.** The error names the offending service. Migrate it first with the `railway-iac-migration` skill.
- **`tracing` is not yet in the SDKs.** `service()` drops a `tracing` block you write, so plan may propose *removing* tracing from a traced service. Don't apply a plan that removes a `tracing` block you didn't remove yourself; set tracing from the Traces page or `railway trace` until SDK support lands.
- **Bucket regions are immutable.** Create a new bucket + copy data to move regions.
- **Volume lifecycle is conservative by design.** Shrinking `sizeMB`, detaching a mounted volume, or deleting a volume is destructive.
- **`railway config pull` does not overwrite blindly.** Use `--force` intentionally when re-importing over a hand-edited file.

## Troubleshooting

| Symptom | Fix |
|---|---|
| `plan` fails: service managed by config file | Service still reads `railway.json`/`railway.toml`. Migrate it (see `railway-iac-migration` skill) |
| `plan` fails: file without partial in environment that has named partials | Add `export const partial = "<name>"`, or release all named ownership first |
| `Cannot manage service "x": already managed by partial "y"` | Move the declaration to the owning repo, or `railway config partials transfer` |
| Apply rejected as stale | Environment changed since plan. Run `railway config plan` again |
| `railway/iac` import not found | `npm install railway` in the repo that holds `.railway/` |
