Next.js Subscription Payments Starter
The all-in-one starter kit for high-performance SaaS applications.
Features
- Secure user management and authentication with Supabase
- Powerful data access & management tooling on top of PostgreSQL with Supabase
- Integration with Stripe Checkout and the Stripe customer portal
- Automatic syncing of pricing plans and subscription statuses via Stripe webhooks
Demo
- https://subscription-payments.vercel.app/
Architecture
Step-by-step setup
When deploying this template, the sequence of steps is important. Follow the steps below in order to get up and running.
Initiate Deployment
Vercel Deploy Button
The Vercel Deployment will create a new repository with this template on your GitHub account and guide your through a new Supabase project creation. The Supabase Vercel Deploy Integration will set up the necessary Supabase environment variables and run the SQL migrations to set up the Database schema on your account. You can inspect the created tables in your project's Table editor.
Should the automatic setup fail, please create a Supabase account, and a new project if needed. In your project, navigate to the SQL editor and select the "Stripe Subscriptions" starter template from the Quick start section.
Configure Auth
In your Supabase project, navigate to auth > URL configuration and set your main production URL (e.g. https://your-deployment-url.vercel.app) as the site url.
Next, in your Vercel deployment settings, add a new Production environment variable called NEXT_PUBLIC_SITE_URL
and set it to the same URL. Make sure to deselect preview and development environments to make sure that preview branches and local development work correctly.
[Optional] - Set up redirect wildcards for deploy previews
For auth redirects (email confirmations, magic links, OAuth providers) to work correctly in deploy previews, navigate to the auth settings and add the following wildcard URL to "Redirect URLs": https://*-username.vercel.app/**
. You can read more about redirect wildcard patterns in the docs.
[Optional] - Set up OAuth providers
You can use third-party login providers like GitHub or Google. Refer to the docs to learn how to configure these. Once configured, you can add them to the provider
array of the Auth
component page.
[Maybe Optional] - Set up Supabase environment variables (not needed if you installed via the Deploy Button)
If you've deployed this template via the "Deploy to Vercel" button above, you can skip this step. Otherwise navigate to the API settings and paste them into the Vercel deployment interface. Copy project API keys and paste into the NEXT_PUBLIC_SUPABASE_ANON_KEY
and SUPABASE_SERVICE_ROLE_KEY
fields, and copy the project URL and paste to Vercel as NEXT_PUBLIC_SUPABASE_URL
.
Congrats, this completes the Supabase setup, almost there!
Configure Stripe
Next, we'll need to configure Stripe to handle test payments. If you don't already have a Stripe account, create one now.
For the following steps, make sure you have the "Test Mode" toggle switched on.
Create a webhook
We need to create a webhook in the Developers
section of Stripe. Pictured in the architecture diagram above, this webhook is the piece that connects Stripe to your Vercel Serverless Functions.
- Click the "Add Endpoint" button on the test Endpoints page.
- Enter your production deployment URL followed by
/api/webhooks
for the endpoint URL. (e.g.https://your-deployment-url.vercel.app/api/webhooks
) - Click
Select events
under theSelect events to listen to
heading. - Click
Select all events
in theSelect events to send
section. - Copy
Signing secret
as we'll need that in the next step. - In addition to the
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
and theSTRIPE_SECRET_KEY
we've set earlier during deployment, we need to add the webhook secret asSTRIPE_WEBHOOK_SECRET
env var.
Redeploy with new env vars
For the newly set environment variables to take effect and everything to work together correctly, we need to redeploy our app in Vercel. In your Vercel Dashboard, navigate to deployments, click the overflow menu button and select "Redeploy" (do NOT enable the "Use existing Build Cache" option). Once Vercel has rebuilt and redeployed your app, you're ready to set up your products and prices.
Create product and pricing information
Your application's webhook listens for product updates on Stripe and automatically propagates them to your Supabase database. So with your webhook listener running, you can now create your product and pricing information in the Stripe Dashboard.
Stripe Checkout currently supports pricing that bills a predefined amount at a specific interval. More complex plans (e.g., different pricing tiers or seats) are not yet supported.
For example, you can create business models with different pricing tiers, e.g.:
- Product 1: Hobby
- Price 1: 10 USD per month
- Price 2: 100 USD per year
- Product 2: Freelancer
- Price 1: 20 USD per month
- Price 2: 200 USD per year
Optionally, to speed up the setup, we have added a fixtures file to bootstrap test product and pricing data in your Stripe account. The Stripe CLI fixtures
command executes a series of API requests defined in this JSON file. Simply run stripe fixtures fixtures/stripe-fixtures.json
.
Important: Make sure that you've configured your Stripe webhook correctly and redeployed with all needed environment variables.
Configure the Stripe customer portal
- Set your custom branding in the settings
- Configure the Customer Portal settings
- Toggle on "Allow customers to update their payment methods"
- Toggle on "Allow customers to update subscriptions"
- Toggle on "Allow customers to cancel subscriptions"
- Add the products and prices that you want
- Set up the required business information and links
That's it
I know, that was quite a lot to get through, but it's worth it. You're now ready to earn recurring revenue from your customers. 🥳
Develop locally
If you haven't already done so, clone your Github repository to your local machine.
Next, use the Vercel CLI to link your project:
vercel login
vercel link
Setting up the env vars locally
Use the Vercel CLI to download the development env vars:
vercel env pull .env.local
Running this command will create a new .env.local
file in your project folder. For security purposes, you will need to set the SUPABASE_SERVICE_ROLE_KEY
manually from your Supabase dashboard (Settings > API
).
Use the Stripe CLI to test webhooks
Install the Stripe CLI and link your Stripe account.
Next, start local webhook forwarding:
stripe listen --forward-to=localhost:3000/api/webhooks
Running this Stripe command will print a webhook secret (such as, whsec_***
) to the console. Set STRIPE_WEBHOOK_SECRET
to this value in your .env.local
file.
Install dependencies and run the Next.js client
In a separate terminal, run the following commands to install dependencies and start the development server:
npm install
npm run dev
# or
yarn
yarn dev
Note that webhook forwarding and the development server must be running concurrently in two separate terminals for the application to work correctly.
Finally, navigate to http://localhost:3000 in your browser to see the application rendered.
Going live
Archive testing products
Archive all test mode Stripe products before going live. Before creating your live mode products, make sure to follow the steps below to set up your live mode env vars and webhooks.
Configure production environment variables
To run the project in live mode and process payments with Stripe, switch Stripe from "test mode" to "production mode." Your Stripe API keys will be different in production mode, and you will have to create a separate production mode webhook. Copy these values and paste them into Vercel, replacing the test mode values.
Redeploy
Afterward, you will need to rebuild your production deployment for the changes to take effect. Within your project Dashboard, navigate to the "Deployments" tab, select the most recent deployment, click the overflow menu button (next to the "Visit" button) and select "Redeploy" (do NOT enable the "Use existing Build Cache" option).
To verify you are running in production mode, test checking out with the Stripe test card. The test card should not work.
A note on reliability
This template mirrors completed Stripe transactions to the Supabase database. This means that if the Supabase database is unavailable, the Stripe transaction will still succeed, but the Supabase database will not be updated, and the application will pass an error code back to Stripe. By default, Stripe will retry sending its response to the webhook for up to three days, or until the database update succeeds. This means that the Stripe transaction will eventually be reflected in the Supabase database as long as the database comes back online within three days. You may want to implement a process to automatically reconcile the Supabase database with Stripe in case of a prolonged outage.