Let’s imagine you’re developing a SaaS app that allows dentists to send appointment reminders to their patients. You’ve hooked up your code to the appointment calendar (maybe using Nylas or Cronofy), and you’ve set up your integration with Twilio which will send an SMS to each patient the day before their appointment, like this:
You’re almost ready to launch, but you need to make sure your users don’t overuse your product by sending more SMS messages than they have paid for. You decide you want your customers to top up their account with credit (for example, via a Stripe payment), and you’ll charge them for every SMS they send.
Moola can help you to achieve this functionality very quickly, with just a few lines of code in your app, and without adding any more tables to your database.
Head over to moola.dev and register an account, then set up an application and grab your API Key.
It will look something like this:
Make your API Key accessible to your application, for example by setting it to your environment variables. If you’re running on Heroku, for example, you could use the CLI command
heroku config:set MOOLA_API_KEY=c91edcf6-1716-4417-831a-1da2aa8e3ce5
Interacting with the Moola API is as simple as sending HTTP requests, and you can use your favourite library for this. For the examples in this post, we’re using Axios, a Promise-based HTTP client for Node.js.
A nice clean way to make it easy for Moola to be called from various places in your application’s code would be to create a helper file like
moola.js, for example:
const axios = require("axios");
For more information about authentication with the Moola API, check the docs.
First of all, let’s set up a wallet for each of your customers. Your code will look something like the following example, which might be called from a controller or a model hook from your ORM.
// you got a new customer! let's create a wallet for them
When your customer wants to add funds to their balance, just send Moola information about how much they paid. For example, here’s some code that you could use when handling a Stripe webhook after a successful charge.
For more information about transactions, check out the docs.
// your customer just paid some money via Stripe
When your customers send SMS appointment reminders, you want to bill them for the usage. You can use transactions again, this time debiting the customer’s wallet. For example:
// you're about to send an appointment reminder for your customer
One of the handy things about Moola is that it can provide some basic rules and controls around wallet balances for you. For instance, since we specified earlier that this wallet must not have a negative balance (using the
balanceCanBeNegative parameter), any attempts to create transactions for this wallet which would cause the balance to fall below zero will fail. By handling this case in our app’s logic, we can make sure customers can only spend what they’ve paid for.
Let’s add some code to the error handler (
.catch()) from our
onReminderCreation() function to demonstrate this. For more information about error codes, check out the docs
// this is the .catch handler in our onReminderCreation() function
You might want to show your users their balance history, so they can see how much they have spent, and when. This can be done like this:
// you're about to get all the transactions for a specific customer's wallet
Wondering about pagination? Check out the docs.
Similarly, you can get information about a single transaction for a particular reminder, like this:
// get details about a specific transaction
Your app is ready to launch - just think of all the clean, shiny teeth your users will have 😁.
Moola is an API for the accounting parts of your application, with flexible pricing plans to meet your business needs. We handle the money plumbing like wallets, transactions and balances for you, so you can build your application quicker and focus on delighting your users.