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.

Simon, Founder of Shipyard

By · Founder of Shipyard

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

ProviderWhat is createdWhat ties it to the rest
GitHubA private repository holding the Next.js appRailway deploys from it on every push to main
RailwayOne project with a web service and a Postgres serviceDATABASE_URL on web is a reference to the Postgres service
CloudflareThe DNS zone for the domainHolds the records Railway, Resend and Google ask for
ResendA verified sending domain and a sending-only API keyThree DNS records, then RESEND_API_KEY and EMAIL_FROM on Railway
StripeProducts, prices and one webhook endpoint, in test modeSTRIPE_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.

  1. 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.
  2. 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.
  3. Railway project, the web service and Postgres. The database goes in before the first deploy so that the app never starts without one.
  4. Custom domain. The bare domain and www are added to the web service, Railway answers with the records it wants, and those go into the zone from step 1.
  5. 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.
  6. Stripe. The webhook address contains the final domain, so it cannot be created before the domain is settled.
  7. 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.

Text
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.

DNS records
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.

VariableComes fromNote
APP_URL, APP_NAMEYour domain and project nameNot secret
DATABASE_URLRailway PostgresA reference, resolved by Railway
SESSION_SECRET, ENCRYPTION_KEYGenerated at random on your computerSign session cookies and encrypt data at rest
EMAIL_FROM, RESEND_API_KEYResendThe key can only send, and only from this domain
STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRETStripeTest mode until you go live
STRIPE_PRICE_<NAME>StripeOne per price, holding its price ID

What goes wrong, and what it looks like

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 S100000000000000

The 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.

Sources

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 stack

Read next