Introduction to APIs with Python FastAPI | by Moosa Ali | CodeX | Oct, 2021

Moosa Ali

With the rise of the Software as a service (SaaS) model, APIs have become a very important part of software development. A short summary of the SaaS model is that software developers no longer need to create installable desktop applications, they simply create their applications on the web and deploy them on a web server and then expose the endpoints to the world using APIs.

But what is an API?

That’s what we’re here to answer!

A Web-Application in a SaaS model has 3 integral parts.

Number one should be self-explanatory. You, the user, are the client. The one using the product. The server is the actual software backend where all the processing happens. The client usually requests something from the server and the server then delivers the required information. Vice versa also occurs but more importantly, this delivery of data/information is where APIs come in. APIs are the bridge that connects the client to the server and carries out this seamless transfer of data from the client to the server and vice versa.

In even simpler words, imagine you are sitting in a restaurant and you order some food. The waiter takes your order to the cook and returns back with your food. In this scenario, you are the client (the user), the cook is the server (the back-end where the processing/cooking happens) and the waiter is the API (the bridge between you and the cook and the carrier of your food).

More about APIs

API stands for “Application Programming Interface”. I may have made an API sound quite simple but in reality, there are a lot more things involved.

Whenever you load a website, you are actually invoking the API of that website which then brings you the requested webpage. When you type in the domain name of the website you want to access and hit ‘Enter’, a request is sent to the server of that website with all the required information and in the response, the entire webpage is returned. This response is interpreted by your browser and it handles the loading of the webpage and all of its assets e.g. when you got to www.google.com, the HTML file of the website is returned to your browser. This HTML file contains information regarding what images to load or any other elements such as the search bar, the location of these assets on the webpage, and links to other webpages (buttons). Reading and displaying this file is your browser’s responsibility and you don’t really need to worry about it.

Another way in which APIs are used is for the simple transfer of data. No fancy assets, no web pages, just plain simple data. There are several standardized formats for the transfer of data over APIs. The most common format is the JavaScript Object Notation (JSON) format. Other formats include XML, YAML, etc. This might be confusing for some people. Why would we request only data like this over the internet? But you use APIs like this more than you realize. When you use the weather application on your phone or on your computer, the application usually uses a third-party API to get the weather information. When you hit refresh, it sends a request to the API with some information such as your location or some other authorization parameter (If required by the API) and in the response, the API returns the weather information to the application in the JSON format, the application then formats the data according to its interface and displays it to you. This is just one use case of an API. Keep reading at the end I’ll show you how you can build your own API and then see how we can request data from a third-party API.

Another important thing to mention here is that all of this communication is done using the HTTP protocol. HTTP is the standard protocol used for all sorts of communication over the web. HTTP has its own technicalities but we’re not here to talk about it so let’s return to APIs.

There is one last thing that I need to mention before moving on to building an API; the API methods (HTTP methods).

There are a total of 4 actions that you can perform using an API.

A GET request is like you (the client) asking the API to get something for you (a webpage or simply data in JSON format — server → client)

A POST method is used when you send some data to the server (client → server). This is done when you fill out a form when signing up for a social media app. The server stores the data that you send to it.

A PUT method is used when you wish to edit some data that already exists on the server. This method simply changes your existing data on the server.

A DELETE request — surprise surprise!! — sends a request to delete your data that exists on the server’s database.