Are you looking to build your first API but not sure where to start? In this tutorial, we’ll show you how to build your first API using FastAPI in just 10 minutes. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+.
Before we begin, make sure you have Python installed on your system. You can download Python from the official website (https://www.python.org/downloads/). Once Python is installed, you can install FastAPI by running the following command in your terminal:
pip install fastapi
Next, you’ll need to install a tool called Uvicorn to run our FastAPI application. You can install Uvicorn by running the following command:
pip install uvicorn
Now that you have FastAPI and Uvicorn installed, let’s build our first API. Create a new Python file called main.py
and open it in your favorite code editor. Here’s a simple example of a FastAPI application that exposes an endpoint to get a list of users:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/")
def get_users():
return {"users": ["Alice", "Bob", "Charlie"]}
In this code snippet, we import the FastAPI
class from the fastapi
module and create a new instance of the FastAPI
class named app
. We then define a route using the @app.get
decorator and the /users/
endpoint. The get_users
function returns a dictionary with a key users
that contains a list of user names.
To run our FastAPI application, open your terminal and run the following command:
uvicorn main:app --reload
This command tells Uvicorn to run the main.py
file and use the app
instance we created. The --reload
flag is optional but useful for development as it automatically reloads the server when you make changes to your code.
Once the server is running, you can open a web browser and go to http://127.0.0.1:8000/users/
to see the list of users returned by our API.
Congratulations! You’ve just built your first API using FastAPI. This is just a basic example, but FastAPI supports many more features such as request validation, dependency injection, API documentation, and more.
If you want to learn more about FastAPI, check out the official documentation at https://fastapi.tiangolo.com/. Happy coding!
A good animation for atom loving people https://youtu.be/r6JF0FEKMvs?si=ylAN6FH_orZqUTG_
Good work, I subscribed
Thank you for this tutorial. It was helpful for my work.
hello I have some ideas but I don't know anything about code so I use no code tools and I had some ideas but I would like to know what you think
Nice Explanation ❤
How to install visual studio code ? Is it free
Excellent explanation mate . Thanks
Wow I have understood APIS now… Thanks very much. However am requesting you to make a broader video explaining the second concept of using payload as it is more complex. Thanks in advance. Please tag me when you make the video.
Thank You !
Worst video on api dev sorry but I'm unliking the video😢
sir please tell me how to Fetch Vehicle Details In Bulk – Get Vehicle Insurance Details | RC Details in Excel using Python please make a video on this topic
Sir I create a api in node
How to I upload my api online
Thanks very good video for actually first steps to understanding the API ❤
at 8:54 when u run the program returns Sruvive = 1, isn't that wrong? because the logic is if (age <15) and sex == F
Any body pls tel me how to reshedule my api 570 exam. When iam doing this its shows that, in location : any prometric cente. I haven't seen any i can choose
Thanks
thanks
It made a lot more sense for me after watching this. Thank you brother.
🎯 Key Takeaways for quick navigation:
00:00 🚀 Introduction to API and FastAPI
– Definition of API: Simplifying the concept of APIs as a way for one program to use the logic of another program.
– Basics of API: Explaining key terms like endpoint, API key, API request, and response types.
– FastAPI Demo: Introducing FastAPI with a simple machine learning model example for predicting survival based on age and sex.
04:35 🛠️ Building Your Own API with FastAPI
– FastAPI Installation: Installing FastAPI and demonstrating a simple rule-based machine learning model in a few lines of code.
– API Development Process: Using decorators and logic to create an API endpoint with FastAPI.
– Testing API: Running and testing the API using FastAPI's interactive documentation.
07:48 🖥️ Consuming the API and API Gateway
– Consuming API: Writing a script to consume the API and retrieve responses based on input parameters.
– API Gateway: Discussing the importance of gateways in API development and demonstrating an alternative method using a payload for data protection.
– Practical Considerations: Emphasizing the need for a running API to interact with and the secure handling of data in API requests.
10:36 🔐 API Key and Final Remarks
– API Key: Explaining the concept of API keys as a means to protect against misuse and ensure controlled access.
– Secure API Usage: Recommending the use of API keys and avoiding passing queries in the URL for better security.
– Homework Assignment: Encouraging viewers to host their machine learning model in an API, access it through a browser, and gain a practical understanding of API functioning.
Made with HARPA AI
Thanks for this man!