2 min read — Published 9 months ago

Building a Subscription System using Stripe, Node.js, and MongoDB - Part 5: Deploy to Production

Building a Subscription System using Stripe, Node.js, and MongoDB - Part 5: Deploy to Production

Featured Image

This is Part 5 in the series of guides on adding, managing, and keeping track of subscription payments using Stripe and Mongo for your SaaS app.


Subscription Payments are the bread and butter of a SaaS application; it's how you start generating revenue. In a well-implemented system, users need to first be able to change or cancel their plans, and second, undergo a trial phase to see if they would even like to pay for premium features. Typically, it's a hassle to set these up. In this set of guides, we will go through all the steps required to build a complete Subscription Payments system for your SaaS app.

In Part 5, we will prepare to host our application in Production by:

  1. Moving all environment secrets to a .env file
  2. Deploying the application to Heroku
  3. Setting up a Cloud Mongo instance
  4. Setting up the Production Stripe Webhook events

Let's start!

Step 1: Store secrets securely

We currently have all the secret keys hardcoded in the source code. This is a big no-no when we go into production. The best way to secure store secrets is in a .env file.

Let's create a .env

STRIPE_SECRET_KEY=sk_xxx PRODUCT_BASIC=price_xxx PRODUCT_PRO=price_xxx MONGODB=mongodb://localhost:27017/users STRIPE_WEBHOOK_SECRET=whsec_xxx TRIAL_DAYS=14

We can then use these values in our source code by installing dotenv.

npm install dotenv

In the first line of app.js


This will load up the secrets in .env and we can access them using process.env

In our src/connect/stripe.js

const Stripe = stripe(process.env.STRIPE_SECRET_KEY, { apiVersion: "2020-08-27", });

Step 2: Deploy on Heroku

The easiest way to deploy our application is using Heroku. We can use their CI/CD pipeline to deploy from a Github repository.

App deployment on Heroku Don't forget to add our environmental variables to the deployment.

Go to Settings > Config Vars and all the .env variables in

## Step 3: Set up a cloud Mongo instance on MongoDB Atlas

We will need a hosted MongoDB instance to work with our application. MongoDB Atlas is a great choice. Plus it's free.

Create a new account at MongoDB Atlas.

Create a new Cluster.

  1. Cloud Provider: AWS
  2. Region: Choose one with a free tier

We also need to add a user to be able to read the data. On the Atlas dashboard, add a new Database User with default privileges of Read and write to any database. You can however set up specific privileges for better security.

Creating a new DB user on Mongo Our database is now ready to be used. To get the connection string, click on Cluster button on the dashboard > Connect your application. The connection string looks like:

Add in your database username, password to the string. We can now add this to the MONGODB Config Var on Heroku.

Step 4: Configure Webhook for Production

We have been using a local webhook for the events from Stripe. This will not work in Production. We need to create a Production webhook key in Stripe Dashboard so that our deployed application can receive events. It's pretty simple to set up.

On the Stripe dashboard, head on over to Developers > Webhooks. Add a new endpoint.

Endpoint URL: Heroku deployment endpoint + /webhook

Events to send: customer.subscription.created and customer.subscription.updated


Adding a new Stripe webhook endpoint Copy over the newly generated Webhook Signing Secret and add it to the Config Vars as STRIPE_WEBHOOK_SECRET in Heroku. Redeploy the application so that the changes can take effect.

Final Thoughts

And there you have it! Your own Subscription Billing solution for your fancy SaaS app - built start to finish in a few hours without any headache.

Share this post

© 2024 Sukh

Made with Engyne