Introduction to FastAPI: A Beginner’s Guide

Posted by


FastAPI is a modern web framework for building APIs with Python. It is fast, easy to use, and highly efficient, making it a popular choice among developers. In this tutorial, we will cover the basics of FastAPI and show you how to create a simple API with it.

To get started with FastAPI, you will need to have Python installed on your machine. You can download and install Python from the official website. Once Python is installed, you can install FastAPI using pip, Python’s package manager. Open a terminal or command prompt and run the following command:

pip install fastapi

After installing FastAPI, you will also need to install a tool called uvicorn to run the FastAPI application. You can install uvicorn using pip as well:

pip install uvicorn

Now that you have FastAPI and uvicorn installed, let’s create a simple API using FastAPI. Create a new Python file, for example app.py, and open it in your favorite code editor. In this file, write the following code to create a basic FastAPI application:

from fastapi import FastAPI

app = FastAPI()

@app.get('/')
async def read_root():
    return {'message': 'Hello, World!'}

In this code snippet, we first import the FastAPI class from the fastapi module. We then create an instance of the FastAPI class called app. Next, we define a route for the root URL (/) using the @app.get decorator. The read_root function will be invoked whenever a GET request is made to the root URL, and it will return a JSON response with the message ‘Hello, World!’.

To run the FastAPI application, open a terminal or command prompt, navigate to the directory where your app.py file is located, and run the following command:

uvicorn app:app --reload

This command tells uvicorn to run the app object defined in the app.py file, with auto-reloading enabled. You should see output similar to the following:

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Now that your FastAPI application is running, you can open a web browser and navigate to http://127.0.0.1:8000/ to see your API in action. You should see a JSON response with the message ‘Hello, World!’.

FastAPI supports automatic generation of API documentation using the OpenAPI standard. To view the generated documentation for your API, navigate to http://127.0.0.1:8000/docs in your web browser. You will see an interactive documentation page where you can explore your API endpoints and make test requests.

In this tutorial, you learned the basics of FastAPI and how to create a simple API using it. FastAPI offers many more features and capabilities, such as request validation, dependency injection, and more. I encourage you to explore the FastAPI documentation to learn more about what you can do with this powerful web framework. Happy coding!

0 0 votes
Article Rating
21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@prinzecharming9239
2 months ago

You should have given credit to Tech with Tim cos this video is a complete replica of his video on same topic

@GoogleUser-nx3wp
2 months ago

import path section doesn't work anymore with the None default use "…" instead of "None"

@vanthele5623
2 months ago

In create-student method, we should convert Student object to dictionary object because Student object does not support item assignment now:
students[student_id] = student.dict()

@onclick-xt3mu
2 months ago

Thank you for you excellent tutorial, it's easier than django rest, Egypt

@AfricaAirandSpaceAssociation
2 months ago

Thank you so much for the energy and the application you put in sir. You just answered all my questions.

@Steph_D_1st
2 months ago

Nice course, but where isy cheat sheet?

@LieutenantGeneral
2 months ago

12:40

@hanSolowQ
2 months ago

24:40 Error Solution: def get_student(student_id: int = Path(…, description="Enter the ID of the student you want to view.", gt=0)):

@LawrenceS-ml5eg
2 months ago

Tommy sounds like a Nigerian

@abdulrasool925
2 months ago

I didn't understand the real difference between app.post, put and delete. If i run the functions under app.put and app.delete decorators in the given example with app.post, still it is working without any error.

@AdanAlinoor
2 months ago

many add

@AdanAlinoor
2 months ago

has alot of adds

@user-yi4zd6gh5h
2 months ago

poor teaching skills, wrong argumentation that's what you will find in this video.
Not recommended.

@isaacafedzi3368
2 months ago

how do use FastAPI for an image recognition model created using pytorch?

@eshabaweja
2 months ago

That /docs seems so handy!

@Phoenix-gi3gu
2 months ago

Great video, thanks. I am curious if there exists some conventions for the Error objects we return from time to time. When I want to analyse the output with JS it would get messy I guess if it doesn't follow any convention. Or maybe one could send an appropiate HTTPErrorCode (like 500 when internal server error is thrown)?

@akshayshinde8573
2 months ago

Path(None, description = "The ID of the student to view") code giving error return params.Path(
^^^^^^^^^^^^
File "/Users/akshayshinde/Documents/codespace/fastapi/env/lib/python3.12/site-packages/fastapi/params.py", line 182, in _init_
assert default is …, "Path parameters cannot have a default value"
AssertionError: Path parameters cannot have a default value

@stephaniecrispin674
2 months ago

If youre watching this in 2024, "python3 -m pip install fastapi" worked for me on a mac. And because I have 2 python versions, still I had to use python3 -m uvicorn myapi:app to start my server

@baloney_sandwich
2 months ago

jesus this is fast, didn't need to spend 32 hours to learn django syntax lol

@ransinghray3688
2 months ago

can Path have default parameter, I tried but got error. in time stamp 24.02 you have used it