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 withNodeJSandGo(thanks to Starlette and Pydantic).One of the fastest Python frameworks available.
- 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.Completioneverywhere. 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 ⭐ 31k(previously known as Swagger) and JSON Schema.
OpenAPI
FastAPI generates a "schema" with all your API using the OpenAPI standard for defining APIs.
"Schema"
A "schema" is a definition or description of something. Not the code that implements it, but just an abstract description.
API "schema"
In this case, OpenAPI ⭐ 31k is a specification that dictates how to define a schema of your API.
This schema definition includes your API paths, the possible parameters they take, etc.
Data "schema"
The term "schema" might also refer to the shape of some data, like a JSON content.
In that case, it would mean the JSON attributes, and data types they have, etc.
OpenAPI and JSON Schema
OpenAPI defines an API schema for your API. And that schema includes definitions (or "schemas") of the data sent and received by your API usingJSON Schema, the standard for JSON data schemas.
http://127.0.0.1:8000/openapi.json
What is OpenAPI for
The OpenAPI schema is what powers the two interactive documentation systems included.
You could also use it to generate code automatically, for clients that communicate with your API. For example, frontend, mobile or IoT applications.
Path Parameters
You can declare path "parameters" or "variables" with the same syntax used by Python format strings
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
Query Parameters
When you declare other function parameters that are not part of the path parameters, they are automatically interpreted as "query" parameters.
The query is the set of key-value pairs that go after the?in a URL, separated by&characters.
http://127.0.0.1:8000/items/?skip=0&limit=10
Request Body
class ClientAuth(Base):
"""
stores clients data that can access the apis
"""
client_secret = db.Column(db.String(72))
client_name = db.Column(db.String(50), nullable=False)
registered_by = db.Column(db.String(36), nullable=False)
Data Types
- int
- float
- str
- bool
- UUID
- datetime.datetime
- datetime.date
- datetime.time
- datetime.timedelta
- frozenset
- bytes
- Decimal