Skip to main content
In this guide, we’ll build a subscription-based SaaS application using zopio’s built-in payments package. You’ll learn how to implement subscription plans, handle payments, and manage user access to premium features.

1. Create a new project

Terminal
This will create a new project with the name subscription-saas and install the necessary dependencies.

2. Configure your environment variables

Follow the guide on Environment Variables to set up your environment variables. For a subscription SaaS application, you’ll need to set up the following variables:
.env

3. Set up the database schema

Let’s create the database schema for our subscription SaaS application. We’ll need tables for subscriptions, plans, and features.
packages/database/schema/subscriptions.ts

4. Set up the payments package

Let’s configure the payments package to handle subscriptions and payments.

4.1 Create the Stripe client

packages/payments/stripe-client.ts

4.2 Create the subscription service

packages/payments/subscription-service.ts

5. Create the API routes

Let’s create the API routes for handling subscriptions and webhooks.

5.1 Create the checkout API route

apps/app/app/api/subscriptions/checkout/route.ts

5.2 Create the webhook API route

apps/app/app/api/webhooks/stripe/route.ts

6. Create the pricing page

Let’s create a pricing page to display the available subscription plans.
apps/app/app/(authenticated)/pricing/page.tsx

7. Create the pricing components

Let’s create the components for displaying and subscribing to plans.
apps/app/app/(authenticated)/pricing/components/pricing-plans.tsx

8. Create a feature access hook

Let’s create a hook to check if a user has access to specific features.
packages/payments/use-feature-access.ts

9. Create the feature access API route

apps/app/app/api/features/access/route.ts

10. Add internationalization support

Let’s add internationalization support to our subscription SaaS application using the internationalization package. First, let’s create translation files for our supported locales.
dictionaries/en.json
dictionaries/tr.json
Then, update our components to use the translations:
apps/app/app/(authenticated)/pricing/page.tsx

11. Run the application

Now you can run your subscription SaaS application:
Terminal
Visit http://localhost:3000/pricing to see your subscription plans.

Next Steps

  • Implement a dashboard to show subscription status and usage metrics
  • Add the ability to upgrade/downgrade plans
  • Implement usage tracking for metered features
  • Add invoice history and payment management
  • Implement trial periods for new users
You’ve successfully built a subscription SaaS application using zopio and the payments package! If you have any questions, please reach out to us on Twitter or open an issue on GitHub.