Introduction to FastAPI
A beginner’s guide to creating web APIs using Python
As a second-year Computer Engineering student, I recently started exploring Python and came across FastAPI. At first, I did not fully understand what an API was or why it was needed. After exploring FastAPI and trying some simple examples, I found it to be a useful and beginner-friendly way to understand how applications communicate with each other. In this blog, I will explain the basics of FastAPI, how to create a simple API, and some of the features that make FastAPI popular.
What Is an API?
API stands for Application Programming Interface. In simple terms, an API allows two software applications or different parts of an application to communicate with each other. It can be thought of as a messenger that carries a request from one part of a system and returns the required response.
For example, when we use a food delivery application, the frontend may ask the backend for a list of restaurants or the status of an order. Similarly, when we log in to a website, the frontend sends our login details to the backend, which checks the information and sends a response. The API provides the communication interface through which these requests and responses can take place.
What Is FastAPI?
FastAPI is a modern Python web framework used to create APIs. It helps developers build endpoints that receive requests and send responses. An endpoint is a specific URL through which an application can perform a particular task.
One of the things I found useful about FastAPI is that a relatively small Python program can become a working API. Its syntax is clear, and it provides features such as automatic validation and interactive API documentation.
How FastAPI Fits into an Application
To understand where FastAPI fits into a real application, it helps to look at the complete flow. The frontend is the part of the application that the user interacts with. When the user requests information, the frontend sends the request to a FastAPI endpoint. FastAPI is usually part of the backend and acts as the API layer through which the frontend communicates with the backend logic and services.
The backend processes the request and, when necessary, interacts with a database where the application’s data is stored. The required information is then returned through the backend and FastAPI to the frontend, where it can be displayed to the user. In simple terms, the flow can be understood as: Frontend → FastAPI/API layer → Backend logic and services → Database → Backend/FastAPI → Frontend.
This does not mean that FastAPI itself is the database or the entire backend. FastAPI provides the framework and API endpoints that help handle communication between the user-facing application and the services and data behind it.
Why FastAPI Is Popular
Some of the features that make FastAPI popular include:
· Fast performance — FastAPI is designed for high-performance API development.
· Easy to learn — Developers who already know Python can start with simple examples.
· Fast development — Its features can reduce the amount of code needed for common API tasks.
· Automatic validation — FastAPI can validate incoming data based on Python type hints and models.
· Automatic documentation — It generates interactive API documentation automatically.
· Type hints — Python type hints make the code easier to understand and help with validation.
· OpenAPI and JSON Schema — FastAPI supports standards used for describing and documenting APIs.
Installing FastAPI
To install FastAPI, I used the following command:
pip install “fastapi[standard]”
After installation, I created a Python file named main.py. This file contains the code for the API.
My First FastAPI Program
Here is a simple FastAPI program:
from fastapi import FastAPI
app = FastAPI()
@app.get(“/”)
def read_root():
return {“message”: “Hello, FastAPI!”}
The FastAPI() function creates the application. The @app.get(“/”) line creates a GET endpoint for the root URL. When a request is made to this endpoint, the read_root() function runs and returns a Python dictionary.
FastAPI converts the returned Python data into a JSON response that can be sent back to the client. This makes it easy to exchange structured data between applications.
To start the development server, I can use:
Get Durga Menon’s stories in your inbox
Join Medium for free to get updates from this writer.
fastapi dev
Once the server is running, the application can be opened locally at http://127.0.0.1:8000. When the root URL is opened in a browser, the response will look similar to:
{“message”: “Hello, FastAPI!”}
Understanding HTTP Methods
APIs use HTTP methods to describe the type of operation a client wants to perform. Some commonly used methods are:
· GET — Used to retrieve data.
· POST — Used to send or create data.
· PUT — Used to update existing data.
· DELETE — Used to delete data.
For example, in a library application, GET could be used to retrieve information about books, POST could be used to add a new book, PUT could be used to update book information, and DELETE could be used to remove a book.
Creating a GET Endpoint with Parameters
FastAPI also allows us to include parameters in the URL. For example:
from fastapi import FastAPI
app = FastAPI()
@app.get(“/items/{item_id}”)
def read_item(item_id: int):
return {“item_id”: item_id}
Here, item_id is a path parameter. The type hint : int tells FastAPI that the value should be an integer. FastAPI can validate the incoming value based on this type information.
Receiving Data Using POST
When an API needs to receive structured data, FastAPI can work with Pydantic models for data validation. For example:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Book(BaseModel):
title: str
author: str
price: float
@app.post(“/books/”)
def create_book(book: Book):
return {
“message”: “Book added successfully”,
“book”: book
}
In this example, the Book model defines the expected fields: title, author, and price. FastAPI uses the model to understand and validate the data received by the API.
This is useful because an application should not blindly accept any data sent to it. Validation helps ensure that the information received by an endpoint follows the expected structure and data types.
Interactive API Documentation
One feature I found especially useful is FastAPI’s automatic interactive documentation. When the application is running, FastAPI provides documentation at:
This opens an interactive documentation page commonly known as Swagger UI. It allows developers to view available endpoints and try API requests directly from the browser.
FastAPI also provides alternative documentation through ReDoc at:
These documentation features make it easier for developers to understand and test an API while building it.
What I Learned from FastAPI
While learning FastAPI, I understood several basic backend concepts more clearly. I learned that an API provides a way for different parts of a software system to communicate. I also became more familiar with HTTP methods, JSON responses, endpoints, routes, request bodies, and data validation.
The examples also helped me understand that a framework such as FastAPI can simplify the process of creating the API layer of an application. Instead of writing everything from scratch, developers can use FastAPI’s built-in features for routing, validation, documentation, and request handling.
Conclusion
FastAPI is a powerful and modern Python framework for building web APIs. For a beginner, it provides a relatively simple way to understand how applications communicate through APIs. Features such as automatic validation, support for JSON data, type hints, and interactive documentation make API development easier and more organized.
My next goal is to explore how FastAPI can be connected to a database and used for operations such as creating, reading, updating, and deleting data. I would also like to learn more about authentication and deployment. This introduction has given me a basic understanding of how FastAPI works and where it fits into a real application.
This blog is created as part of learning at Pillai College of Engineering (PCE). For more details, visit https://www.pce.ac.in





