Hello, fellow developer! So, you're thinking of leaving that 9-5, or maybe you've got a brilliant idea for a side hustle. You want to build a Software as a Service (SaaS) business, but the starting line seems so far away. This boilerplate is for you.
This isn't just a collection of files; it's a launchpad. It's designed to handle one of the most complex parts of any SaaS: getting paid. We've built a complete, realistic user authentication and payment flow using the power of SvelteKit 5 and the reliability of Paystack, with a focus on the Kenyan market (using KES).
This template will get you somewhere solid along your journey, letting you focus on building your actual product, not the plumbing.
This boilerplate comes pre-configured with a modern, powerful stack and essential SaaS features:
This boilerplate shines in its handling of different user scenarios. Here’s a breakdown of the flows you get out of the box:
This is the "happy path" for a new customer.
/checkout
page with their plan details pre-filled. They enter their email for the first time./payment-pending
page. In the background, the app verifies the payment with Paystack and—crucially—creates a session for them, logging them in automatically./app/dashboard
.This flow handles returning users who need to access protected content.
/app/dashboard
./login
, but cleverly appends their original destination: /login?redirect=/app/dashboard
.redirect
parameter to send them straight to /app/dashboard
./app
runs again. This time, the user is logged in, but the system sees they don't have an active subscription. It redirects them to the /paywall
, still remembering their final destination: /paywall?redirect=/app/dashboard
./app/dashboard
.This flow handles users who try to access protected content and need to sign up first.
/app/dashboard
./login?redirect=/app/dashboard
./register?redirect=/app/dashboard
, still preserving the destination./app/dashboard
.Ready to build your dream? Here’s how to get this boilerplate up and running in minutes.
Clone the Repository
git clone https://github.com/your-repo/sveltekit-paystack-saas.git
cd sveltekit-paystack-saas
Install Dependencies
pnpm install
Paystack Configuration
To process payments, you need to get your API keys from Paystack and set up a webhook to receive events.
A. Get Your API Secret Key:
.env
file:cp .env.example .env
.env
file and paste your key:SECRET_PAYSTACK_TEST_KEY=sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
B. Set Up Your Webhook for Local Development:
Paystack needs to send events to your running application (like charge.success
). When you're developing on localhost
, your app isn't accessible on the public internet. We can use a tool called ngrok to fix this.
Install ngrok: Follow the instructions on the ngrok website to download and install it.
Run Your SvelteKit App: Start your app on the default port (5173):
pnpm run dev
Expose Your Port with ngrok: In a new terminal window, run the following command:
ngrok http 5173
Get Your Webhook URL: ngrok will give you a public "Forwarding" URL (it will look something like https://random-string.ngrok-free.app
). Your webhook URL is this public URL plus the webhook path from this project:
https://random-string.ngrok-free.app/api/webhooks/paystack
Add the URL to Paystack:
Now, when a payment is made in your local test environment, Paystack will send the success event to the ngrok URL, which will forward it to your running SvelteKit application.
Run the Development Server
pnpm run dev
Your app is now running at http://localhost:5173
.
This boilerplate is a powerful starting point, but it is not a production-ready application. Here are the key limitations you should be aware of:
Map
as an in-memory store. This is fantastic for demos and getting started, but it resets every time the server restarts. You will need to replace this with a real database for production.This is where your journey truly begins. Here is a clear, step-by-step guide on how to extend this boilerplate into a full-fledged SaaS application.
This is your first and most important step.
users
, payments
, subscriptions
tables).src/lib/server/store.js
with actual database calls using your ORM. Instead of paymentStore.set(...)
, you'll be doing db.insert(...).values(...)
.Replace the mock session with a secure, production-ready authentication system.
src/hooks.server.ts
.login
and register
server actions in src/routes/login/+page.server.js
and src/routes/register/+page.server.js
to use your auth library to create real users in your database, including hashing passwords with a library like bcrypt
.This is the core of your SaaS business model.
subscription_status
(e.g., active
, inactive
, cancelled
) and a subscription_ends_at
(a timestamp) field to your users
table.src/routes/api/webhooks/paystack/+server.js
, when you receive a charge.success
event, don't just update the payment status. You should:subscription_status
to active
.subscription_ends_at
to the appropriate date (e.g., one month or one year from now).src/routes/app/+layout.server.ts
, change the check from the mock accessContext
to a real database query. Check if user.subscription_status === 'active'
and if user.subscription_ends_at
is in the future.This is the fun part!
/app/dashboard
page is your canvas. This is where you build the actual features that your customers are paying for./app
directory, so you can confidently build out your application here.Building a SaaS is a marathon, not a sprint. This boilerplate is designed to get you through the first few kilometers, handling the tricky parts so you can focus on your vision.
This is just the beginning.
Good luck on your journey. Now, go build something amazing.