run

View as MarkdownOpen in Claude

Universal entry point that detects the execution environment and starts the server accordingly. In standard server mode it launches uvicorn; in serverless environments (AWS Lambda, CGI) it processes the incoming request and returns a response.

This method blocks in server mode. For production deployments behind gunicorn or another ASGI server, use server.app directly instead of calling run().

Parameters

event
AnyDefaults to None

Serverless event object (AWS Lambda, Google Cloud Functions). Pass the Lambda handler’s event parameter here. Ignored in server mode.

context
AnyDefaults to None

Serverless context object (AWS Lambda, Google Cloud Functions). Pass the Lambda handler’s context parameter here. Ignored in server mode.

host
str | NoneDefaults to None

Override the host set in the constructor. Only applies in server mode.

port
int | NoneDefaults to None

Override the port set in the constructor. Only applies in server mode.

Returns

None in server mode (blocks until shutdown). In Lambda mode, returns a response dict with statusCode, headers, and body. In CGI mode, returns the formatted response string.

Examples

Server Mode

from signalwire import AgentServer
from signalwire import AgentBase
agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")
server = AgentServer(host="0.0.0.0", port=3000)
server.register(agent)
server.run()

AWS Lambda

from signalwire import AgentServer
from signalwire import AgentBase
agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")
server = AgentServer()
server.register(agent)
def lambda_handler(event, context):
return server.run(event, context)

Gunicorn (external ASGI server)

from signalwire import AgentServer
from signalwire import AgentBase
agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")
server = AgentServer()
server.register(agent)
# Expose the FastAPI app for gunicorn:
# gunicorn app:app -k uvicorn.workers.UvicornWorker
app = server.app

If SWML_SSL_ENABLED is set to true in the environment along with SWML_SSL_CERT_PATH and SWML_SSL_KEY_PATH, the server starts with HTTPS automatically.