DEV Community

Mary-softeng
Mary-softeng

Posted on

FASTAPI

what is FastAPI?
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
The key features are:
•Fast: Very high performance, on par with NodeJS and Go.
•Fast to code: Increase the speed to develop features by about 200% to 300%.
•Fewer bugs: Reduce about 40% of human (developer) induced errors.
•Intuitive: Great editor support. Completion everywhere. Less time debugging.
•Easy: Designed to be easy to use and learn. Less time reading docs.
•Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
•Robust: Get production-ready code. With automatic interactive documentation.
•Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema
How to install FastAPI
Pipenv is used while setting up the development environment for our APIs. Pipenv makes it easier to isolate your development environment irrespective of what things are installed on your machine. It also lets you pick a different Python version than whatever is installed on your machine. It uses Pipfile to manage all your project-related dependencies. Use pip install FastApi in a command prompt.

Alt Text

Once installed you can activate the virtual environment by running the command pipenv shell
Alt Text

FastAPI Hello World
Let’s get some practice with FastAPI! We’ll take a look at a simple Hello World! and break down the pieces.
Alt Text
To start the server, we need to run the following command:

Let’s break this down:
•main: refers to the file name
•app: refers to the object of FastAPI created inside the hello.py file
•--reload: parameter that makes the server restart after the code changes
Let’s break down our Hello World! code:
•Line 1: We import FastAPI, which is a Python class that provides all the functionality for the API.
•Line 3: We create an instance of the class FastAPI and name it app. This is the app referred to by uvicorn in the above command.
•Line 5: We create a GET path.
•Line 6: We define the function that will execute whenever someone visits the above path.
•Line 7: We return a response to the client whenever the route is accessed.
Basic FastAPI building blocks
Let’s explore some of the building blocks of FastAPI, including path parameters, query parameters, and request bodies.
Path parameters
Path parameters help scope the API call down to a single resource, which means you don’t have to build a body for something as simple as a resource finder.
These parameters are enclosed in curly brackets {}, and they offer a way for you to control the representation of specific resources. They’re placed before the query string and within the path of an endpoint.
Let’s take a look at how to use them:
Alt Text
The value of the path parameter course_name will be passed to the function read_couse() as the argument course_name.
Query parameters
Query parameters are optional. In FastAPI, function parameters that aren’t declared as part of the path parameters are automatically interpreted as query parameters.
Let’s look at some sample code:
Alt Text

The query is the set of key-value pairs that comes after the question mark ? in a URL, separated by an ampersand &.
Take a look at the following URL:
http://localhost:8000/courses/?start=0&end=10
Its query parameters are:
start with a value of 0 and end with a value of 10.
In line 8 of the code, we pass the two query parameters that our API would expect.
Request body
A request body is data sent by the client to your API. To declare one in FastAPI, we can use Pydantic models.
Let’s see an example of how we can do this:
Alt Text

Let’s break this down:
•Lines 1-3: We import the required packages.
•Line 5: We declare the request data model.
•Line 11: We create an instance of the FastAPI class.
•Line 13: We create a POST path.
•Line 14: We add the request data model to the path.

https://colab.research.google.com/drive/1_LLiBhSQ1RZuHnnL7cE6ML9mwVV1h_Nb?usp=sharing

Top comments (0)