Build FastAPI really fast !!

Part 1: Getting started with FastAPI

Krishna K
3 min readMay 12, 2022

Over the years, I had explored multiple API frameworks — there is always a lot of options to choose from based on your business case. If your language of choice is Python, then you are battling with Flask Vs. FastAPI frameworks.

For me, choice was straightforward as my plan was to use FastAPI only for backend and React as my front end.

If you are looking at a python framework that can do both front end and backend at a cost of fast building, I would recommend Flask Framework. Another important point to highlight — Building FastAPI is really Fast !!

Unbeatable features of FastAPI:

  1. Clearly detailed step by step documentation https://fastapi.tiangolo.com
  2. Automatically generated API documentation with openAPI Schema and Swagger UI
  3. Ability to perform async operation and seamlessly work with various classes of code spread in main, models, schema, database, crud etc.
FastAPI is really Fast !!

This is the first of a series of blogs, I would be writing on FastAPI. In this I would cover the broader picture of how to get the app running in your local IDE.

Organize your files:

Well organized project directory is critical to building an API, because you can always make changes to models, schema when you need to add additional attributes to an endpoint.

My preferred choice is to split Project into files shown below

  1. Main.py : Defines all the decorators, routers, classes
  2. Model.py: Stores models for data attributes
  3. schema.py: Stores schema procedures for data attributes
  4. database.py: Contains database configurations and connections
  5. crud.py: Create, Read, Update and Delate classes

Getting started with the Fast API setup:

Enter the PyCharm

PyCharm is my IDE of choice for all Python projects because

  1. You can create FastAPI project with just one click using PyCharm

PyCharm adds packages and code template needed to get going !!

PyCharm to Create FastAPI

2. Test your endpoints right in HTTP Client

HTTP Client

main.py

Some people would like to name this as app.py, but I would prefer main.py as it doesn’t matter as long as your run

uvicorn main:app — reload

Template main.py code with all necessary packages for initial run

In the above decorator ‘@app.get(“/”)’ assigns an URL to the function in this “/” is the route to the endpoint in your local browser.

Once you have main.py set you can run the app by entering
uvicorn main:app — reload” in your terminal.

By default, app runs on port 8000, if port is already occupied by other app — you can easily switch to other ports.

http://localhost:8000/

In my upcoming posts I will detail basic and advanced concepts

Part 2: Concepts needed to build routes

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

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.