In addition to your frontend app, Reflex uses a FastAPI backend to serve your app. The API transformer feature allows you to transform or extend the ASGI app that serves your Reflex application.

The API transformer provides a way to:

  1. Integrate existing FastAPI or Starlette applications with your Reflex app
  2. Apply middleware or transformations to the ASGI app
  3. Extend your Reflex app with additional API endpoints

This is useful for creating a backend API that can be used for purposes beyond your Reflex app, or for integrating Reflex with existing backend services.

You can set the api_transformer parameter when initializing your Reflex app:

The api_transformer parameter can accept:

  1. A Starlette or FastAPI instance
  2. A callable that takes an ASGIApp and returns an ASGIApp
  3. A sequence of the above

When you provide a FastAPI or Starlette instance as the API transformer, Reflex will mount its internal API to your app, allowing you to define additional routes:

You can also provide a callable that transforms the ASGI app:

You can apply multiple transformers by providing a sequence:

Some routes on the backend are reserved for the runtime of Reflex, and should not be overridden unless you know what you are doing.

localhost:8000/ping/: You can use this route to check the health of the backend.

The expected return is "pong".

localhost:8000/_event: the frontend will use this route to notify the backend that an event occurred.

Overriding this route will break the event communication

localhost:8000/_upload: This route is used for the upload of file when using rx.upload().

Built with Reflex