Skip to content

FastAPI API

Requires pip install ontosql[fastapi]. Beta-experimental tier — see SPECS.md. Not safe on public networks without authSECURITY.md.

OntoRouter

ontosql.fastapi.router.OntoRouter

Register CRUD routes for OntoModel entities with content negotiation.

Requires onto_async_session_lifespan on the host app (AsyncSessionDep). Not safe for public internet without dependencies authn/authz — see docs/guides/production-router.md.

register

register(entity_type: type[OntoModel]) -> None

Add GET/POST/PATCH/DELETE routes for one entity type.

include_in

include_in(app: Any, *, prefix: str | None = None) -> None

Mount routes on a FastAPI app and install semantic OpenAPI enrichment.

ontosql.fastapi.router.DEFAULT_MAX_BODY_BYTES module-attribute

DEFAULT_MAX_BODY_BYTES = 64 * 1024

Lifespan and dependencies

ontosql.fastapi.deps.onto_async_session_lifespan

onto_async_session_lifespan(app: FastAPI, engine: AsyncEngine, maps: list[type[Any]]) -> None

Store async engine and maps on app.state for AsyncSessionDep.

ontosql.fastapi.deps.onto_session_lifespan

onto_session_lifespan(app: FastAPI, engine: Engine, maps: list[type[Any]]) -> None

Store engine and maps on app.state for sync session dependencies.

ontosql.fastapi.deps.AsyncSessionDep module-attribute

AsyncSessionDep = Annotated[AsyncOntoSession, Depends(get_async_onto_session)]

ontosql.fastapi.deps.SessionDep module-attribute

SessionDep = Annotated[OntoSession, Depends(get_onto_session)]

ontosql.fastapi.deps.get_async_onto_session async

get_async_onto_session(request: Request) -> AsyncIterator[AsyncOntoSession]

Yield an AsyncOntoSession bound to app.state async engine and maps.

ontosql.fastapi.deps.get_onto_session

get_onto_session(request: Request) -> Iterator[OntoSession]

Yield a synchronous OntoSession bound to app.state engine and maps.

Content negotiation

ontosql.fastapi.negotiate.negotiate_onto_response

negotiate_onto_response(request: Request, data: Any) -> Response

Return a FastAPI Response based on the request Accept header.

Falls back to JSON-LD if data supports to_jsonld(), otherwise JSON.

RDF responses

ontosql.fastapi.responses.JSONLDResponse

Bases: RDFResponse

JSON-LD response (application/ld+json).

ontosql.fastapi.responses.TurtleResponse

Bases: RDFResponse

ontosql.fastapi.responses.NTriplesResponse

Bases: RDFResponse

ontosql.fastapi.responses.RDFXMLResponse

Bases: RDFResponse

ontosql.fastapi.responses.RDFResponse

Bases: Response

RDF serialization response (turtle, n-triples, rdf+xml, json-ld).

OpenAPI helpers

ontosql.fastapi.openapi.enrich_openapi_schema

enrich_openapi_schema(app: FastAPI, entity_types: list[type[OntoModel]]) -> dict[str, Any]

Return OpenAPI schema with semantic hints for registered OntoModel types.

ontosql.fastapi.openapi.install_onto_openapi

install_onto_openapi(app: FastAPI, entity_types: list[type[OntoModel]]) -> None

Replace app.openapi with semantic-enriched schema generation.

Production patterns

List GET routes default to application/ld+json when no Accept header is sent (same as item GET). Pass Accept: application/json for a plain JSON array.

See production-router.md and FastAPI quick start.