3.1. Introduction to FastAPI: Lightweight Python Framework to Build APIs
Hi! I’m Kumar Harsh, exploring Python nowadays, and I believe the best way to learn something is to write. I try to share my learnings in a series of blogs. I’ve already written blogs on topics like React and Node. You may check them as well. Happy Reading!
What is FastAPI?
FastAPI is a lightweight Python web framework built using other two frameworks starlette & pydantic which empowers it to tackle asynchronous requests with lightning speed. It’s at par with Node & Go.
Getting Started with FastAPI:
You need to install fastapi & uvicorn to get started with the development. uvicorn provides a local server to host the application. In addition, FastAPI comes loaded with an interactive API documentation tool called Swagger UI, which is automatically generated based on your API code and type hints. Swagger UI provides a user-friendly interface for exploring and testing your API endpoints. Additionally, FastAPI also generates JSON Schema documentation.
To access the Swagger UI documentation, you need to run your FastAPI application and navigate to the /docs
endpoint. For example, if your FastAPI application is running at http://127.0.0.1:8000
, you can access the Swagger UI documentation at http://127.0.0.1:8000/docs
. FastAPI also provides an alternative documentation feature using Swagger, ReDoc. Redoc is an open source tool for generating documentation from OpenAPI (Swagger) definitions. You can access it at http://127.0.0.1:8000/redoc
Installations and Setup
If you’re using a virtual environment, first activate it and then install the dependencies in the root folder. If not, navigate to the respective directory where you’ve saved your Python code using ‘cd/directory_path’ and type the following command in the terminal.
pip install fastapi uvicorn
Hello! World Program:
Now, run this program using this command in the terminal.
uvicorn main:app --reload
The URL marked under the red rectangle is the local server location where the application is running. Copy and paste the URL in the browser to run your application. The browser will return the message in the format you declared in the program.
Furthermore, you can access the Swagger UI documentation at [url/docs] in my case:http://127.0.0.1:8000/docs
You need not use any third-party API testing tool like Postman. FastAPI itself offers the API testing service with documentation.
ReDoc Documentation
ReDoc offers an interactive library of all your APIs defined in the codebase. You can also download OpenAPI Specification which contains all the important details related to your APIs in the JSON format. You can access it at http://127.0.0.1:8000/redoc
Path Parameter & Validations
With FastAPI, by using short, intuitive and standard Python type declarations, you get:
- Editor support: error checks, autocompletion, etc.
- Data “parsing”
- Data validation
- API annotation and automatic documentation
And you only have to declare them once. Reference: https://fastapi.tiangolo.com/tutorial/path-params/
Here, I’ve declared the validation checks lt(less than) & gt(greater than) with a description. If you try to override the validations the API will throw an Error: Unprocessable Entry.
Thanks for reading out, I’ll try to come up with more such blogs as I learn it further. You may always refer the official FastAPI Documentation.
Happy Reading !!.