Skip to content

Starlite logo

PyPI - License PyPI - Python Version

PyPI - License PyPI - Python Version

Coverage

Quality Gate Status Maintainability Rating Reliability Rating Security Rating

Language grade: Python Total alerts

Discord Matrix

Medium

Starlite is a light, opinionated and flexible ASGI API framework built on top of pydantic and Starlette.

The Starlite framework supports plugins, ships with dependency injection, authentication , OpenAPI specifications-generation – among other common API-framework components such as middleware and guards.

Installation

pip install starlite

Extras

To install the extras required for the built-in Testing helpers:

pip install starlite[testing]

To install the extras required for using the Brotli Compression Middleware:

pip install starlite[brotli]

To install the extras required for using the Session Middleware:

pip install starlite[cryptography]

And to install all of the above:

pip install starlite[full]

Minimal Example

Define your data model using pydantic or any library based on it (for example ormar, beanie, SQLModel):

my_app/models/user.py
from pydantic import BaseModel, UUID4


class User(BaseModel):
    first_name: str
    last_name: str
    id: UUID4

Alternatively, you can use a dataclass – either from dataclasses or from pydantic:

my_app/models/user.py
from uuid import UUID

# from pydantic.dataclasses import dataclass
from dataclasses import dataclass


@dataclass
class User:
    first_name: str
    last_name: str
    id: UUID

Define a Controller for your data model:

my_app/controllers/user.py
from typing import List, NoReturn

from pydantic import UUID4
from starlite import Controller, Partial, get, post, put, patch, delete

from my_app.models import User


class UserController(Controller):
    path = "/users"

    @post()
    async def create_user(self, data: User) -> User:
        ...

    @get()
    async def list_users(self) -> List[User]:
        ...

    @patch(path="/{user_id:uuid}")
    async def partial_update_user(self, user_id: UUID4, data: Partial[User]) -> User:
        ...

    @put(path="/{user_id:uuid}")
    async def update_user(self, user_id: UUID4, data: User) -> User:
        ...

    @get(path="/{user_id:uuid}")
    async def get_user(self, user_id: UUID4) -> User:
        ...

    @delete(path="/{user_id:uuid}")
    async def delete_user(self, user_id: UUID4) -> NoReturn:
        ...

When instantiating your app, import your controller into your application's entry-point and pass it to Starlite:

my_app/main.py
from starlite import Starlite

from my_app.controllers.user import UserController

app = Starlite(route_handlers=[UserController])

To run your application, use an ASGI server such as uvicorn:

uvicorn my_app.main:app --reload

About the Starlite Project

This project builds on top the Starlette ASGI toolkit and pydantic modelling to create a higher-order opinionated framework. The idea to use these two libraries as a basis is of course not new - it was first done in FastAPI, which in this regard (and some others) was a source of inspiration for this framework. Nonetheless, Starlite is not FastAPI - it has a different design, different project goals and a completely different codebase.

  1. The goal of this project is to become a community-driven project. That is, not to have a single "owner" but rather a core team of maintainers that leads the project, as well as community contributors.
  2. Starlite draws inspiration from NestJS - a contemporary TypeScript framework - which places opinions and patterns at its core. As such, the design of the API breaks from the Starlette design and instead offers an opinionated alternative.
  3. Finally, Python OOP is extremely powerful and versatile. While still allowing for function-based endpoints, Starlite seeks to build on this by placing class-based controllers at its core.

Example Applications

  • starlite-pg-redis-docker: In addition to Starlite, this demonstrates a pattern of application modularity, SQLAlchemy 2.0 ORM, Redis cache connectivity, and more. Like all Starlite projects, this application is open to contributions, big and small.
  • starlite-hello-world: A bare-minimum application setup. Great for testing and POC work.