Skip to main content

Installation | FastAPI

Create internal apps directly in your FastAPI codebase with the Compose SDK. This guide will get you started with two starter apps in ~3 minutes.

1. Install the SDK

Use your package manager to install the Compose SDK.

pip install compose-sdk
# or
poetry add compose-sdk
# or
uv add compose-sdk

2. Add starter apps

Create a new file in your project where you'll add your Compose apps, e.g. compose.py, and paste the following code:

import compose_sdk as c

# For demo purposes. In a real app, you'd use your actual database.
db_users = [
{ "name": "John Doe", "email": "john@doe.com" },
{ "name": "Jane Smith", "email": "jane@smith.com" },
]

def view_users_handler(page: c.Page, ui: c.UI):
page.add(lambda: ui.header("View Users", size="lg"))
users = [*db_users] # fake database call
page.add(lambda: ui.table("users-table", users))

def create_user_handler(page: c.Page, ui: c.UI):
def on_submit(form):
db_users.append({ "name": form["name"], "email": form["email"] })
page.toast("User created successfully", appearance="success")
page.link("view-users")

page.add(lambda: ui.header("Create User", size="lg"))
page.add(lambda: ui.form(
"create-user-form",
[
ui.text_input("name"),
ui.email_input("email")
],
on_submit=on_submit
))

nav = c.Navigation(["view-users", "create-user"])

compose_client = c.Client(
api_key="API_KEY_HERE",
apps=[
c.App(route="view-users", nav=nav, handler=view_users_handler),
c.App(route="create-user", nav=nav, handler=create_user_handler)
]
)

Replace API_KEY_HERE with your own Compose API key. Create a free account on Compose to get one.

3. Start the Compose Client

In the main entry point of your project, e.g. main.py, add the following:

from fastapi import FastAPI
from contextlib import asynccontextmanager

from .compose import compose_client

# Use the `lifespan` context manager to connect the Compose dashboard once
# when the app starts up, and disconnect when the app shuts down.
@asynccontextmanager
async def lifespan(app: FastAPI):
compose_client.connect_async()
yield
compose_client.shutdown()

app = FastAPI(lifespan=lifespan)
connect_async vs connect

The Python SDK provides two methods to connect to the Compose web dashboard: connect_async and connect.

  • connect_async starts the WebSocket connection in a separate background thread. Use this in environments like FastAPI where blocking the main thread would interfere with request handling.
  • connect runs the connection in the main thread. Use this when running a standalone Python process dedicated to Compose apps.

4. Run your app

Start your FastAPI server. Compose will automatically connect in the background.

fastapi dev main.py

You should see your apps come online in the Compose web dashboard.

Next steps

  1. Replace db_users with real database calls.
  2. Go through Compose's core concepts to quickly become productive with the SDK.
  3. Join the Discord community to ask questions and talk to the core team.