Building a Fast Restful API using FastAPI

Building a Fast Restful API using FastAPI

Although there are many python libraries for making API for machine learning or deep learning. Some of them you already know that is the popular Flask Framework. But If I will say that there is an API framework that automatically builds API documentation for all the API URLs and also in a faster way than Flask Framework. Then you will believe it. Many of you will not. In this entire tutorial, you will learn how to build a Fast Restful API using FastAPI.

Step 1: Install the necessary Libraries

First of all install the necessary libraries. These libraries are fastapi, uvicorn for production or deployment of the app.

Step 2: Create API Url

Let’s create an API URL. I am creating these URLs for demonstration purposes only. Use the following code in your project.

from fastapi import FastAPI

app = FastAPI()


@app.get("/api")
def index():
    return {"message": "Hello Data Science Learner"}


@app.get("/api/{price}")
def price_data(price: float):
    return {"message": f'Price of the item is {price}'}

Here you can see I am creating two URLs one is /api and other /api/{price}. The first one will display the default JSON response that “message”: “Hello Data Science Learner” and the other is according to the input in the URL.

Step 3: Run the APP

Now after defining the urls for the app run it using the uvicorn command. The app will run on the port 8000.

uvicorn main:app --reload

When you will visit the API for each of the URL created you will get the following things as described in the screenshot.

/api

/api/10

api url with the price

Documentation URL

/docs

swagger docs with api url

These are the get requests. Let’s make a post request API URL with Model Validation.

Step 1: Define the Class Model

First I will make a Class model for Post Request.

class Product(BaseModel):
    name: str
    description: str = None
    price: float

Step 2: Create the API URL

Now create an API URL that will accept the post request made by the server.

@app.post("/api/products/")
async def create_item(product: Product):
    return product

The above URL will accept all the things in defined in the model Product and you can do anything after receiving the post requests. Now you can do anything from the data you have get.

If you again open docs URL. You will the API URL and test from there using try it now button.

api url with the post and class model

These are some of the basic things you can Building a Fast Restful API using FastAPI.  There are some other things you can do the same as Flask APP like authorization e.t.c. You can read the official FastAPI documentation for more information. If you any other problem regarding this please contact us for the solution.

Bonus Content

Udemy Fastapi Course

If you are building web applications or want to deploy machine learning on web applications then here is the course that I will recommend to take to explore the FastAPI. In this course, you will learn how to use FastAPI with Flask to deploy your model and application.

Building Machine Learning Web Apps with Python

build web application machine learning udemy course

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner