Litestar

The official blog of the Litestar (fka Starlite) API framework

Follow publication

Litestar x Scalar: Seamless API Live Sync

Jacob Coffee
Litestar
Published in
5 min readJan 9, 2025

Developers have to deal with with switching between tools and refreshing browser pages to test API changes. Scalar’s live sync simplifies this by automatically updating API routes in real-time, saving time and screen space.

Imagine this: You are working on a local version of your application and you have your two monitors (let’s be real–4 monitors… I’m looking at you r/battlestations!) or maybe you are minimalist (read: madman) like myself and live off the laptop screen. You need to bounce around your copious, or lack thereof, amounts of screen real estate–from IDE to browser–to test or preview your changes in the rendered API docs.

Herein lies the question: What if you didn’t have to?

If you are using a lesser OpenAPI documentation renderer such as Swagger, ReDocly, Stoplight, RapiDoc, etc. you would probably to visit your browser and refresh the page. With Scalar and their newly released API client you can just point it at your local web server and be off the to the races.

To summarize, you (optionally) create a new workspace with the API client, import a collection from your OpenAPI document such as http://127.0.0.0.1:8000/schema/openapi.json, ensure that Watch Mode is enabled, and each time you add a route in your Litestar app all you will need to do is change windows to the Scalar API client and watch it ✨ automagically ✨ reload–ready for you to test!

Below I will take you through a quick run-through on how to get going.

Live API Sync Walkthrough

Prerequisites

Creating a new workspace

Once inside the Scalar API client app, click the current workspace¹ and then + Create Workspace².

Creating a new workspace in the Scalar API client
Creating a new workspace in the Scalar API client

Give it a cute name, and smash the Create Workspace button.

Naming our Scalar API client workspace
Naming and creating your Scalar API client workspace

Importing an OpenAPI Collection

Within your new workspace, you will want to click Import Collection¹, give your local development server address (Note: Ensure it points to your OpenAPI document!)², enable watch mode³, and click Import from URL

Importing a collection in the Litestar x Scalar workspace in the Scalar API client
Importing your OpenAPI collection from local development URL

Expanding our Litestar app

Now the magic can happen! As it stands from our template app from the prerequisites section, we have no routes.

Scalar API client in the Litestar workspace with no routes
Scalar API client with no routes

We need to change that. Let’s add a basic route to return the age old classic, Hello, world!

# /// script
# requires-python = "==3.12"
# dependencies = [
# "litestar[standard]",
# ]
# ///
from litestar import get
from litestar.app import Litestar
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import ScalarRenderPlugin
from litestar.openapi.spec import Server

@get("/")
async def index() -> str:
return "Hello, world!"

app = Litestar(
route_handlers=[index],
openapi_config=OpenAPIConfig(
title="Litestar API",
version="1.0.0",
render_plugins=[
ScalarRenderPlugin(),
],
path="/api",
servers=[Server(url="http://127.0.0.1:8000/", description="Litestar x Scalar")],
)
)

if __name__ == "__main__":
from litestar.__main__ import run_cli

run_cli()

The new parts being our index route, the corresponding get decorator import, and the addition of a new route handler into the Litestar app constructor

...
from litestar import get
...

@get("/")
async def index() -> str:
return "Hello, world!
...

app = Litestar(
route_handlers=[index, health],
...
)

...

…and if we swap over over to Scalar API client-land we can see the watcher has politely updated our available routes, enabling us to test with ease.

Seeing the Scalar API client automatically reload in watch mode to show new Litestar routes. Wild appearance from Marc @ Scalar.
Scalar API client automatically reloads in watch mode to show new Litestar routes. Bonus cameo from Marc, Co-founder @ Scalar

We can do it one more time, for good measure. Let’s add a health check route that is 100% factual and accurate. Here is our final app.py

# /// script
# requires-python = "==3.12"
# dependencies = [
# "litestar[standard]",
# ]
# ///
from litestar import get
from litestar.app import Litestar
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import ScalarRenderPlugin
from litestar.openapi.spec import Server

@get("/")
async def index() -> str:
return "Hello, world!"

@get("/health")
async def health() -> dict:
return {"status": "ok"}

app = Litestar(
route_handlers=[index, health],
openapi_config=OpenAPIConfig(
title="Litestar API",
version="1.0.0",
render_plugins=[
ScalarRenderPlugin(),
],
path="/api",
servers=[Server(url="http://127.0.0.1:8000/", description="Litestar x Scalar")],
)
)

if __name__ == "__main__":
from litestar.__main__ import run_cli

run_cli()

The new parts being our health function and adding that route to our Litestar app constructors route_handlers :

...
@get("/health")
async def health() -> dict:
return {"status": "ok"}

app = Litestar(
route_handlers=[index, health],
...
)
...

…and just like that when we swap back to our Scalar API client we have a new route ready for us to test!

A new route, Health, is shown when swapping back to the Scalar API client thanks to watch mode / live API sync
A new route, Health, is shown when swapping back to the Scalar API client thanks to watch mode / live API sync

Conclusion

These may be small time-saving changes, but given the amount of time we spend CRUDing around our applications API schema it adds up and is a fantastic QOL feature.

Scalar is available for use thanks to a large amount of integrations out of the box (including Litestar, Django, FastAPI, and even more non-Python integrations).

Check out the Scalar.com post about API live sync using FastAPI

If you’d like to learn more about Litestar or just enjoy hanging out with a diverse community of web app and API developers, check out the documentation, code repository, and join us in Discord!

Tips / Tricks

Enabling/Disabling Watch mode after the fact:

Enable or disable Watch Mode with ease at any point

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet