awesome-python/website/data/category_intros/web-apis.md
Vinta Chen d450705926
docs: add web APIs category intro
The Web APIs category page had no intro, leaving readers without a pick among Django REST framework, Django Ninja, FastAPI, and the GraphQL and RPC libraries.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-09-27 03:27:23 +08:00

5.1 KiB

For a Django project, pick Django REST framework or Django Ninja. Outside Django, build on FastAPI, a Python API framework based on type hints.

How to choose:

  • Django, with CRUD endpoints over your models: Django REST framework
  • Django, with endpoints declared by type hints: Django Ninja
  • A new service outside Django: FastAPI
  • GraphQL over Django models: Strawberry GraphQL Django
  • Django, with msgspec, attrs, or dataclass schemas: django-modern-rest
  • A Flask app: APIFlask
  • An OpenAPI spec written before the code: Connexion
  • GraphQL on FastAPI, Flask, or another framework: Strawberry
  • RPC between services in any language: gRPC

Django REST framework adds serializers and a browsable API to Django. For CRUD over your models, group the views in a ModelViewSet and register it with a router. For an OpenAPI schema, its docs recommend drf-spectacular. By default, the API allows unrestricted access, so set a policy in DEFAULT_PERMISSION_CLASSES.

Django Ninja is heavily inspired by FastAPI and works with Django's ORM, URLs, views, and auth. You write each endpoint as a function with type hints, and it generates the OpenAPI docs from them. Run async views on an ASGI server. If you use Django's cookie-based auth, keep Django's CSRF protection on.

FastAPI builds request validation and OpenAPI docs from standard type hints, on top of Pydantic and Starlette. Install fastapi[standard], which brings Uvicorn and the fastapi command: run fastapi dev while you work and fastapi run in production.

Strawberry GraphQL Django builds types from your Django models, while Strawberry alone only provides a GraphQL view for Django. Add its query optimizer, which calls select_related() and prefetch_related() for you to avoid N+1 queries.

django-modern-rest drops into an existing Django app and validates both requests and responses against your schemas: msgspec, Pydantic, attrs, dataclasses, and more. Its docs recommend always installing msgspec to parse JSON, even when your schemas are Pydantic models.

APIFlask is a thin wrapper on Flask: swap Flask for APIFlask and Blueprint for APIBlueprint, and your Flask extensions keep working. Declare each endpoint's input and output with @app.input() and @app.output(), as marshmallow schemas or Pydantic models, and APIFlask generates the OpenAPI docs from them.

Connexion is spec-first: you write the OpenAPI spec, and Connexion routes and validates requests against it, so server and client can be built in parallel. FastAPI goes the other way, and its maintainer says it's not meant for writing the schema first. Start a new project on AsyncApp, or on FlaskApp to keep the Flask ecosystem.

Strawberry builds a GraphQL schema from dataclasses and type hints, and FastAPI's docs recommend it for GraphQL. Before you deploy, turn off GraphiQL and introspection, and add the security extensions that limit query depth, aliases, and tokens.

gRPC has you define a service once in a .proto file and generate its clients and servers in any language gRPC supports. Install grpcio and grpcio-tools, and compile the .proto file into Python code with grpc_tools.protoc. Use TLS to authenticate the server and encrypt the traffic.

Keep separate models for what an endpoint takes in and what it sends back. FastAPI filters the response through the output model, so fields the output model leaves out, like a password, never reach the client. APIFlask recommends separate input and output schemas too.