API Transformer

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.

Overview

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.

Using API Transformer

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

Types of API Transformers

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

Using a FastAPI or Starlette Instance

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:

Using a Callable Transformer

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

Using Multiple Transformers

You can apply multiple transformers by providing a sequence:

Reserved Routes

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

Ping

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

The expected return is "pong".

Event

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

Upload

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

Built with Reflex