Lably
Skip to content

FastAPI

Web framework for Python

  • 7 min read
  • 3 chapters

Related on Lably

Through HTTP and Python (programming language)

Components

Pydantic

Pydantic is a data validation library for Python. While writing code in an IDE, Pydantic provides type hints based on annotations. FastAPI extensively utilizes Pydantic models for data validation, serialization, and automatic API documentation. These models are using standard Python type hints, providing a declarative way to specify the structure and types of data for incoming requests (e.g., HTTP bodies) and outgoing responses.

<span id="mwLQ"></span><span class="kn" id="mwLg">from</span><span class="w" id="mwLw"> </span><span class="nn" id="mwMA">fastapi</span><span class="w" id="mwMQ"> </span><span class="kn" id="mwMg">import</span> <span class="n" id="mwMw">FastAPI</span> <span class="kn" id="mwNA">from</span><span class="w" id="mwNQ"> </span><span class="nn" id="mwNg">pydantic</span><span class="w" id="mwNw"> </span><span class="kn" id="mwOA">import</span> <span class="n" id="mwOQ">BaseModel</span> <span class="n" id="mwOg">app</span> <span class="o" id="mwOw">=</span> <span class="n" id="mwPA">FastAPI</span><span class="p" id="mwPQ">()</span> <span class="k" id="mwPg">class</span><span class="w" id="mwPw"> </span><span class="nc" id="mwQA">Item</span><span class="p" id="mwQQ">(</span><span class="n" id="mwQg">BaseModel</span><span class="p" id="mwQw">):</span> <span class="n" id="mwRA">name</span><span class="p" id="mwRQ">:</span> <span class="nb" id="mwRg">str</span> <span class="n" id="mwRw">price</span><span class="p" id="mwSA">:</span> <span class="nb" id="mwSQ">float</span> <span class="n" id="mwSg">is_offer</span><span class="p" id="mwSw">:</span> <span class="nb" id="mwTA">bool</span> <span class="o" id="mwTQ">|</span> <span class="kc" id="mwTg">None</span> <span class="o" id="mwTw">=</span> <span class="kc" id="mwUA">None</span> <span class="nd" id="mwUQ">@app</span><span class="o" id="mwUg">.</span><span class="n" id="mwUw">post</span><span class="p" id="mwVA">(</span><span class="s2" id="mwVQ">"/items/"</span><span class="p" id="mwVg">)</span> <span class="k" id="mwVw">def</span><span class="w" id="mwWA"> </span><span class="nf" id="mwWQ">create_item</span><span class="p" id="mwWg">(</span><span class="n" id="mwWw">item</span><span class="p" id="mwXA">:</span> <span class="n" id="mwXQ">Item</span><span class="p" id="mwXg">):</span> <span class="c1" id="mwXw"># The 'item' object is already validated and typed</span> <span class="k" id="mwYA">return</span> <span class="p" id="mwYQ">{</span><span class="s2" id="mwYg">"message"</span><span class="p" id="mwYw">:</span> <span class="s2" id="mwZA">"Item received"</span><span class="p" id="mwZQ">,</span> <span class="s2" id="mwZg">"item_name"</span><span class="p" id="mwZw">:</span> <span class="n" id="mwaA">item</span><span class="o" id="mwaQ">.</span><span class="n" id="mwag">name</span><span class="p" id="mwaw">}</span>

Starlette

Starlette is a lightweight ASGI framework/toolkit, to support async functionality in Python.

Uvicorn

Uvicorn is a minimal low-level server/application web server for async frameworks, following the ASGI specification. Technically, it implements a multi-process model with one main process, which is responsible for managing a pool of worker processes and distributing incoming HTTP requests to them. The number of worker processes is pre-configured, but can also be adjusted up or down at runtime.

OpenAPI integration

FastAPI automatically generates OpenAPI documentation for APIs. This documentation includes both Swagger UI and ReDoc, which provide interactive API documentation that you can use to explore and test your endpoints in real time. This is particularly useful for developing, testing, and sharing APIs with other developers or users. Swagger UI is accessible by default at /docs and ReDoc at /redoc route.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication