Local Setup
This guide provides a step-by-step process to set up your local development environment for the Next.js Supabase SaaS Template. Follow the instructions to configure and run the application.
Prerequisites
Before starting the setup, ensure you have the following accounts ready:
-
Supabase Account
- Sign up or Sign in to your Supabase account.
- Create a new project in Supabase, which will serve as the backend for the authentication, database, and storage.
-
Stripe Account
- Sign up or Sign in to your Stripe account.
- This is required for handling payments and billing.
-
Resend Account
- Sign up or Sign in to Resend account.
- This is required for handling email (Marketing and Transactional) functionality.
Steps for Local Setup
Clone the Repository
Clone the repository to your local machine using git:
Navigate to the project directory:
Install Dependencies
Install the required dependencies using npm
(Node package manager):
Node.js installtion
node -v
. If Node.js is not installed, download and install it from the official Node.js website. We use v20.10.0 of nodejs.Setup Supabase
You can use either a local or cloud Supabase environment to run the application locally. We recommend using the local Supabase environment during development.
Local (Optional)
To set up a local Supabase instance, you need to install Docker. You can install Docker by visiting this link.
Once Docker is installed, run the following command:
This command will pull Supabase Docker images and run them as containers. Next, apply the migration and generate types:
Navigate to the supabase
directory in template:
Then generate the types:
Permission Issue
./generate-types.sh
command, use the following command to grant the necessary permissions: chmod ug+x generate-types.sh
command to give it proper access.To get your local Supabase credentials, you can run the following command in the root folder. This will provide the anon key, secret key, API URL, Studio URL, and other configuration information.
Go to the SQL section under Supabase Studio and run this command:
Cloud (Optional)
If you prefer to use the cloud version of Supabase, follow the cloud-specific instructions provided below:
First, you need to push the migration from your local environment to the cloud environment.
Install the Supabase CLI on your local machine.
Next, push the migration to the Supabase cloud environment using the following commands:
- List your Supabase projects: This command will display the available Supabase projects linked to your account.
- Link your local project to the Supabase cloud project: Replace $PROJECT_REF with the reference ID of the project you want to link to.
- Push the database migration to the cloud: This command applies your local migrations to the Supabase cloud environment.
- Go to the SQL section in Supabase Studio by visiting https://supabase.com/dashboard/project/_/sql/new, select your project, and then run this command:
Setup Resend
Setup Domain
To set up a domain, go to the following link and click the "+ Add Domain" button.
Resend will guide you through the DNS registrar setup. Once the domain name is verified, you can proceed to set up the API Key.
Setup API Key
To set up an API Key, go to the following link and click the "+ Create API Key" button.
If you're also setting up an audience, grant full permissions; otherwise, you can grant only "Sending access" permission.
Setup Audience (Optional)
To set up an audience, go to the following link, where you will see one audience created by default.
This audience ID will need to be added to your environment variables.
Setup Webhook (Optional)
To set up a webhook, go to the following link and click the "+ Add Webhook" button.
For local environments, you can use ngrok to create a tunnel for localhost:3000
. Add the webhook URL as follows: https://<ngrok-url>/api/webhook/resend
For events, the code is currently configured to accept only the contact.updated
event.
Setup Stripe
You need Stripe CLI installed for local development. You can download it from stripe official documentation.
Test Mode
Always use test mode to create products and prices. Once you're ready for production, you can copy them to production mode.
Note: To enable test mode, toggle the switch located in the top-right corner. This will allow you to create and manage products in test mode.
Create a Product
- Log in to your Stripe Dashboard.
- Navigate to Products in the left sidebar.
- Click the + Add product button.
- Fill in the product details (Name, Description, etc.) and click Save.
- After saving, you’ll be redirected to the product page where you can add pricing information.
Create a Price
- On the product page, click + Add price.
- Select the pricing model (Recurring or One-time).
- Set the price, billing interval (for subscriptions), and currency.
- Click Save.
You can add multiple prices for different billing frequencies (e.g., monthly and yearly plans). Each price will have its own price_id
.
Setup stripe webhooks
Webhooks allow Stripe to notify your app when certain events happen, like when a payment succeeds or a subscription is created.
- Sign in to your Stripe Dashboard.
- Go to Developers > Webhooks in the left sidebar.
- Click the + Add endpoint button.
- Set the endpoint URL. For local development, this will be your ngrok URL (e.g.,
https://<ngrok-url>/api/webhook/stripe
). - Select the events you want to listen to. Events for subscriptions include:
product.created
product.updated
price.created
price.updated
product.deleted
customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
checkout.session.completed
- Click Add endpoint to save.
To test webhooks locally, you need to use the Stripe CLI, which allows you to forward events to your local server.
Sign in with your stripe account:
Configure env variables
Copy the .env.example
file and rename it to .env.local
:
Open the .env.local
file and fill in the environment variables related to: Supabase, Stripe, Resend.
Start the application
- Run the application in development mode:
View your application
- Open your browser and navigate to:
Sync stripe data
- Open your browser and navigate to:
You should now see your application running locally. You're all set!