How to make a simple API with python and bottle

How to make a simple API with python and bottle

Do you want to know how to make an API using Python and bottle? at the end of this lesson, you will be able to make your own API with python. First of all we will look what an API is.

What is an API?

API is the acronym for the application programming interface. In here I am not going to explain a lot about what is an API and what its benefits. you can follow this link to get to know about API. I will provide you with an example of an API. When you use a mobile application like Facebook Twitter or Instagram, you usually send and receive data from a database. This database is located in another country or a place, but somehow we have to send and receive data from it. But the mobile application can’t directly communicate with the database. As you can see below we have to use an API to connect front end mobile application with the database.

How API works?
How API works?

As you can see above, a mobile phone will send data to API and the API will store data inside the database. Whenever the mobile phone needs data from the database, API will fetch data from the database and send back those data to the mobile application. This is the basic usage of an API. There are a lot of programming languages and frameworks which we can use to make an API. They have their own advantages and disadvantages. Today I am going to use python and bottle framework to make an API. As a python developer, you can also use Django or flask to make an API. Let’s get started then.

Let’s make your first API

First of all, you need to install bottle framework to your computer. To do that we are going to use pip package installer. Please open a terminal and type the following command and hit enter.

pip install bottle

Pip will automatically install bottle to your system. If you don’t have pip in your system, please follow this article of my python basics tutorial. Now let’s make a new Python file named api.py., then we will type the following code. Please have a close look at this code first. Then I will explain one by one.

#Import run and get from bottle
from bottle import run, get
@get('/hello')
def getAllEmployee():
    return 'hello'
run(host='localhost', port=8080)

As you can see here, I am importing bottle using my first line of code. Then I am defining a route and named it as hello. Now, whenever I get a request to this route from any client, API will trigger this route and do the necessary steps. But in here we wrote the code to return a string. As soon as the hello route triggers, the API will return “Hello”. This is how we can make a simple API using Python and bottle. Now let’s see a bit of an advanced example. Please have a look at the code below.

from bottle import run, get
members_info  =  [{'name' : 'Anna','address':'Sri Lanka'},
              {'name': 'Brian', 'address': 'Dubai'},
              {'name': 'Joe', 'address': 'South Africa'}]
@get('/members')
def getAllEmployee():
    return{'members':members_info}
run(host='localhost', port=8080)

As you can see after importing run and get from bottle I have any initialized an array containing three objects. those three objects have names and addresses of few members. Whenever the API received a get request to the members route the API will return members array. You can see the result below. I have used postman to make a get request. And the API returned me the members array.

members API result
members API result

Now let’s look at the POST request example.

from bottle import run, get, post,request
members_info  =  [{'name' : 'Anna','address':'Sri Lanka'},
              {'name': 'Brian', 'address': 'Dubai'},
              {'name': 'Joe', 'address': 'South Africa'}]
#defining a POST route
@post('/addMember')
def add_member():
    #Getting parameters from the request
    new_member = {'name' : request.json.get('name'),'address' : request.json.get('address')}

    #Appending or adding new parameters which we got from the request to array.
    members_info.append(new_member)
    return {'members' : members_info}
run(host='localhost', port=8080)

As you can see here I have changed the route type from GET to POST. Then whenever the route gets a request, My code will get the parameters form the request. As you can see above, I have added new data which came with the API, to a new object. Then I have appended that object to existing members_info array. Finally I wrote the code to return newly updated members_info array. Below you can see, how I am sending data to API. I’m sending JSON data from Postman to my API.

This is one of the easiest way to build a simple API using Python and bottle. You can always connect the database inside those routes and store or retrieve data from the database. Feel free to look into my python database connection tutorial to get to know about Python database connection. Feel free to share this with your friends. If you have any concerns please don’t hesitate to contact me or comment under my posts. Have a nice day.

One Reply to “How to make a simple API with python and bottle”

Leave a Reply

Your email address will not be published. Required fields are marked *