Skip to main content

FastAPI Plugin for FastStream

A plugin that allows you to use Depends and FastAPI other objects in the FastStream

Features

Use FastAPI Dependency Injection

In FastStream handlers, it will be possible to use the familiar DI from FastAPI

from fastapi import Path, Body, Header, Depends

class BodyModel(BaseModel):
    field: int

@broker.subscriber("subject.{num}")
async def subscriber_handler(
    num: Annotated[int, Path()],
    body: Annotated[BodyModel, Body()],
    x_user_id: Annotated[int, Header()],
    my_dep: Annotated[int, Depends(int)],
) -> None:
    ...

Сan use FastAPI dependency overrides

fastapi = FastAPI()
fastapi.dependency_overrides[Dep] = lambda: "Dep"

@broker.subscriber("subject")
async def subscriber(dep: Annotated[str, De[]]) -> None:
    assert dep == "Dep"

Using the FastStream Context

from faststream import Context

@broker.subscriber("subject")
async def subscriber_handler(context_data: Annotated[int, Context("data")]) -> Response: ...

Use FastAPI Request and Response

from fastapi import Request, Response
from fastapi.responses import JSONResponse

@broker.subscriber("subject")
async def subscriber_handler(request: Request) -> Response:
    return JSONResponse({"data": 1})

Minimalistic plugin connection to your application

All you need to do is wrap the FastAPI with the FastStreamAPI object from the plugin

application = FastStreamAPI(
    NatsBroker(),
    application=FastAPI(),
)
uvicorn.run(application)

Managing a lifespan state

Now the lifespan state is also available in FastStream handlers

@asynccontextmanager
async def lifespan(app: FastAPI):
    yield {"lifespan_data": "LIFESPAN DATA"}

@broker.subscriber("subject")
async def subscriber(request: Request) -> None:
    assert request.state.lifespan_data == "LIFESPAN DATA"

The ability to configure AsyncAPI

The ability to configure AsyncAPI using SpecificationFactory from FastStream and AsyncAPIConfig from the plugin

FastStreamAPI(
    ...,
    specification=AsyncAPI(
        title="My app",
        version="1.0.0",
        description="...",
        ...,
    ),
    asyncapi_path="/fs_docs",
    # or
    asyncapi_path=AsyncAPIRouter(
        "/fs_docs",
        description="...",
        ...
    ),
)

Backgrounds tasks

You can use BackgroundTasks from FastAPI in FastStream handlers

@broker.subscriber("subject")
async def handler1(
    tasks: BackgroundTasks,
) -> None:
    tasks.add_task(...)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faststream_fastapi-1.2.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

faststream_fastapi-1.2.1-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file faststream_fastapi-1.2.1.tar.gz.

File metadata

  • Download URL: faststream_fastapi-1.2.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for faststream_fastapi-1.2.1.tar.gz
Algorithm Hash digest
SHA256 f34e39439bb008dd8df3a616d41ca2dda2cd76726585476f204526e1f76bda1e
MD5 a2deea707b50478ed201b426dd2bcc39
BLAKE2b-256 135976b140ed8d6f66e9e8b8e1f5f341ee8b24376356ba712cab904be9670319

See more details on using hashes here.

Provenance

The following attestation bundles were made for faststream_fastapi-1.2.1.tar.gz:

Publisher: publish.yml on faststream-community/faststream_fastapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file faststream_fastapi-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: faststream_fastapi-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for faststream_fastapi-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1fec6f8de29f72289065308d4f3954b60d4e36c5f04d3b09f62152a461e9af8
MD5 30c80f9121a12cac2f7f451afb3df20c
BLAKE2b-256 114c2e80037d4fc563148dd06ac01e72a09a6b8a90e9a7687db7a060f57a24b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for faststream_fastapi-1.2.1-py3-none-any.whl:

Publisher: publish.yml on faststream-community/faststream_fastapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.3.1

2 files

1.3.0

2 files

This release

1.2.1 This release

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page