Skip to main content

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

NameTypeDescription
methodstrHTTP method to expose. Must be a non-empty string. Defaults to "GET".
labelstr | NoneOptional label shown in the dashboard and generated docs.
docsboolWhether to generate interactive documentation (/docs).
requires_authboolRequire 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}!"}
tip

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

NameTypeDescription
labelstr | NoneOptional label used in dashboards and logs.
requires_authboolEnforce 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
note

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

NameTypeDescription
labelstr | NoneOptional label used in dashboards and logs.
requires_authboolEnforce API key authentication for all routes.

Usage Notes

  • The decorated function must return a WSGI application (callable with environ and start_response arguments).
  • 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

NameTypeDescription
portintRequired port that the process listens on (1–65535).
startup_timeoutintSeconds to wait for the server to start before failing. Default 300.
labelstr | NoneOptional label used in dashboards and logs.
requires_authboolRequire 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)
caution

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.