Category: python
What is a FastAPI Service?
Published on 22 Aug 2026
Explanation
A FastAPI service is a Python application that exposes functionality through HTTP endpoints. Clients such as web applications, mobile applications, or other microservices can send requests to these endpoints and receive responses. FastAPI makes it easy to create REST APIs using Python type hints. A FastAPI application starts with a FastAPI object, and routes are created using decorators such as @app.get(), @app.post(), @app.put(), and @app.delete().
Code:
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def home():
return {
'message': 'Welcome to my FastAPI service'
}
# Run the application:
# uvicorn main:app --reload
Explanation
FastAPI is the web framework used to create the API, while Uvicorn is an ASGI server used to run the FastAPI application. Both can be installed using pip. It is recommended to create and activate a Python virtual environment before installing project dependencies. After installation, a FastAPI application can be started using the uvicorn command.
Code:
# Create virtual environment python -m venv venv # Activate on macOS/Linux source venv/bin/activate # Activate on Windows venv\Scripts\activate # Install FastAPI and Uvicorn pip install fastapi uvicorn # Save dependencies pip freeze > requirements.txt
Explanation
An API endpoint is a URL that performs a specific operation. FastAPI uses decorators to connect HTTP methods with Python functions. The @app.get() decorator creates a GET endpoint. When a client sends a GET request to the specified path, FastAPI executes the associated function and returns the result. Python dictionaries are automatically converted into JSON responses.
Code:
from fastapi import FastAPI
app = FastAPI()
@app.get('/hello')
def hello():
return {
'message': 'Hello from FastAPI'
}
@app.get('/about')
def about():
return {
'application': 'Employee Management API',
'version': '1.0'
}
# Run:
# uvicorn main:app --reload
Explanation
FastAPI supports path parameters and query parameters for receiving input from clients. A path parameter is part of the URL, such as /users/101. A query parameter is provided after a question mark, such as /users?name=John. FastAPI uses Python type hints to validate parameter values automatically. This makes API development simpler and helps prevent invalid input from reaching business logic.
Code:
from fastapi import FastAPI
app = FastAPI()
@app.get('/users/{user_id}')
def get_user(user_id: int):
return {
'user_id': user_id
}
@app.get('/users')
def search_users(name: str = ''):
return {
'search_name': name
}
# Examples:
# GET /users/101
# GET /users?name=John
Explanation
Uvicorn runs the FastAPI application as an ASGI web server. The --reload option automatically restarts the server when source code changes during development. FastAPI also provides interactive API documentation automatically. The Swagger UI is available at /docs, while an alternative ReDoc interface is available at /redoc. These tools allow developers to test API endpoints directly from the browser without needing a separate API client.
Code:
# Start the FastAPI service
uvicorn main:app --reload --port 8000
# API
# http://localhost:8000/
# Swagger UI
# http://localhost:8000/docs
# ReDoc
# http://localhost:8000/redoc
# Example response from GET /
# {
# 'message': 'Welcome to my FastAPI service'
# }