Build FastAPI really fast !!

Krishna K
3 min readMay 20, 2022

Part 3: Creating GET, POST, PUT, DELETE requests

Requests interact with your database and perform a requested action, whether it is to fetch, add, change or delete records in database.

In this post, I will show you how to create requests, for reference on endpoint creation please visit my previous post.

All API frameworks support GET, POST, PUT and DELETE requests. Fast API makes it fast and easy.

Making Requests using FastAPI:

Understanding of decorators and statements, shown in my previous post is critical to making these requests to database.

GET request:

This is the most common request used using an API endpoints. In the below code snippet we are getting data related to a specific id.

car_by_id function looks for id match in db database. If the id is found it will return the ‘data’ or else it will return ‘No car with id’.

How it works:

  • GET Request is sent to db (database)
  • db returns the data records requested

POST request:

This request is used to add new data record to a database. POST requests are also widely used in API authentication process, a POST request/ call is made to authentication server to receive a token response.

In the code snippet below, we are posting or adding data to database db using add_car function.

In the add_car function adding a new_car data attributes and it will be appended to database db. save_db makes sure that the appended data is saved in the database as a data record.

Note: Will cover models CarInput and CarOutput models in upcoming Pydantic data models blog (part 5).

How it works:

  • POST request is sent to db (database) with new record parameters
  • New data is appended to database and saved

PUT request:

This request is used to modify / change a data record that is already existed in the database. Very handy request to edit data in a database.

In the code snippet below, we are changing a data record in database db using change_car function. This function changes data in the matched records and uses save_db to save record in database db.

How it works:

  • PUT request is sent to db where data record need to be changed
  • New data record is saved in the database

DELETE request:

This request is used to delete a record from database db. DELETE request is a straight forward request.

In below code snippet, remove_car function looks for the id provided and deletes the record. Once deleted record is saved using save_db function.

How it works:

  • DELETE request deletes data records from database

Key Takeaways:

  • Choosing a right requests is critical part of building API as they define the specific function
  • It is important to have a database to connect so API can process the requests

In my upcoming posts, will detail basic and advanced concepts in

Part 1: Getting started with FastAPI

Part 2: Concepts needed to build routes

Part 4: Middleware & Azure application Insights

Part 5: Pydantic data models

Part 6: Define schemas

Part 7: Connect with SQL server to pull real-time data

Part 8: Secure your FastAPI & adding authentication

Part 9: Deploy using docker & AKS

Part 10: Bringing it all together

Thank you for reading !!

--

--

Krishna K

Data Scientist, The World Bank. I blog about data science, machine learning, and building web apps & APIs.