DEV Community

Alex Spinov
Alex Spinov

Posted on

Litestar Has a Free API — The Python Web Framework Built for Performance

Litestar (formerly Starlite) is a high-performance Python ASGI framework with built-in OpenAPI docs, dependency injection, and ORM integration. It's faster than FastAPI and more feature-complete out of the box.

Why Litestar?

  • Faster than FastAPI in benchmarks
  • Built-in DI, caching, rate limiting, sessions
  • SQLAlchemy integration — first-class ORM support
  • DTO system — automatic serialization/filtering

Quick Start

pip install litestar[standard]
Enter fullscreen mode Exit fullscreen mode
from litestar import Litestar, get, post
from dataclasses import dataclass

@dataclass
class Message:
    text: str
    sender: str

@get('/hello/{name:str}')
async def hello(name: str) -> dict:
    return {'message': f'Hello, {name}!'}

@post('/messages')
async def create_message(data: Message) -> Message:
    return data

app = Litestar([hello, create_message])
Enter fullscreen mode Exit fullscreen mode
litestar run
# Docs at /schema/swagger
Enter fullscreen mode Exit fullscreen mode

Controllers

from litestar import Controller, get, post, put, delete

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

    @get()
    async def list_users(self) -> list[User]:
        return await User.select()

    @post()
    async def create_user(self, data: UserCreate) -> User:
        return await User.create(**data.__dict__)

    @get('/{user_id:int}')
    async def get_user(self, user_id: int) -> User:
        return await User.get(user_id)

    @delete('/{user_id:int}')
    async def delete_user(self, user_id: int) -> None:
        await User.delete(user_id)

app = Litestar([UserController])
Enter fullscreen mode Exit fullscreen mode

Dependency Injection

from litestar.di import Provide

async def get_db() -> AsyncSession:
    async with async_session() as session:
        yield session

@get('/users', dependencies={'db': Provide(get_db)})
async def get_users(db: AsyncSession) -> list[User]:
    result = await db.execute(select(User))
    return result.scalars().all()
Enter fullscreen mode Exit fullscreen mode

Guards (Authorization)

from litestar.connection import ASGIConnection
from litestar.handlers import BaseRouteHandler

async def admin_guard(connection: ASGIConnection, _: BaseRouteHandler) -> None:
    if not connection.user.is_admin:
        raise PermissionDeniedException()

@get('/admin', guards=[admin_guard])
async def admin_panel() -> dict:
    return {'admin': True}
Enter fullscreen mode Exit fullscreen mode

Rate Limiting

from litestar.middleware.rate_limit import RateLimitConfig

rate_limit = RateLimitConfig(rate_limit=('minute', 60))

app = Litestar(
    [hello],
    middleware=[rate_limit.middleware],
)
Enter fullscreen mode Exit fullscreen mode

Building high-performance APIs? Check out my Apify actors for production-ready web scrapers, or email spinov001@gmail.com for custom API development.

FastAPI or Litestar — which do you choose for new projects? Share below!

Top comments (0)