Railway¶
This starter kit deploys to Railway as one web service, one MongoDB service with a
persistent volume, and one private S3-compatible bucket. Express serves the built
React app and /api from the same HTTPS origin. Railway builds the repository's
Dockerfile; the frontend does not need a separate hosting service.
Infrastructure as code (IaC) describes the resources in a TypeScript file. The Railway CLI previews changes before applying them. Application secrets remain in Railway, while database and bucket references connect the resources automatically.
Before you deploy¶
- Create a Railway account, connect GitHub, and install the Railway GitHub App with access to the application's repository. Accept any pending permission updates in GitHub's installation settings.
- Select Singapore as the preferred deployment region in the Railway
dashboard before creating resources, where the account's plan permits it.
The bucket example below explicitly selects Singapore with
sin. - Use Use this template on Launchpad to create an application repository, then clone it. A template copy has its own history and is independent of Launchpad.
- Install dependencies with
bun install. The root development dependencies include@railway/cliand therailwayIaC SDK. - Set up Resend and have its API key ready.
Application copies can remove the starter's docs/ directory and replace its root
README. Replace the starter landing page when building the application's own UI;
see Development workflow.
Free plan and usage
Railway Free includes a small monthly usage credit. This should be enough for our use case but isn't enough to actually run this application in production. Check current pricing and usage in the dashboard.
Create and describe the project¶
Run all commands from the application's repository root:
init creates and links a fresh Railway project.
Replace the generated authoring file with the following example. Replace
YOUR_OWNER/YOUR_REPO with the application's GitHub repository and your-app with
its project name.
import {
bucket,
defineRailway,
github,
mongo,
preserve,
project,
ref,
service,
} from "railway/iac";
export default defineRailway(() => {
const database = Object.assign(mongo("mongodb"), {
deploy: { sleepApplication: true },
});
database.variables = {
GLIBC_TUNABLES: { type: "literal", value: "glibc.pthread.rseq=1" },
};
const uploads = bucket("uploads", { region: "sin" });
const web = service("web", {
source: github("YOUR_OWNER/YOUR_REPO", {
branch: "main",
checkSuites: true,
}),
build: { builder: "DOCKERFILE", dockerfilePath: "Dockerfile" },
healthcheck: "/api/health",
deploy: { sleepApplication: true },
env: {
PORT: "8080",
MONGODB_URI: database.env.MONGO_URL,
APP_URL: preserve(),
BETTER_AUTH_URL: preserve(),
BETTER_AUTH_SECRET: preserve(),
RESEND_API_KEY: preserve(),
STORAGE_ENDPOINT: ref(uploads, "ENDPOINT"),
STORAGE_REGION: ref(uploads, "REGION"),
STORAGE_BUCKET: ref(uploads, "BUCKET"),
STORAGE_ACCESS_KEY_ID: ref(uploads, "ACCESS_KEY_ID"),
STORAGE_SECRET_ACCESS_KEY: ref(uploads, "SECRET_ACCESS_KEY"),
},
});
return project("your-app", { resources: [database, uploads, web] });
});
The MongoDB helper provisions the database and its persistent volume. Its connection reference uses Railway's private network. Keep MongoDB private; only the web service needs a public domain. The bucket is a separate storage resource, not a process inside the application container.
The MongoDB configuration includes GLIBC_TUNABLES=glibc.pthread.rseq=1 to address
its Linux kernel 6.19+ startup incompatibility on Railway. This is the workaround
verified in the deployment. Track the
MongoDB issue for a permanent fix.
preserve() retains values configured outside IaC without committing them to source.
checkSuites: true enables Wait for CI. The existing Docker workflow runs on
pushes and invokes the checks workflow. Railway waits for GitHub Actions check
suites before deploying; a failed workflow prevents deployment. Ensure the Railway
GitHub App has the required permissions. See
Railway's CI requirements
for cancellation and timeout behavior.
Preview and apply the configuration:
Review every change. The initial plan should create MongoDB, the bucket, and the web
service without deleting anything. Commit .railway/ and push it to GitHub.
Subsequent infrastructure edits require another plan and apply; pushing the authoring
file alone does not apply its resource changes. GitHub pushes deploy application code.
The first app deployment can fail while required production settings are missing. Continue with the next section, then redeploy. Do not remove configuration validation to make an incomplete deployment pass.
Configure the public URL and secrets¶
Create a Railway domain for the web service:
IaC sets PORT=8080, and the domain targets the same port. The server honors
PORT; the Dockerfile sets NODE_ENV=production.
Set these variables on web using the returned HTTPS URL:
| Variable | Required value |
|---|---|
APP_URL |
The public HTTPS origin, without a path |
BETTER_AUTH_URL |
Exactly the same origin as APP_URL |
BETTER_AUTH_SECRET |
A unique random secret of at least 32 characters |
RESEND_API_KEY |
The application's Resend API key |
Use Railway's dashboard Variables tab or its CLI editor:
The editor uses the shell's configured EDITOR. It shows a redacted diff before
applying. To generate and set the authentication secret without printing it, run:
node -e "process.stdout.write(require('node:crypto').randomBytes(32).toString('base64'))" | bunx railway variable set BETTER_AUTH_SECRET --stdin --service web --skip-deploys
Generate it once for the environment. Replacing it later invalidates existing sessions.
--skip-deploys avoids starting a deployment for each individual setting.
IaC supplies MONGODB_URI and all five storage variables automatically. Do not copy
bucket access keys manually. Storage limits, allowed MIME types, and proxy settings
are described in Development workflow.
MailDev is not a production requirement.
Redeploy after completing the settings:
Allow browser access to the bucket¶
Direct browser uploads and downloads use presigned URLs. Configure the bucket's
CORS policy to allow the application's origin, PUT and GET, and Content-Type.
The bucket remains private; CORS does not grant unauthenticated object access.
Railway supports configuring CORS through the S3 API. A dashboard CORS control is not required. The following command uses the application's installed AWS SDK, injects the web service's variables into a local process, and preserves existing rules. It does not print credentials. Run it only on a trusted computer.
bunx railway run --service web --no-local -- node --input-type=module -e '
import { createRequire } from "node:module";
const require = createRequire(new URL("./apps/server/package.json", import.meta.url));
const { S3Client, GetBucketCorsCommand, PutBucketCorsCommand } = require("@aws-sdk/client-s3");
const env = process.env;
const client = new S3Client({
endpoint: env.STORAGE_ENDPOINT,
region: env.STORAGE_REGION,
forcePathStyle: true,
credentials: {
accessKeyId: env.STORAGE_ACCESS_KEY_ID,
secretAccessKey: env.STORAGE_SECRET_ACCESS_KEY,
},
});
const matches = (rule) =>
rule.AllowedOrigins?.includes(env.APP_URL) &&
["PUT", "GET"].every((method) => rule.AllowedMethods?.includes(method)) &&
rule.AllowedHeaders?.some((header) =>
header === "*" || header.toLowerCase() === "content-type");
try {
let rules = [];
try {
rules = (await client.send(new GetBucketCorsCommand({
Bucket: env.STORAGE_BUCKET,
}))).CORSRules ?? [];
} catch (error) {
if (!["NoSuchCORSConfiguration", "NoSuchCORS"].includes(error.name)) throw error;
}
if (!rules.some(matches)) {
rules.push({
AllowedOrigins: [env.APP_URL],
AllowedMethods: ["PUT", "GET"],
AllowedHeaders: ["Content-Type"],
MaxAgeSeconds: 3600,
});
await client.send(new PutBucketCorsCommand({
Bucket: env.STORAGE_BUCKET,
CORSConfiguration: { CORSRules: rules },
}));
}
const saved = await client.send(new GetBucketCorsCommand({ Bucket: env.STORAGE_BUCKET }));
if (!saved.CORSRules?.some(matches)) throw new Error("CORS verification failed");
console.log("Bucket CORS configured and verified.");
} catch (error) {
console.error("Bucket CORS failed:", error.name);
process.exitCode = 1;
} finally {
client.destroy();
}
'
The endpoint and URL style in the bucket's Credentials tab are authoritative. New Railway buckets normally use virtual-hosted URLs. The pilot also verified bucket access with this starter's existing path-style S3 client. Verify the complete upload, confirmation, and private download sequence, not just the presence of variables. Repeat CORS configuration when the public origin changes or a new environment gets its own bucket.
Verify the deployment¶
bunx railway deployment list --service web --json
bunx railway deployment list --service mongodb --json
curl https://your-app.up.railway.app/api/health
Replace the example URL with the application's domain. Confirm both deployments
reach SUCCESS, the homepage responds, and /api/health returns HTTP 200 with
{"status":"ok"}. The health endpoint checks the live MongoDB connection.
A successful build or deployment status alone does not establish public reachability.
Create an account, sign in, and exercise a protected API route. Verify file upload, confirmation, and private download, and test delivery from a verified Resend sending domain for application features that send email. Inspect Railway usage and arrange backups for persistent data before relying on the deployment.