Set up Stripe subscriptions
A subscription is not one payment but a lifecycle. Your app needs to follow it through renewals, failures, upgrades and cancellations.

Key takeaways
- Create products and recurring prices in Stripe, start subscriptions with Checkout in subscription mode, and let the customer portal handle card updates and cancellations.
- Your database learns about the subscription only from verified webhooks. Map Stripe statuses to access in one place.
- Keep STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET in Backend → Secrets, and test renewals and failed payments before launch.
To set up Stripe subscriptions, create a product with a recurring price in your Stripe dashboard, send customers to Stripe Checkout in subscription mode from a server function, and keep your app's record of each subscription updated from Stripe's webhook events: created, updated, deleted, and invoice paid or failed. Stripe's hosted customer portal handles card changes, plan switches and cancellations. Your app never decides on its own that someone has paid; it reads that from verified events and grants access accordingly.
This page is the wiring. For the product side (which plans to offer, trials, limits and churn), see the subscription app guide; for one-time payments and the basics of checkout, see accepting payments.
What you need
- A Stripe account in test mode to start. Activating live payments needs your business and bank details.
- Startbase Cloud's Payments section. Payments run on your own Stripe account and Startbase adds no fee of its own. There are two paths: Stripe Connect, with no keys to paste, or a manual setup where you save `STRIPE_SECRET_KEY` in Secrets, scaffold checkout and billing, and register a webhook whose signing secret you save as `STRIPE_WEBHOOK_SECRET`. The Payments docs cover both; this article follows the manual path because every moving part of a subscription is visible there.
- Products and prices. For example, product `Studio` with two prices: $12 monthly and $120 yearly. Store the price ids (`price_...`) in a `plans` table or as settings, never hardcoded in the frontend as the source of truth.
The tables your app keeps
- `billing_customers`: `user_id` (or `org_id`), `stripe_customer_id`. One Stripe customer per paying account, created the first time they check out.
- `subscriptions`: `stripe_subscription_id`, `account_id`, `price_id`, `status`, `current_period_end`, `cancel_at_period_end`, `updated_from_event_at`.
- `stripe_events`: every webhook event id with a unique constraint, so retries never apply twice. The general pattern is in receiving webhooks.
Starting a subscription
- 01The user picks Studio yearly. The app calls a `create-subscription-checkout` function with only the plan key (`studio_yearly`).
- 02The function verifies the session, looks up the price id from the database, finds or creates the Stripe customer for this account, and creates a Checkout Session with `mode: subscription`, the price, `client_reference_id` set to the account id, and success and cancel URLs.
- 03The browser redirects to Stripe's hosted page. Card details never touch your app.
- 04On success Stripe redirects back to `/billing/success`. That page shows 'Finishing setup' and waits for the webhook to land; it does not grant access from the redirect, which anyone can visit.
The webhook events that matter
Subscribe your endpoint to these, at minimum, and handle each idempotently after verifying the `Stripe-Signature` header against `STRIPE_WEBHOOK_SECRET` on the raw body:
- `checkout.session.completed`: link the new subscription to the account using `client_reference_id`.
- `customer.subscription.created` and `customer.subscription.updated`: upsert the subscription row (status, price, period end, cancel at period end). Plan changes and cancellations scheduled in the portal arrive here.
- `customer.subscription.deleted`: the subscription has ended. Set status to `canceled` and remove access.
- `invoice.paid`: a renewal succeeded. Good moment to send your own receipt or reset monthly usage.
- `invoice.payment_failed`: a renewal failed. Show an in-app banner asking for a new card, and let Stripe's retry settings work.
A robust trick: when any subscription event arrives, fetch the subscription from Stripe's API and save that, rather than trusting the event's snapshot. Events can arrive out of order; the API always returns the current state.
Map statuses to access in one function
Stripe subscription statuses include `trialing`, `active`, `past_due`, `unpaid`, `canceled`, `incomplete`, `incomplete_expired` and `paused`. Write one `hasPaidAccess(account)` check used everywhere. A sensible starting policy: `trialing` and `active` get full access; `past_due` keeps access for a grace period with a warning banner; everything else falls back to the free plan. A user who cancels keeps access until `current_period_end`, because `cancel_at_period_end` is true and the status stays `active` until then.
Let the customer portal do the rest
Turn on Stripe's customer portal in the dashboard and choose what customers may do: update payment methods, view invoices, switch between your prices, cancel. A `create-portal-session` function creates a portal session for the signed-in account's customer id and returns its URL. That one button replaces an entire billing settings screen, and every change it makes comes back to you as webhook events you already handle.
Paste-ready brief
Add Stripe subscriptions to my app. STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET are in Secrets. Plans: Free, Studio monthly and Studio yearly, with Stripe price ids stored in a plans table. Create billing_customers, subscriptions and stripe_events tables. Write create-subscription-checkout (takes a plan key, finds or creates the Stripe customer, starts Checkout in subscription mode with client_reference_id) and create-portal-session. Write a public stripe-webhook function that verifies the signature on the raw body, records each event id once, and on checkout.session.completed and customer.subscription created, updated or deleted fetches the subscription from Stripe and upserts it. On invoice.payment_failed show a banner asking for a new card. Put access rules in one hasPaidAccess function: active and trialing get Studio, past_due keeps it for 7 days, everything else is Free. The success page waits for the webhook instead of granting access.
Test the whole lifecycle
- Test mode and test cards. Card `4242 4242 4242 4242` with any future date succeeds; Stripe's test card list includes cards that decline or require authentication.
- Webhooks locally and in preview. Stripe's CLI can forward events to your function and trigger specific events on demand.
- Renewals without waiting a month. Stripe's test clocks let you move a test customer's time forward to see renewals, failed payments and trial endings.
- The checklist: subscribe, renew, fail a renewal and recover, upgrade monthly to yearly, cancel and keep access until period end, and receive the same webhook twice.
Pitfalls
- Granting access on the success redirect. Anyone can open that URL. Access follows the webhook-updated subscription row only.
- The secret key in the browser. `STRIPE_SECRET_KEY` can move money and read customer data. It lives in Secrets and edge functions only; the publish security check also looks for leaked Stripe keys.
- Skipping signature checks. Without verifying `Stripe-Signature`, anyone can post a fake 'subscription active' event to your endpoint.
- Test and live mixed up. Test and live mode have different keys, price ids and webhook secrets. Going live means replacing all three, and registering a live webhook endpoint.
- Plan changes without a policy. Moving from monthly to yearly mid-cycle raises proration questions: charge the difference now, or switch at renewal? Decide, configure the portal to match, and say it on your pricing page so support never has to improvise.
- One customer per signup instead of per account. If a team shares a workspace, the Stripe customer belongs to the workspace, not to whoever clicked first. Otherwise the subscription leaves with that person.
- Mobile apps. Digital subscriptions sold inside iOS and Android apps generally have to use the stores' own billing. Web checkout fits web apps; check store rules before adding Stripe to a phone app.
Frequently asked questions
Which Stripe webhook events do I need for subscriptions?
At minimum checkout.session.completed, customer.subscription.created, customer.subscription.updated, customer.subscription.deleted, invoice.paid and invoice.payment_failed.
What happens when a subscription renewal fails?
The subscription usually moves to past_due while Stripe retries the card on the schedule set in your dashboard. Show a banner asking for a new card, keep access for a short grace period, and downgrade if the subscription ends up canceled or unpaid.
Do I need to build a billing settings page?
Mostly no. Stripe customer portal handles card updates, invoices, plan switches and cancellations. Your app needs one button that opens a portal session, plus the webhooks that report the changes.
How do I test monthly renewals without waiting?
Use Stripe test mode with test clocks, which advance a test customer time so renewals, trial ends and failed payments happen in minutes.

