Serving apps
Flyte SDK lets you serve apps on your Union/Flyte instance, making them accessible via HTTP endpoints. Apps are long-running services that can be accessed by users or other services.
Make sure to run the local setup before going through this guide.
First install FastAPI in your virtual environment:
pip install fastapiHello world example
Create a file called hello_app.py with the following content:
# /// script
# requires-python = "==3.13"
# dependencies = [
# "flyte==2.0.0b31",
# "fastapi",
# "uvicorn",
# ]
# ///
"""A simple "Hello World" FastAPI app example for serving."""
from fastapi import FastAPI
import pathlib
import flyte
from flyte.app.extras import FastAPIAppEnvironment
# Define a simple FastAPI application
app = FastAPI(
title="Hello World API",
description="A simple FastAPI application",
version="1.0.0",
)
# Create an AppEnvironment for the FastAPI app
env = FastAPIAppEnvironment(
name="hello-app",
app=app,
image=flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages(
"fastapi",
"uvicorn",
),
resources=flyte.Resources(cpu=1, memory="512Mi"),
requires_auth=False,
)
# Define API endpoints
@app.get("/")
async def root():
return {"message": "Hello, World!"}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
# Serving this script will deploy and serve the app on your Union/Flyte instance.
if __name__ == "__main__":
# Initialize Flyte from a config file.
flyte.init_from_config(root_dir=pathlib.Path(__file__).parent)
# Serve the app remotely.
app_instance = flyte.serve(env)
# Print the app URL.
print(app_instance.url)
print("App 'hello-app' is now serving.")
Understanding the code
In the code above we do the following:
- Import the
flytepackage andFastAPIAppEnvironmentfromflyte.app.extras. - Define a FastAPI application using the
FastAPIclass. - Create an
AppEnvironmentusingFastAPIAppEnvironment:- Apps are long-running services, unlike tasks which run to completion.
- The
FastAPIAppEnvironmentautomatically configures the app to run with uvicorn. - We specify the container image with required dependencies (FastAPI and uvicorn).
- We set resource limits (CPU and memory).
- We disable authentication for this example (
requires_auth=False) so you can easily access the app with acurlcommand.
Serving the app
Make sure that your config.yaml file is in the same directory as your hello_app.py script.
Now, serve the app with:
flyte serve hello_app.py envYou can also serve it via python:
python hello_app.pyThis will use the code in the if __name__ == "__main__": block to serve the app
with the flyte.serve() function.
You can also serve the app using python hello_app.py, which
uses the main guard section in the script. It invokes flyte.init_from_config() to set up the connection with your Union/Flyte instance and flyte.serve() to deploy and serve your app on that instance.
The example scripts in this guide have a main guard that programmatically serves the apps defined in the same file.
All you have to do is execute the script itself.
You can also serve apps using the flyte serve CLI command. We will cover this in a later section.
Viewing the results
In your terminal, you should see output like this:
https://my-instance.flyte.com/v2/domain/development/project/flytesnacks/apps/hello-app
App 'hello-app' is now serving.Click the link to go to your Union instance and see the app in the UI, where you can find
the app URL, or visit /docs for the interactive Swagger UI API documentation.
Next steps
Now that you’ve served your first app, you can learn more about:
- Configuring apps: Learn how to configure app environments, including images, resources, ports, and more
- Building apps: Explore different types of apps you can build (FastAPI, Streamlit, vLLM, SGLang)
- Serving and deploying apps: Understand the difference between serving (development) and deploying (production) apps