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.
.railway/railway.ts # generally available (the documented default)
.railway/railway.py # beta
.railway/railway.go # betaKeep 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
# 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.0If 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:
railway config init # new project; scaffolds .railway/railway.ts + .railway/README.md
railway config pull # existing project; imports live config into the authoring filerailway 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:
railway config plan
# expect: "Your Railway configuration is already up to date."Step 3: Author
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.
Step 4: Plan, review, apply
railway config plan # read-only diff; secrets shown as «hidden»
railway config apply # fresh plan + interactive confirmUseful flags:
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-destructiveplan 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.
Safety rules: read before applying
- Destructive changes are marked in the plan. Deleting services, variables, volumes, or buckets requires
--confirm-destructivein non-interactive runs; a stray--yescannot 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.NAMEfor 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.tomlblockplan. The error names the offending service. Migrate it first with therailway-iac-migrationskill. tracingis not yet in the SDKs.service()drops atracingblock you write, so plan may propose removing tracing from a traced service. Don't apply a plan that removes atracingblock you didn't remove yourself; set tracing from the Traces page orrailway traceuntil 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 pulldoes not overwrite blindly. Use--forceintentionally 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/ |