FastAPI is a modern, fast (high-performance), web framework used for building APIs in Python with minimal code. In this tutorial, we will walk through how to create a basic REST API using FastAPI in just 15 minutes.
Step 1: Setup the Environment
First, you will need to have Python installed on your machine. You can check if you have it installed by running the following command in your terminal:
python --version
If you don’t have Python installed, you can download and install it from the official Python website.
Next, you will need to install FastAPI and Uvicorn, a lightning-fast ASGI server which will run our FastAPI application. You can install them using pip:
pip install fastapi uvicorn
Step 2: Create the FastAPI Application
Create a new Python file named main.py
and open it in your favorite code editor. In this file, we will define our FastAPI application and endpoints.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, World!"}
This code creates a FastAPI application with one endpoint that returns a simple message when you access the root of the API.
Step 3: Start the FastAPI Application
To start the FastAPI application, run the following command in your terminal:
uvicorn main:app --reload
This command tells Uvicorn to run the FastAPI application defined in the main.py
file. The --reload
flag enables auto-reloading, which means the server will restart automatically when you make changes to your code.
Step 4: Test the API
Once you have started the FastAPI application, you can open a web browser and navigate to http://127.0.0.1:8000/
to see the message "Hello, World!" displayed.
You can also test the API using cURL or a tool like Postman. For example, to make a GET request to the root endpoint, run the following command in your terminal:
curl http://127.0.0.1:8000/
Step 5: Add More Endpoints
Now that you have a basic FastAPI application up and running, you can add more endpoints to create a more robust API. Here’s an example of adding a new endpoint that accepts a name
parameter and returns a personalized message:
@app.get("/hello/{name}")
def say_hello(name: str):
return {"message": f"Hello, {name}!"}
With this endpoint, you can now make a GET request to http://127.0.0.1:8000/hello/John
and receive a response containing the message "Hello, John!".
Step 6: Deploy the API
Once you have finished building and testing your FastAPI application locally, you can deploy it to a server so that it can be accessed by others. There are many hosting options available for Python applications, such as Heroku, AWS, and Google Cloud.
To deploy your FastAPI application, you will need to package your code and dependencies, set up the server environment, and run the application using a production-grade server like Gunicorn.
Conclusion
In this tutorial, we have covered the basics of creating a REST API using FastAPI in Python. FastAPI is a powerful and efficient web framework that makes it easy to build APIs with minimal code while providing high performance and robustness.
By following this tutorial, you should now have a good understanding of how to create a basic FastAPI application, define endpoints, and test the API. You can continue to explore FastAPI’s features and build more complex APIs to suit your specific use case.
I was wondering how it was 15 minutes then realized he's not using a database lmao. Add hours.
I have problems when call fastapi through fetch from react native only in release mod but in debug it is working?
I just randomly landed here and realised even though i have learnt FastAPI, i should have learnt it from you.
So precise and less blabbering makes this quality content.
great video!!! nice examples and easy to follow!
your video was great. i am interested to build sql database management server using fast api, could you provide it in the next your video? pleasee
Thank you 😀😀
Very well explained and done; detail information is provided, and the pace is perfect (considering I can pause and rewind). High quality. I've seen some of your other videos, and your talented at teaching!
you cut and paste a little bit too fast…
why are you using the terminal as wel ? cant you use the browser ?
arey erripooka first virtual environment create cheskomani cheppu, tala peekoni sachha
thaks
nice voice, good content organization and good presentation.
thank you.
Nice, simple easy to grasp
is there any library which will help me create CRUD webapp. I tried streamlit but I update all the data which takes long. I want something event driven.
Thank you! very nice explanation of FastAPI. Keep up the great work!
This really is one of the most useful and well structured tutorials (on any topic) I have ever seen. You have won yourself a big fan.
Fantastic tutorial, it's accessable and information dense with great coverage. Thank you!
Nice video, straight to the point. Cover a lot in 15 min.
Nice video, straight to the point, no fussing around, this channel deserves a subscription
Nice beginner tutorial. Thank you!