Web Decorators
Targon provides a set of decorators for exposing functions as HTTP endpoints or full web applications. These decorators add the necessary metadata for the Targon runtime to route traffic, enforce authentication, and display documentation.
@targon.fastapi_endpoint()
Publish a single FastAPI-style handler as an HTTP endpoint. The decorated function can declare parameters (with type hints) that are automatically parsed from the incoming request.
@targon.fastapi_endpoint(
method="GET",
label="Greeting Endpoint",
docs=True,
requires_auth=False,
)
Parameters
| Name | Type | Description |
|---|---|---|
method | str | HTTP method to expose. Must be a non-empty string. Defaults to "GET". |
label | str | None | Optional label shown in the dashboard and generated docs. |
docs | bool | Whether to generate interactive documentation (/docs). |
requires_auth | bool | Require callers to include a valid Targon API key. |
Example
# targon-sdk/examples/gettin_started/web_endpoint_simple.py
@app.function()
@targon.fastapi_endpoint(method="GET", docs=True)
def greet(name: str = "World"):
return {"greeting": f"Hello, {name}!"}
Enable docs=True to automatically expose a Swagger UI at /docs for rapid testing.
@targon.asgi_app()
Attach a full ASGI application (for example FastAPI or Starlette) to a Targon function. Use this when you need multiple routes, middleware, or advanced request handling.
@targon.asgi_app(label="api", requires_auth=False)
Parameters
| Name | Type | Description |
|---|---|---|
label | str | None | Optional label used in dashboards and logs. |
requires_auth | bool | Enforce API key authentication for all routes. |
Example
# targon-sdk/examples/web/web_endpoint_asgi.py
@app.function()
@targon.asgi_app(label="api")
def hello():
from fastapi import FastAPI
api = FastAPI(title="Targon Demo API")
@api.get("/")
def root():
return {"message": "Welcome to Targon ASGI Demo API"}
return api
ASGI applications must be synchronous factory functions that return the ASGI callable. To use async factories, wrap them with asyncio.run() before returning.
@targon.wsgi_app()
Expose a WSGI-compatible application (Flask, Django, etc.). This decorator mirrors @targon.asgi_app() but targets WSGI servers.
@targon.wsgi_app(label="legacy-ui", requires_auth=True)
Parameters
| Name | Type | Description |
|---|---|---|
label | str | None | Optional label used in dashboards and logs. |
requires_auth | bool | Enforce API key authentication for all routes. |
Usage Notes
- The decorated function must return a WSGI application (callable with
environandstart_responsearguments). - Ensure your app is thread-safe to support concurrent requests.
@targon.web_server()
Run an arbitrary web server process inside your function. This is ideal for wrapping existing services that listen on a port (for example, Uvicorn, Gradio, or custom binaries).
@targon.web_server(
port=8080,
startup_timeout=300,
label="inference-server",
requires_auth=False,
)
Parameters
| Name | Type | Description |
|---|---|---|
port | int | Required port that the process listens on (1–65535). |
startup_timeout | int | Seconds to wait for the server to start before failing. Default 300. |
label | str | None | Optional label used in dashboards and logs. |
requires_auth | bool | Require API key authentication for all requests. |
Example
# targon-sdk/examples/llm/vllm_example.py
@app.function(resource="h200-small", max_replicas=1)
@targon.web_server(port=8080)
def serve():
import subprocess
cmd = [
"vllm",
"serve",
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
"--host",
"0.0.0.0",
"--port",
"8080",
]
subprocess.Popen(" ".join(cmd), shell=True)
startup_timeout should be increased for heavyweight models or frameworks that take longer to boot. If the process does not bind to the port before the timeout, the deployment will fail health checks.
Choosing the Right Decorator
- Use
@targon.fastapi_endpoint()for simple request/response handlers with optional auto-generated docs. - Use
@targon.asgi_app()when you need full ASGI frameworks, middleware, or multiple routes. - Use
@targon.wsgi_app()to bring existing WSGI applications to Targon. - Use
@targon.web_server()when you need to manage the lifecycle of a custom web server process.
For end-to-end walkthroughs, see the Web Endpoints guide.