Schedule & send rent invoices to tenants at regular intervals using Django & AfricasTalking API


If you are a caretaker or a landlord of a rentals property, chances are every end of the month, you draft paper invoices and then walk door to door issuing each tenant with their specific invoice. Not only is this tedious and time consuming but also you risk walking to a tenant's door and finding that they are not in and you have to come again at a later time or the next day(s).
A hypothetical property, Kwetu Apartments is one such business that has hitherto been doing things this way and they have decided that it is time they embrace technology and become a digital first business by leveraging on automation tools to handle the otherwise daunting manual invoicing method.


The owner of Kwetu Apartments approached our team of software developers and after lengthy talks, we decided to develop a portal for them that will:
1.Send text message reminders to tenants with invoice details every 30th of each month.
2.process rent payments using either card or mpesa and then update each tenant's status as either paid or not.
The portal will then send text messages to those who have not paid to remind them to make payments.
The assumption here is that we have already developed the logic to process tenants payments and now what we have to do is implement the logic to send text reminders every 30th each month. In case you need an article on payment processing then go through this post I wrote a few days back.


Create a new project and start a new app:

django-admin startproject tenants_project .

python startapp invoices

Remember to add invoices to the list of installed apps in tenants_project/
Let's quickly create a database class that will house all our tenants and their details:

from django.db import models

# Create your models here.
class Tenant(models.Model):

    def __str__(self):

Go ahead and makemigrations then migrate. Register the model in Also create a superuser, login to the admin section of the project and add a few tenant instances. After that we can begin sending them invoices messages every month!


Stop the server but retain the virtual env. Install AfricasTalking helper library by:

pip  install africastalking

Now log in to their website. Click on 'Sandbox' and from the resulting dashboard click on 'Settings' then 'API Key'. Click to generate a new key which you will copy and store it safe as we are using it in a bit.


We then move on to install a package that will help us with task scheduling. While still on terminal do this:

pip install apscheduler

Create a file within invoices called and add the code below to it:

from .models import Tenant
import africastalking

# Initialize SDK
username = "sandbox"    
africastalking.initialize(username, api_key)

sms = africastalking.SMS

def send_reminders():
    for tenant in tenants:
        invoice_message=f'Hello, {}, your rent payment of shs 10,000 is due on demand. Please plan to pay before 5th.'
        response =sms.send(invoice_message, [str(]) 

Next, create another file within invoices called and add the code below to it:

from apscheduler.schedulers.background import BackgroundScheduler
from . import invoice_update

def start():
    scheduler = BackgroundScheduler()
    scheduler.add_job(invoice_update.send_reminders, 'interval', seconds=10)

So here we are scheduling a reminder with invoice details for all the tenants in our database. For testing purposes we are running that task every ten seconds but in production we would put a cron job to run every 30th. Save that file then move to invoices/ and update it like so:

from django.apps import AppConfig

class InvoicesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'invoices'

    def ready(self):
        from . import updater

So every time our project boots, our scheduler runs in the background and does its thing.
Let's take it for a spin and see what's what:

python runserver

Head over to your AT Sandbox and launch it.
You will see an image like below:
sandbox home
Click on the SMS card and you will see:
Great, our tenants got the invoice messages as intended by Kwetu Apartments.


In this article we have eliminated the need of Kwetu Apartment caretakers and landlords to draft paper invoices and then walk door to door delivering them to tenants. We developed a solution that is robust and scalable that can server as many users as possibles with minimal human input.
The code to this project can be found at my repo.

Thanks for reading and coding along. Go ahead and add creativity and share what you will come up with.
If you have any questions or comment then leave them below or on my Twitter.
Cheers and happy hacking!