FAST API
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.
Requirements
Python 3.6+
FastAPI stands on the shoulders of giants:
· Starlette for the web parts.
· Pydantic for the data parts.
Installation
pip3 install fastapi // For Python 3
pip install fastapi // For Python 2
pip3 install uvicorn // For Python 3
pip install uvicorn // For Python 2
With that, we have FastAPI and Uvicorn installed and are ready to learn how to use them. FastAPI is the framework we will use to build our API, and Uvicorn is the server that we will use the API we build to serve requests.
Create First API
Recommended by LinkedIn
from typing import Optiona
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "Boss!!!"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
Copy the code above to a file named main.py, and just like that, you have a fully functional API application with some best practices like automatic documentation and serialization built in. You will learn more about those features next.
This code defines your application, but it won’t run on itself if you call it with python directly. To run it, you need a server program. In the steps above, you already installed Uvicorn. That will be your server.
Run the First API App With Uvicorn
python3 -m uvicorn main:app --reload
The highlighted line in the output shows the URL where our app is being served in our local machine. Since we used --reload for development, when you update your application code, the server will reload automatically.
Check the Response
Open browser to http://127.0.0.1:8000, which will make browser send a request to application. It will then send a JSON response with the following:
{"Hello":"Boss!!!"}
That JSON message is the same dictionary that we returned from the function in our application. FastAPI takes care of serializing the Python dict into a JSON object and setting the appropriate Content-Type.
Check the Interactive API Documentation
Now open http://127.0.0.1:8000/docs in your browser.
You will see the automatic interactive API documentation provided by Swagger UI:
The browser-based user interface documenting your API is provided and integrated by default. You don’t have to do anything else to take advantage of it with FastAPI.
Check the Alternative Interactive API Documentation
Now, go to http://127.0.0.1:8000/redoc in your browser.
You’ll see the alternative automatic documentation provided by ReDoc:
For more insights and detailed information on A