Next.js on Railway with Postgres, Resend and Stripe: how the pieces connect
What gets created at Railway, Cloudflare, Resend and Stripe for a Next.js app, the DNS records and variables that join them, and where it goes wrong.
Each of these four providers has a good guide of its own. What none of them documents is the other three: which record Resend needs in the DNS zone that Railway also writes to, or why the Stripe webhook has to wait for the domain. This page follows one setup from an empty account to a live site and names every record and variable that crosses from one provider to another.
It is the stack Shipyard picks when you change nothing, so the details below come from two places: the providers' documentation, linked at the end, and the code Shipyard runs. You can follow it by hand just as well.
What exists when the setup is done
| Provider | What is created | What ties it to the rest |
|---|---|---|
| GitHub | A private repository holding the Next.js app | Railway deploys from it on every push to main |
| Railway | One project with a web service and a Postgres service | DATABASE_URL on web is a reference to the Postgres service |
| Cloudflare | The DNS zone for the domain | Holds the records Railway, Resend and Google ask for |
| Resend | A verified sending domain and a sending-only API key | Three DNS records, then RESEND_API_KEY and EMAIL_FROM on Railway |
| Stripe | Products, prices and one webhook endpoint, in test mode | STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET and one STRIPE_PRICE_ variable per price |
The order, and why it is this order
Setups often fail on ordering: a step ran before the thing it depends on was ready. This order has no step that waits on a later one.
- Domain and DNS zone. Every later step writes a record, so the zone must exist and be active first. Cloudflare says to allow up to 24 hours for a registrar to switch nameservers. A domain bought at Cloudflare Registrar already uses them.
- Repository. Railway connects a service to a repository and a branch, so the repository has to exist, and the Railway GitHub app must be allowed to see it.
- Railway project, the
webservice and Postgres. The database goes in before the first deploy so that the app never starts without one. - Custom domain. The bare domain and
wwware added to thewebservice, Railway answers with the records it wants, and those go into the zone from step 1. - Resend. Its records go into the same zone. Doing this after the hosting records means the checks for both run while you wait once, not twice.
- Stripe. The webhook address contains the final domain, so it cannot be created before the domain is settled.
- Variables, all in one write, and only then a check of the live site. Writing them last means the app is never deployed with half of its keys.
Railway and Postgres: a reference, not a copied password
Railway's Postgres service publishes its own variables, among them DATABASE_URL, PGHOST, PGUSER and PGPASSWORD. The web service does not get a copy of the connection string. It gets a reference that Railway resolves inside the project, shown below.
Two things follow. The password never leaves Railway, so there is nothing to leak from a laptop or a chat window, and Shipyard never sees it either. And the database stays private: Railway keeps a database reachable only from inside its project unless you switch on public access, which adds a TCP proxy and a second variable, DATABASE_PUBLIC_URL. Railway bills network egress on that proxy, so leave it off until you need to connect from your own machine.
DATABASE_URL=${{Postgres.DATABASE_URL}}Railway and Cloudflare: two records per name
For each custom domain Railway returns a CNAME value ending in up.railway.app and a TXT record that proves you own the name. Both are needed. Railway's documentation is explicit that a domain does not verify on the CNAME alone, and that requests answer 404 until the TXT record is in place. Copy the TXT host and value exactly as Railway shows them; they differ per domain.
A CNAME on a bare domain is not valid in plain DNS. Cloudflare flattens it, which is why this stack keeps DNS there even when the domain was bought somewhere else. Shipyard writes these records as DNS only, with Cloudflare's proxy off. If you turn the proxy on later, Railway says to set Cloudflare's SSL/TLS mode to Full, and warns that Full (Strict) does not work as intended.
Count your names before you start. Railway's documentation gives the Trial plan one custom domain and the Hobby plan two per service, and counts the bare domain and www as two. Both names therefore need a paid plan, and they use up the Hobby allowance for that service.
CNAME example.com <value from Railway>.up.railway.app DNS only
TXT <host from Railway> <value from Railway>
CNAME www.example.com <value from Railway>.up.railway.app DNS only
TXT <host from Railway> <value from Railway>Resend and Cloudflare: three records, then a narrower key
Resend verifies a domain with three records. An MX record and a TXT record sit on the send subdomain: the MX points at the feedback mail server Resend gives you and the TXT is the SPF policy. A second TXT record on resend._domainkey carries the DKIM public key. Because they live on send and resend._domainkey, they do not collide with anything on the bare domain, such as the MX records of a mailbox. Shipyard reads the exact values from Resend's API and adds a fourth record of its own, a DMARC policy on _dmarc that starts as v=DMARC1; p=none, which only monitors.
Resend recommends sending from a subdomain to keep reputations apart. Shipyard verifies the bare domain instead, so that mail comes from hello@example.com. That is a trade-off to know about, not a requirement of the stack.
The key matters as much as the records. Adding and verifying a domain needs a Resend key with full access. Your app should not hold that key. Once the domain is verified, a second key is created with the sending_access permission and restricted to this one domain, and that is the value of RESEND_API_KEY on Railway. If it leaks, it can send mail from your domain and do nothing else.
Resend says verification can take up to 72 hours and is often much faster. Shipyard asks again for twenty minutes, then stops so you can resume later without redoing anything.
Stripe: test objects and one webhook endpoint
Stripe gets products, prices with lookup keys, an optional welcome coupon, and one webhook endpoint at https://example.com/api/stripe/webhook. The endpoint listens for seven events and no others, which is what Stripe advises: checkout.session.completed, customer.created, customer.subscription.created, customer.subscription.updated, customer.subscription.deleted, invoice.paid and invoice.payment_failed.
Creating the endpoint returns its signing secret, a value that starts with whsec_. It becomes STRIPE_WEBHOOK_SECRET. Stripe issues a different secret for every endpoint, and a different one again in live mode, so a secret copied from another project or from the Stripe CLI will reject every real event. Verification also needs the raw request body: Stripe warns that any change to the body before the check makes it fail.
Everything here is created in test mode. Stripe keeps test and live objects apart, so a test price cannot be part of a live payment. Going live means creating the same products, prices and webhook again with a live key, and replacing the secret key, the webhook secret and the price IDs on the host. In Shipyard that is its own command, shipyard go-live, and it asks first.
The variables that end up on Railway
Analytics, error tracking, file storage and an AI provider add their own names when they are part of the stack, for example NEXT_PUBLIC_GA_MEASUREMENT_ID, SENTRY_DSN, the five S3_ variables and ANTHROPIC_API_KEY. Variables you set yourself are left alone: the write adds and updates, it does not replace the list.
| Variable | Comes from | Note |
|---|---|---|
APP_URL, APP_NAME | Your domain and project name | Not secret |
DATABASE_URL | Railway Postgres | A reference, resolved by Railway |
SESSION_SECRET, ENCRYPTION_KEY | Generated at random on your computer | Sign session cookies and encrypt data at rest |
EMAIL_FROM, RESEND_API_KEY | Resend | The key can only send, and only from this domain |
STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET | Stripe | Test mode until you go live |
STRIPE_PRICE_<NAME> | Stripe | One per price, holding its price ID |
What goes wrong, and what it looks like
- Railway cannot see the repository. The Railway GitHub app is not installed on the account that owns it, or was limited to other repositories.
- The certificate stays pending. Railway lists the usual causes: a missing TXT record, CAA records that leave out Let's Encrypt, or broken DNSSEC. It expects a certificate within an hour of correct DNS, and advises against deleting and adding the domain again, because Let's Encrypt rate limits can then block it for seven days.
- A record already sits on the name. An old A record on the bare domain blocks the new CNAME. Shipyard stops and names the record instead of overwriting it; remove it yourself and run again.
- The Postgres service takes a moment to appear. Railway deploys it in the background. Running the same step again finds the existing service; it does not create a second database.
- The webhook check fails. After the variables are written, Shipyard creates a test customer in Stripe and waits two minutes for your app to acknowledge
customer.created. A handler at a different path, or one that answers with a redirect, fails here and not in front of a customer.
Changing one part of this stack
The registrar is the easiest part to change: the domain can come from Cloudflare Registrar or Namecheap, and the rest of this page stays the same. Sign-in is built into the app, with sessions stored in the same Postgres. If you would rather have Supabase hold the database and the sign-in, the Railway and Supabase guide covers what changes.
To start from exactly this stack, load its code and look at the plan before anything is created:
shipyard stack use S100000000000000The setup article for each provider
Each provider in this guide has one article with its full steps. They are all listed on the integrations page.
- Railway: A project with a web service and Postgres, linked together, with every variable and your domain.
- Cloudflare DNS: The zone, the nameservers and every record your host, email and Google need.
- Resend: The sending domain, its DKIM, SPF and DMARC records, verification and a sending-only key.
- Stripe: Products, prices, a welcome coupon and the webhook endpoint, in test mode first.
- GitHub: A private repository with the starter committed, connected to your host so every push deploys.
Sources
- Railway: working with domainsdocs.railway.com
- Railway: troubleshooting SSLdocs.railway.com
- Railway: PostgreSQLdocs.railway.com
- Cloudflare: full DNS setupdevelopers.cloudflare.com
- Resend: DNS records on Cloudflareresend.com
- Resend: create an API keyresend.com
- Stripe: receive events in your webhook endpointdocs.stripe.com
- Stripe: API keysdocs.stripe.com
Have this set up for you
Shipyard creates everything on this page on your own accounts, from your coding agent. Your keys stay in the keychain on your computer, and anything that costs money waits for your yes.
Pick your stackRead next
- Deploying a Next.js app with Postgres on Railway
- Setting up Resend on a new domain
- Stripe webhooks explained
- Connect a custom domain to Railway or Vercel
- Railway with Supabase as the database and Supabase Auth for sign-in
- Next.js on Vercel with Supabase: connection strings, DNS and variables
- Cloudflare Registrar or Namecheap for a new product's domain