Built-in dependencies in FastAPI
FastAPI provides a set of built-in dependencies that are readily available for use. These built-in dependencies cover common scenarios such as handling HTTP requests, security, and data validation. They simplify the process of incorporating essential functionality into your API routes. Examples of built-in dependencies include Request
, Query
, and Path
, which allow you to extract information from incoming HTTP requests.
main.py
Sum of Two Numbers
In this example, the sum_numbers() function depends on the a and b query parameters. FastAPI will automatically inject the values of these query parameters into the function when called.
- FastAPI Application Setup: The code begins by importing the FastAPI framework and creating an instance of the
FastAPI
class, which is namedapp
. Thisapp
instance serves as the foundation for building a web application using FastAPI. It is the central component that handles incoming HTTP requests and routes them to the appropriate functions for processing. - API Endpoint with Route Parameters: The code defines an API endpoint using the
@app.get("/sum")
decorator. This endpoint is accessible through an HTTP GET request at the path “/sum.” The endpoint is associated with a Python function namedsum_numbers
. This function takes two parameters,a
andb
, both of which are expected to be integers. These parameters represent the two numbers that the user wants to add. - Endpoint Logic and Documentation: Inside the
sum_numbers
function, the code calculates the sum of the two input numbersa
andb
and returns the result. The function also includes a docstring, which provides documentation about what the function does, the arguments it expects, and the value it returns. This documentation is useful for developers and tools to understand the purpose and usage of the endpoint.
Python3
from fastapi import FastAPI app = FastAPI() @app .get( "/sum" ) async def sum_numbers(a: int , b: int ): """ Calculates the sum of two numbers. Args: a: The first number. b: The second number. Returns: The sum of a and b. """ return a + b |
Then run the below command in terminal
uvicorn main:app --reload
You can now access the FastAPI endpoint at the following URL:
http://localhost:8000/sum?a={value}&b={value}
http://127.0.0.1:8000/sum?a=10&b=20
Output
FastAPI – Dependencies
In this article, we will explore FastAPI – Dependencies. FastAPI is a state-of-the-art, high-performance web framework for creating Python-based APIs. It is built on the principles of dependency injection and type hinting, which facilitate the creation of clean, maintainable, and scalable code. This article delves deeper into topics such as Dependency Injection, Dependency Provider, Dependency Scope, and the use of dependencies in FastAPI, all with practical examples. Following this, we will examine the advantages of incorporating dependencies.
FastAPI’s dependency injection approach stands out as one of its pivotal features. Dependencies refer to the components that a class or function requires for its operation. When a function or class is invoked, FastAPI automatically supplies the dependencies as declared in the code.