When software and applications are being developed, it is sometimes important that the teams are able to share their work and publicize it as a service.

Sometimes, even within the same software, there could be components that require integration with another internal module.

For instance, if our software is a ticketing system, there could be a calculation module that might calculate the final cost including the taxes and vats. This can be easily made possible with the APIs.

The ability to build and maintain an API is important. This article will teach you how to build a RESTful API in Python using the Flask framework.

Table of Contents

You can skip to a specific section of this Python API tutorial using the table of contents below:

  • What are APIs?
  • What is Flask?
  • Creating a Basic Flask Application
  • A Flask Application with Multiple Functionalities
    • 1. Variable Path
    • 2. Returning JSON Responses
  • Final Thoughts

What are APIs?

API is one of the most heard terms in the field of software engineering. Even most of the non-technical users of the internet are somewhat familiar with this term API. It stands for application programming interface. APIs can be identified as a software intermediary that acts as an interface for applications to communicate with each other, accept external requests, and respond appropriately abiding by the set of rules that have been specified by the developers.

APIs usually are independent entities that offer functionalities to the external world without having to publicize how its internals work. Therefore, APIs really support the concept of building software as blocks of codes. Moreover, this helps developers to start writing software not from the scratch but rather as pre-built reusable components. Not only does this speed up the software development process but also helps teams to collaborate and even offer their APIs as a service to other teams and organizations.

Usually, when we are talking about APIs, we are naturally referring to REST APIs. REST APIs are programming interfaces with a specific set of predefined rules and these interfaces can be accessed via HTTP adhering to these set of rules. There are also methods supported by the developers of the API allowing the external parties to interact with it. These methods are as follows.

  1. GET - These types of requests cater to the functionality of requesting data from a remote server and retrieving it at the client’s side.
  2. POST - These types of requests are associated with sending data to the remote server.
  3. PUT - These requests are also used to send and modify data in a remote server.
  4. DELETE - These requests are used to delete any persisting data in a remote database.

As an example, let’s take a look at how we send a GET request and retrieve data from a remote server.

import requests
import json
response = requests.get("http://api.open-notify.org/iss-now.json")
print(json.loads(response.text) )

In this, we are requesting the current position of the International Space Station using the open API provided by Open Notify. If we look at the code, we are importing the requests library and using the requests.get() method to retrieve a response. Then we are printing it using the json.loads() method and the text attribute of the response we received.

Output:

{'message': 'success', 'iss_position': {'latitude': '41.9740', 'longitude': '150.8389'}, 'timestamp': 1600591768}

This is how we can interact with an API as the client. In this tutorial, we will be developing the server-side application to cater to the needs of clients who are requesting our service. We will be using Flask for that and let’s now take a look at what it is and what we can do with it.

#python #flask #api #web-development #programming

How to Build a RESTful API in Python with Flask
2.30 GEEK