In this tutorial, we will be building our first web app using FastAPI, a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. FastAPI is built on top of Starlette for the web parts and Pydantic for the data validation and serialization parts.
Prerequisites:
- Python 3.6+
- pip (Python package installer)
Step 1: Install FastAPI and Uvicorn
First, we need to install FastAPI and Uvicorn, which is a lightning-fast ASGI server implementation, using the following pip commands:
pip install fastapi
pip install uvicorn
Step 2: Create a FastAPI App
Create a new Python file, for example, app.py
, and import the necessary modules:
from fastapi import FastAPI
app = FastAPI()
Step 3: Define a Path Operation
Define a path operation using a decorators such as @app.get()
or @app.post()
. For example, let’s create a basic "Hello World" API:
@app.get("/")
def read_root():
return {"Hello": "World"}
Step 4: Run the FastAPI App
To run the FastAPI app, use Uvicorn with the following command:
uvicorn app:app --reload
This command tells Uvicorn to run the app
module and use the app
FastAPI instance. The --reload
flag enables hot-reloading, which means that the server will automatically reload when you make changes to your code.
Step 5: Test the API
Open your browser or a REST client like Postman and navigate to http://localhost:8000
to see the "Hello World" response.
Congratulations! You have built your first web app using FastAPI. You can now explore more features such as request validation, dependency injection, and more.
Additional Resources:
- FastAPI Documentation: https://fastapi.tiangolo.com/
- Starlette Documentation: https://www.starlette.io/
- Pydantic Documentation: https://pydantic-docs.helpmanual.io/
I hope this tutorial was helpful for you to get started with building web apps using FastAPI. Happy coding!
Let me know if you want to see more tutorials with FastAPI in the future 🙂
It’s GitHub what?
Very helpful video!
This video is very useful for FastAPI beginner. I want to watch another video with FastAPI and Thank you very much.
Thanks!!
do you have a video for fastapi and templating that does it from the point of view of data coming from a remote db with more then one column?
Thank you🙏
It would be useful if you could increase the font size of the text.
FastAPI is delicious. So clean and easy
Thank you for your help mate 🙂
Good video, even for someone not knowing much about development I was able to understand this easily
Thanks G 🔥
how to request for the verbose = True content for ML stuff to display on the web page using FastAPI
I just re enabled youtube notification for your channel. Pls no hesitate to put more…
😊
Automatic Api documentation? Sold!
The way he says parameters really sounds weird
I have PyCharm that I use for Python, Can you do a video on that one too, build a FastAPI in PyCharm
very very nice, thank you very much!
I must correct my previous comment where I ignorantly complained about route thinking you were mispronouncing some sort of abbreviation for routine. I'm learning. And learning to make less ignorant comments.
very nice and helpful!