Skip to content
← All posts

·

How to deploy your SaaS: env vars and webhooks

Your first production deploy needs the right env vars, a new auth secret and a webhook at the live URL. The manual steps, and what npm run setup does.

Short answer: a first production deploy is mostly about configuration, not code. Production needs its own copy of every secret, a public URL that matches the one your auth library and payment provider expect, and a webhook registered at that URL, which gives you a signing secret you can only set after the first deploy. So you deploy twice: once to get the URL, once more with the final values. Launchfast does both passes for you: npm run setup copies your keys to Vercel, deploys, registers the payment webhook and redeploys.

Why does the first production deploy break when it worked locally?

The code is the same. The environment is not. These are the symptoms people search for:

  • The build passes but the first request fails with Missing environment variable DATABASE_URL (or whichever variable you forgot).
  • Sign-up works locally, but in production the confirmation link points to http://localhost:3000, because the app URL is still the local one.
  • Checkout opens, the buyer pays, and nothing happens in your app, because the payment provider has no webhook for your live URL and your server never hears about the payment.
  • Webhooks arrive but are rejected with a signature error, because the signing secret in production belongs to a different endpoint or to the other mode (test vs live).
  • Google or GitHub login fails with redirect_uri_mismatch, because the OAuth app only lists localhost as a callback.
  • Sign-up fails in production because no email provider is configured and the code that printed emails to your terminal is not running.

Each one is a value that exists on your laptop and not on the server.

How do you deploy to production by hand?

The steps below use Vercel as the example, but the same list applies to any host.

  1. List every variable your app reads. Search the code for process.env, and keep .env.example in sync so the list lives in one place. Missing variables are the most common cause of a failed first deploy.
  2. Generate new secrets for production. Never reuse your local auth secret. For example, openssl rand -base64 32 gives you a fresh one. Anyone who has the secret can forge sessions, so it should exist only in your host's settings.
  3. Use the production database. Create a database (or a separate branch) for production and run your migrations against it. Use the pooled connection string for the app, as hosts that run short-lived functions open many connections.
  4. Set the public app URL. Your auth library uses it to build links and check where requests come from. In production it must be your real address, with https://, and not localhost. Set it before the build if any NEXT_PUBLIC_ variable depends on it, because those values are baked in at build time.
  5. Use live payment keys, with the right mode. A test key paired with live mode (or the other way round) fails. Create the product in the mode you will sell in, and use that product's ID.
  6. Add the variables to the host. In Vercel that is Project, Settings, Environment Variables, and each variable can be limited to Production, Preview or Development. You can also set them from the command line with vercel env add. Check the Vercel docs on environment variables for the current options.
  7. Deploy once to get the URL. If you use the default address, the project name decides it. If you use a custom domain, connect it first and set its DNS.
  8. Register the webhook at the live URL. In your payment provider's dashboard, add an endpoint like https://your-domain/api/webhooks/<provider> and subscribe to the payment and subscription events you handle. The provider shows a signing secret for that endpoint.
  9. Put the signing secret in production and redeploy. Environment variable changes only apply to new deployments, so the app needs a second deploy to read the secret.
  10. Add the production callback URLs to your OAuth apps. Google and GitHub each need https://your-domain/api/auth/callback/<provider> added to the allowed list.
  11. Verify your email domain. Add the provider's SPF and DKIM records at your DNS host, wait for verification, and send from an address on that domain.
  12. Test one real flow end to end. Sign up, confirm the email, make a purchase in the provider's test mode, and check that the payment appears in your database. Then switch to live keys.

The traps are steps 8 and 9. The signing secret only exists after the URL does, so a single deploy is never enough, and a webhook handler must never accept requests it has not verified.

What does Launchfast already do for a production deploy?

The kit is built so the list of variables, the URL and the webhook are handled by one command.

The variable list is in one file. .env.example documents every variable, including which ones are optional (Google, GitHub) and which are required in production. src/lib/env.ts has requireEnv(), which fails with a message that says how to fix it:

export function requireEnv(name: string): string {
  const value = process.env[name];
  if (!value) {
    throw new Error(
      `Missing environment variable ${name}. Run the setup script or copy .env.example to .env.local and fill it in.`,
    );
  }
  return value;
}

Email fails loudly in production. In src/lib/email.ts, sendEmail() prints to the terminal in development when there is no RESEND_API_KEY, but in production it throws Missing environment variable RESEND_API_KEY instead of silently dropping the message.

npm run setup is the deploy. When you answer yes to "Deploy to Vercel at the end?", the script in scripts/setup/index.mts:

  1. Generates BETTER_AUTH_SECRET with randomBytes(32) if there isn't one.
  2. Logs you in to Vercel and links the project through the Vercel CLI (scripts/setup/vercel.mts).
  3. Asks for a custom domain, or uses https://<project>.vercel.app, and sets NEXT_PUBLIC_APP_URL to it.
  4. Copies the production variables (DATABASE_URL, BETTER_AUTH_SECRET, BILLING_PROVIDER, your payment keys and product or price IDs, the Resend keys, and any Google or GitHub keys) to Vercel's production environment.
  5. Pins the function region next to your Neon database, and saves it in vercel.json.
  6. Deploys, reads the real production URL, and registers the webhook there.
  7. Sets the webhook signing secret, sets the final URL and deploys a second time.
  8. Checks that the site answers.

The second pass is the part that is easy to get wrong by hand, and it is a few lines in the script:

const webhookEnv = await payments.registerWebhook(appUrl);
await step("Redeploying with the final URL and webhook secret", async () => {
  vercel.setProductionEnv("NEXT_PUBLIC_APP_URL", appUrl);
  for (const [key, value] of Object.entries(webhookEnv)) vercel.setProductionEnv(key, value);
  return vercel.deployProduction();
});

registerWebhook() creates the endpoint at /api/webhooks/dodo or /api/webhooks/stripe, depending on BILLING_PROVIDER. The Dodo route returns 401 Invalid signature when verification fails, so a wrong secret is rejected, not trusted.

What it doesn't do. Social login apps can't be created by API, so setup shows you the callback URL to add (<your-domain>/api/auth/callback/google and .../github) and asks for the client ID and secret. Taking real money is also a separate decision: run npm run setup again with a live key, and it asks you to confirm before it does. After you deploy, npm run make-admin -- you@example.com makes your account an admin.

How much time does this save?

You skip the variable hunt, the two-pass deploy and the webhook registration. What remains is the part that needs you: your accounts, your domain and your own checks. Run through one test purchase before you trust it with real money.

Start at the Launchfast home page. Related reading: how to accept payments in your SaaS, how to set up a Postgres database and how to send verification and password reset emails.