Fast API
Sun 19 March 2023FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints. It is designed to be easy to use and provide high performance, with automatic validation of request and response data, and automatic generation of OpenAPI and JSON Schema documentation.
Installation
To install FastAPI, simply use pip:
pip install fastapi
You may also want to install the uvicorn
server, which is the recommended server to use with FastAPI:
pip install uvicorn[standard]
Usage
Here's an example of how to create a simple FastAPI app:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello, World!"}
In this example, we first import the FastAPI class from the fastapi module. We then create an instance of FastAPI() and store it in the app variable.
Next, we define a GET
endpoint for the root path / using the @app.get()
decorator. The root()
function returns a simple JSON object with the message "Hello, World!".
Finally, we start the app using the uvicorn server:
uvicorn main:app --reload
Here, main
refers to the name of our Python module (i.e., main.py
), and app
refers to the name of the instance of FastAPI()
.
Conclusion
In this example, we showed you how to create a simple FastAPI app that responds with a "Hello, World!" message. FastAPI is a powerful and easy-to-use framework for building high-performance web APIs with Python, and it has many more features and capabilities beyond what we covered here. We encourage you to explore the FastAPI documentation to learn more!