Create your first API in just 10 minutes with this FastAPI tutorial

Posted by


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!

0 0 votes
Article Rating
44 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@maz2045
28 days ago

A good animation for atom loving people https://youtu.be/r6JF0FEKMvs?si=ylAN6FH_orZqUTG_

@maz2045
28 days ago

Good work, I subscribed

@jermesastudio
28 days ago

Thank you for this tutorial. It was helpful for my work.

@chrisder1814
28 days ago

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

@omg_kundan
28 days ago

Nice Explanation ❤

@sethupathyasokan7412
28 days ago

How to install visual studio code ? Is it free

@sethupathyasokan7412
28 days ago

Excellent explanation mate . Thanks

@evilgenius8444
28 days ago

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.

@user-km6rm3td5r
28 days ago

Thank You !

@rajkumarmaurya2918
28 days ago

Worst video on api dev sorry but I'm unliking the video😢

@TechnicalEinstein
28 days ago

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

@balaji5703
28 days ago

Sir I create a api in node
How to I upload my api online

@wakeelkumarsah4476
28 days ago

Thanks very good video for actually first steps to understanding the API ❤

@amrithMilW
28 days ago

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

@princephilip497
28 days ago

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

@kiranbakale8207
28 days ago

Thanks

@rahulshendre7089
28 days ago

thanks

@the_80s_gamer
28 days ago

It made a lot more sense for me after watching this. Thank you brother.

@fairmeir
28 days ago

🎯 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

@JoelSamJohnson
28 days ago

Thanks for this man!