serve_static_files

View as MarkdownOpen in Claude

Serve static files (HTML, CSS, JS, images) from a local directory. Agent routes take priority over static files, so requests matching a registered agent route are handled by the agent first. Unmatched paths fall through to static file serving.

This method is useful for serving a web frontend alongside your agents — for example, an admin dashboard or a configuration UI.

Unlike mounting StaticFiles directly on the FastAPI app, this method integrates correctly with agent route priority. Static files are served via the catch-all handler that runs on startup.

Parameters

directory
strRequired

Path to the directory containing static files. Must exist and be a valid directory. Raises ValueError if the path does not exist or is not a directory.

route
strDefaults to /

URL path prefix for static files. Use "/" to serve from the root. Directory requests automatically resolve to index.html if it exists.

Returns

None

Example

1from signalwire import AgentServer
2from signalwire import AgentBase
3
4support = AgentBase(name="support", route="/support")
5support.set_prompt_text("You are a helpful assistant.")
6
7server = AgentServer(port=3000)
8server.register(support)
9
10server.serve_static_files("./web")
11server.run()

With this configuration:

Request PathServed By
/supportSupportAgent
/support/swaigSupportAgent SWAIG endpoint
/index.html./web/index.html
/./web/index.html
/css/style.css./web/css/style.css

Path traversal attempts (e.g., /../etc/passwd) are blocked. The resolved file path must remain within the configured static directory.