Railway IaC TypeScript DSL Reference
All helpers import from railway/iac. A file exports defineRailway and returns one project.
import {
bucket,
defineRailway,
github,
group,
image,
postgres,
preserve,
project,
redis,
service,
volume,
} from "railway/iac";
export default defineRailway((ctx) => {
/* ...resources... */
return project("my-project", { resources: [/* ... */] });
});Environment context (ctx)
defineRailway receives a context object. Use it to render different desired state per Railway environment.
| Field | Description |
|---|---|
ctx.command |
plan, apply, etc. |
ctx.projectId |
Linked project ID |
ctx.projectName |
Linked project name |
ctx.environmentId |
Target environment ID |
ctx.environment |
Target environment name |
ctx.environmentName |
Alias for ctx.environment |
ctx.isEnvironment(name) |
Boolean name check |
ctx.shared.NAME |
Reference to an existing shared variable on the environment (compiles to ${{shared.NAME}}; does not create it) |
const prod = ctx.isEnvironment("production");Services
const web = service("web", {
source: github("acme/web"), // or image(...), see Sources
build: "pnpm build", // build command
start: "pnpm start", // start command
healthcheck: "/health", // healthcheck path
healthcheckTimeout: 30, // seconds
preDeploy: "npx drizzle-kit migrate", // runs between build and deploy
replicas: 3, // number, or per-region map
domains: ["app.example.com"], // strings or { domain, port }
env: { NODE_ENV: "production" }, // variables, see below
volumeMounts: { "/data": dataVol }, // mount path -> volume(...) resource
});- Service names should match what users see in Railway; spaces are valid (
service("Docs Frontend", {...})). - Omit
sourceto manage service settings without declaring a repo/image. preDeploy: "..."is equivalent todeploy: { preDeployCommand: ["..."] }(the formconfig pullrenders).- Per-region replicas:
replicas: { "us-west2": 2, "europe-west4": 1 }.
Sources
source: github("owner/repo") // GitHub, default branch
source: github("owner/repo", { branch: "main" })
source: github("acme/monorepo", { rootDirectory: "apps/api" })
source: image("ghcr.io/acme/worker:latest") // Docker image
source: image("ghcr.io/acme/worker:1.2.3", {
autoUpdates: { type: "patch" }, // disabled | patch | minor
})autoUpdates works only for Docker Hub and GHCR images, not GitHub sources or other registries.
Environment variables
env: {
NODE_ENV: "production", // literal
DATABASE_URL: db.env.DATABASE_URL, // reference another resource's variable
SENTRY_DSN: ctx.shared.SENTRY_DSN, // existing shared variable on the environment
STRIPE_SECRET_KEY: preserve(), // keep whatever value Railway already has
API_HOST: api.env.RAILWAY_PRIVATE_DOMAIN,
}preserve() means "keep the value already set in Railway"; it is used for imported secrets the CLI can't read. Never paste secret literals into the file.
Tracing (not yet supported by SDKs)
The tracing: { enabled, autoInstrumentation } block exists in the compiled config but service() in TS/Python/Go drops it before the CLI sees it. Consequence: plan proposes to remove tracing from traced services. Until SDK support lands, manage tracing from the Traces page or railway trace, and do not apply a plan that removes a tracing block you didn't remove.
Databases
const db = postgres("postgres");
const cache = redis("redis");
const mysqlDb = mysql("mysql");
const mongoDb = mongo("mongo");Reference their variables from services: db.env.DATABASE_URL, cache.env.REDIS_URL. Provisioning is a Railway product workflow; don't attach volumes to managed database helpers yourself.
Volumes
const data = volume("backend-data", {
region: "us-west2",
sizeMB: 1024,
});
const backend = service("backend", {
volumeMounts: { "/data": data }, // key = mount path in the container
});- One volume attaches to one service.
- Increasing
sizeMBis a non-destructive resize. Decreasing, deleting, detaching a mounted volume, or changing placement is destructive. config pullmay import managed-database volumes so Railway can preserve them; thepostgres()helper still owns the database intent.
Buckets
const media = bucket("media", { region: "iad" });Region is immutable after creation. To move regions: new bucket, copy data, repoint services, delete old bucket.
Custom domains
domains: ["app.example.com"]
domains: [{ domain: "api.example.com", port: 3000 }]Generated *.up.railway.app domains are never imported or managed.
Groups
Canvas organization only: structural, no runtime effect.
const backend = group("Backend", [api, worker, db]);
return project("my-app", { resources: [backend, web] });Partials (multi-repo)
Each repository manages its own slice of a shared environment:
export const partial = "api"; // Python: PARTIAL = "api"; Go: const Partial = "api"- Name: 1-64 chars from
a-z A-Z 0-9 . _ -. - Once any partial in an environment has a name, every file targeting that environment must export one.
- First apply claims ownership of everything the file declares (apply runs even with a no-change plan, to record the claim).
- A partial deletes only resources it owns.
- Renaming the export does not transfer ownership. Use
railway config partials transfer. - Whole-environment planning requires all named ownership released (
partials release).
Complete example
export default defineRailway((ctx) => {
const prod = ctx.environment === "production";
const db = postgres("postgres");
const cache = redis("redis");
const uploads = bucket("uploads", { region: "iad" });
const workerData = volume("worker-data", { region: "us-west2", sizeMB: 1024 });
const api = service("api", {
source: github("acme/monorepo", { rootDirectory: "apps/api" }),
build: "pnpm --filter api build",
start: "pnpm --filter api start",
healthcheck: "/health",
replicas: prod ? { "us-west2": 2, "europe-west4": 1 } : 1,
env: {
DATABASE_URL: db.env.DATABASE_URL,
REDIS_URL: cache.env.REDIS_URL,
JWT_SECRET: preserve(),
},
});
const web = service("web", {
source: github("acme/monorepo", { rootDirectory: "apps/web" }),
build: "pnpm --filter web build",
start: "pnpm --filter web start",
domains: prod ? ["app.example.com"] : [],
env: { API_HOST: api.env.RAILWAY_PRIVATE_DOMAIN },
});
const worker = service("worker", {
source: github("acme/monorepo", { rootDirectory: "apps/worker" }),
build: "pnpm --filter worker build",
start: "pnpm --filter worker start",
volumeMounts: { "/data": workerData },
env: { DATABASE_URL: db.env.DATABASE_URL, REDIS_URL: cache.env.REDIS_URL },
});
const backend = group("Backend", [db, cache, api, worker]);
const storage = group("Storage", [uploads, workerData]);
return project("acme", { resources: [backend, storage, web] });
});