FastAPI Quick overview
Advantages:
1. Data validation
Traditionally you would write code to check for values to make sure they are the correct type
FastAPI does this for you automatically
2. Auto Documentation
Can automatically generate documentation that also works kinda like a test script
3. Auto Completion and Code Suggestions
Installation:
pip:
on Linux:
import:
What is an endpoint?
Example:
Endpoints are:
/hello
/get-item
You would access it by the url below if app is hosted on localhost: localhost/hello
How to create an endpoint in fast-api:
Initialize the object
use the object followed by
.method("/endpoint-path")
once the endpoint is reached this will return the dictionary
Types of methods:
GET
- get infoPOST
- send infoPUT
- update infoDELETE
- delete info
Note: This is similar to CRUD in SQL
How to run the server:
Testing the API
You can access the link: http://127.0.0.1:8000/docs
This will bring you to the automatically generated API documentation
It documents all API endpoints and you can test them out as well by doing the following:
Select the API Endpoint and click Try it Out:
2. Click Execute:
3. Check the Response Body, or other data that you require:
Endpoint paths
Generate the invetory
Define path of api with GET method
Define the get_item function and specify what type of data is expected
Return data
Returns:
If you use ID 2 and it is not defined:
If you use anything else than integer as defined:
Import Path
used to detail endpoints out
usually provided to end users
Example:
Import the
Path
variableFirst argument has to be default (In our case
None
)Added description of item
Go to the /docs endpoint and now the endpoint should be documented:
Last updated