DEV Community

Alex Spinov
Alex Spinov

Posted on

Litestar Has a Free API You're Not Using

Litestar (formerly Starlite) is a Python ASGI framework that's faster than FastAPI with better dependency injection and built-in features. Most Python developers haven't even heard of it.

What is Litestar?

Litestar is a high-performance, opinionated ASGI framework built on top of Starlette with a focus on developer ergonomics and correctness.

The Free APIs You're Missing

1. DTO Layer — Automatic Data Transfer Objects

from litestar import Litestar, get, post
from litestar.dto import DTOConfig
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO

class UserDTO(SQLAlchemyDTO[User]):
    config = DTOConfig(
        exclude={"password_hash", "created_at"},
        rename_fields={"email_address": "email"},
    )

@get("/users", return_dto=UserDTO)
async def list_users(db: AsyncSession) -> list[User]:
    result = await db.execute(select(User))
    return result.scalars().all()
Enter fullscreen mode Exit fullscreen mode

Automatic field exclusion, renaming, and serialization from your ORM models. No manual schemas.

2. Dependency Injection — Built-In DI Container

from litestar import Litestar, get
from litestar.di import Provide

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

async def get_user_service(db: AsyncSession) -> UserService:
    return UserService(db)

@get("/users/{user_id:int}")
async def get_user(user_service: UserService, user_id: int) -> User:
    return await user_service.get(user_id)

app = Litestar(
    route_handlers=[get_user],
    dependencies={"db": Provide(get_db_session), "user_service": Provide(get_user_service)},
)
Enter fullscreen mode Exit fullscreen mode

Proper dependency injection with lifecycle management. Not just function parameters.

3. Guards — Declarative Authorization

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

def admin_guard(connection: ASGIConnection, handler: BaseRouteHandler) -> None:
    if not connection.user.is_admin:
        raise PermissionDeniedException("Admin access required")

@get("/admin/dashboard", guards=[admin_guard])
async def admin_dashboard() -> dict:
    return {"stats": "admin only data"}
Enter fullscreen mode Exit fullscreen mode

4. Channels — Built-In WebSocket Pub/Sub

from litestar import Litestar, WebSocket
from litestar.channels import ChannelsPlugin
from litestar.channels.backends.memory import MemoryChannelsBackend

channels = ChannelsPlugin(backend=MemoryChannelsBackend(), channels=["notifications"])

@websocket("/ws")
async def ws_handler(socket: WebSocket, channels: ChannelsPlugin) -> None:
    await socket.accept()
    async with channels.subscribe("notifications") as subscriber:
        async for message in subscriber:
            await socket.send_text(message)
Enter fullscreen mode Exit fullscreen mode

Real-time WebSocket channels with zero external dependencies.

5. OpenAPI — Automatic Documentation with Customization

from litestar.openapi import OpenAPIConfig, OpenAPIController

app = Litestar(
    openapi_config=OpenAPIConfig(
        title="My API",
        version="1.0.0",
        root_schema_site="swagger",
    ),
)
Enter fullscreen mode Exit fullscreen mode

Benchmarks

Litestar consistently outperforms FastAPI in benchmarks:

  • Requests/sec: Litestar ~12,000 vs FastAPI ~8,500
  • Latency p99: Litestar 4.2ms vs FastAPI 6.8ms

Getting Started

pip install litestar[standard]
litestar init my-app
litestar run
Enter fullscreen mode Exit fullscreen mode

Need data from any website delivered as clean JSON? I build production web scrapers that handle anti-bot, proxies, and rate limits. 77 scrapers running in production. Email me: Spinov001@gmail.com

Check out my awesome-web-scraping list for the best scraping tools and resources.

Top comments (0)