ADVANCED DATA COLLECTION: HOW TO FETCH KOBOTOOLBOX API TO WEB INTERFACE USING DJANGO

Ogunmolu Oluwaseun
5 min readSep 20, 2022

--

Designed by Oluwaseun Ogunmolu

My first year at the University of Abuja, Nigeria, stands out in my mind. One of my friends explained to me his plan to create an API that would link student matriculation numbers to exam results and other data. As a technology novice, I immediately looked up the meaning of API on the internet and was astounded to discover the significance of API in the development of both web and mobile applications.

APIs are critical in modern software development, and their values cannot be over-emphasized. API serves as a link between the backend and the frontend. In other words, it aids in the integration of applications in order to execute a certain purpose centered on data sharing and execution of pre-defined processes. In this article, you will learn how to fetch Kobotoolbox API using Django.

You may want to ask, why do I need to fetch Kobotoolbox API? The answer is rather obvious. Kobotoolbox can help you if, for example, you run a website and need to show users specific information on the data collected from respondents in real-time. You can do this by connecting the Kobotoolbox API to your website so that users can view the information in real-time.

You may also want to ask this question, what exactly is Django? Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. In other words, Django is a Python framework for developing web applications.

Now that you know the importance of API and Django, let’s jump into the main business which is to fetch Kobotoolbox API with Django.

SECTION 1

The first thing we need to do is to create a form in Kobotoolbox form builder.

In order to make this article short, create a simple form with just three questions, just like the form below

Now that we’ve created a form, the next thing is to save the form and deploy the form to server, then open the link in a new web tab and click “open”

We need to fill out the form with fictitious data. So, if you’ve constructed the form, enter some dummy data in it, then click the “Submit” button.

Now, we need to do some configurations on the form. Go to settings, click on “sharing” then click “Anyone can view form” and also “Anyone can view submissions made to this form”.

The next step is to go to “DATA” interface, click on “Downloads” and make sure that “Export Select Many questions as…” is set to “Single and separate columns”. Click on “Save selection as…” and name your export settings. “Django” will be is the default name for this form, you can name the form whatever name you wish, then click on “EXPORT”.

Now, it’s time to locate the API. What you need to do is to use this url https://kobo.humanitarianresponse.info/api/v2/assets/ to locate the API then use ctrl+F to find the form name in the Current Endpoint of the API Root. Then copy the url. The last thing to do is to obtain the token using https://kobo.humanitarianresponse.info/token

SECTION 2

In this section, we are going to learn how to install Django and also how to fetch the Kobotoolbox API.

Please note this, before Django could work on your PC you need to make sure that you have Python installed. So, if you don’t have Python installed on your PC, please visit https://www.python.org/ to download and install.

The first thing to do in order to install Django is to open a command prompt on your PC and create a directory where Django will be installed. After that, create a virtual environment and activate the virtual environment. Then, install Django in the virtual environment using “pip install Django” and “pip install requests”.

Now, that we’ve successfully installed Django, the second step is to create Django project. To create a Django project, use this simple syntax which is Django-admin startproject. After that, open the project in any IDE (Integrated Development Environment) of your choice. The next step is to create an app in the Django project. To do that, write “python manage.py startapp api”.

The next step is to do the configuration on the project settings. In the “settings.py” locate “TEMPLATES” and write [BASE_DIR, “templates”] in ‘DIRS’. After that, “INSTALLED_APPS” write ‘api.apps.ApiConfig’.

The next settings are on the project folder. Open a folder on the project and name it “templates” then open a file and name it “home.html”. In the api app, open a file and write “urls.py” to create a python script.

In views.py import json and requests, declare the url and the token then pass the context and return it in the render function. Follow the link below.

Now we need to import the views into urls.py and declare the path.

After this, include the api urls into the project urls. This can be done by importing include and declaring the path function.

On the home.html, use the code in the code palette to view the total deployment count on the web interface.

The last thing we need to do is to run the server using “python manage.py runserver” in the terminal. Congratulations! You have successfully fetch Kobotoolbox API using Django. Also note that, this method can be used to fetch any API to web interface using Django.

Click on this link to download the source code https://github.com/lagrandecode/kobotoolbox_api

Click on the clap button and follow for more updates.

--

--

Ogunmolu Oluwaseun

Data Scientist, Software Developer — I analyse with PowerBi but I love Tableau. I mostly code on Java but Python is fun. ARCGIS is good but QGIS is life.