Getting started with APIs and FastAPI

ELAKIA VM
featurepreneur
Published in
4 min readJul 24, 2022

With the rise of the software as service(SaaS) model, APIs have become a very important part of software development. 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 APIs?

For that, we have to know to a web application has 3 integral parts.

1. Client

2. Server

3. API

Let me explain what are they. you, the user, are the client. The using the product. The server is the actual software backend where all the process is done. The client usually makes a request to the server and the server delivers the required information. The APIs are the bridge that connects the client and server and carries out this seamless transfer of data from the client to the server and vice versa.

In simple words, imagine you are sitting in a restaurant and you are ordering some food. The waiter takes the 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

An application programming interface is a way for two or more computer programs to communicate with each other, A set of definitions and protocols to build and integrate application software.

When the request is sent to the server it responds back to the data on the webpage is requested. There are several standardized formats for the transfer of data over APIs. The most common format is the JavaScript Object Notation (JSON) format.

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.API methods (HTTP methods).

There are 4 methods they are as follows:

  • GET
  • POST
  • PUT
  • DELETE

GET methods mean asking the API to get something for you

(JSON formate → server →client).

POST methods mean when you send some data to the server

(client →server).

PUT method means when you wish to edit some data that already exists on the server.

A DELETE method means sending a request to delete your data that exists on the server’s database.

In the modern world, web APIs have become increasingly important to the operation of data and businesses. As companies become more relay on the data, the importance of data communication continues to grow. We need a fast, high-performance and robust framework to build our APIs.

More about FastAPI

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+. Its performance can be compared with NodeJS and Go and it is rated as one of the fastest Python frameworks available. Tech giants like Microsoft, Netflix, and Uber amongst many other corporations are already started building their APIs with the FastAPI library. The framework is designed to optimize the experience so that we can write simple code to build production-ready APIs with best practices by default. Without further ado, let’s learn how can we build a robust API solution using FastAPI.

Installation and Setup

Create the virtual environment and install Fastapi using pip

$ pip3 install fastapi

We also required an ASGI server for production, such as Uvicorn.

$ pip3 install uvicorn

Hello World to FastAPI

Create a file app.py with:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
return {"message": "Hello World"}

Run the server with:

$ uvicorn app:app -- reload

Open your browser at http://127.0.0.1:8000/

You will see the JSON response as: {"message": "Hello World"}

Function for the square of the number:

@app.get("/square/{num}")async def predict_complex_model(request: Request, num: int = 0):      square = num**2      return {      "square"    : square      }

Function for a count of vowels:

@app.get("/vowel/{orig}")async def predict_complex_model(request: Request, orig: str = "0"):      count = 0      vowels = ['a', 'e', 'i', 'o', 'u', 'A', "E", "I", "O", 'U']      for letter in orig:           if letter in vowels:                 count += 1       return {       "Count of vowels"    : count       }

Conclusion:

That ends with a brief introduction to APIs. In the modern world, APIs play a major role in web development and it is vital for every software developer to know their basics. FastAPI is a lighter web framework for Python. It allows you to build APIs easily, quickly, and efficiently. If you’re interested in web application development, learning FastAPI will put you ahead of the curve.

Congratulations, your first program of FastAPI is live. I hope it was easy and helpful.

--

--