---
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"
title: railway-iac
canonical_url: https://skilld.dev/gh/sivarampg/railway-iac-skill/railway-iac
last_updated: 2026-10-04T16:18:07.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/dsl-reference.md](https://skilld.dev/api/skills-raw/sivarampg/railway-iac-skill/railway-iac/references/dsl-reference.md), [references/workflows.md](https://skilld.dev/api/skills-raw/sivarampg/railway-iac-skill/railway-iac/references/workflows.md).
>
> If the user asked to install this Skill, run `npx skilld install sivarampg/railway-iac-skill/railway-iac`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/railway-iac`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/sivarampg/railway-iac-skill/railway-iac) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/railway-iac`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/railway-iac --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/railway-iac --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# 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](https://skilld.dev/api/skills-raw/sivarampg/railway-iac-skill/railway-iac/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](https://skilld.dev/api/skills-raw/sivarampg/railway-iac-skill/railway-iac/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/` |
