Build FastAPI really fast !!
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:
- Clearly detailed step by step documentation https://fastapi.tiangolo.com
- Automatically generated API documentation with openAPI Schema and Swagger UI
- Ability to perform async operation and seamlessly work with various classes of code spread in main, models, schema, database, crud etc.
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
- Main.py : Defines all the decorators, routers, classes
- Model.py: Stores models for data attributes
- schema.py: Stores schema procedures for data attributes
- database.py: Contains database configurations and connections
- 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
- You can create FastAPI project with just one click using PyCharm
PyCharm adds packages and code template needed to get going !!
2. Test your endpoints right in 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
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 !!