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
Optional[Any]

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

context
Optional[Any]

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

host
Optional[str]

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

port
Optional[int]

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

1from signalwire import AgentServer
2from signalwire import AgentBase
3
4agent = AgentBase(name="my-agent", route="/agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7server = AgentServer(host="0.0.0.0", port=3000)
8server.register(agent)
9server.run()

AWS Lambda

1from signalwire import AgentServer
2from signalwire import AgentBase
3
4agent = AgentBase(name="my-agent", route="/agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7server = AgentServer()
8server.register(agent)
9
10def lambda_handler(event, context):
11 return server.run(event, context)

Gunicorn (external ASGI server)

1from signalwire import AgentServer
2from signalwire import AgentBase
3
4agent = AgentBase(name="my-agent", route="/agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7server = AgentServer()
8server.register(agent)
9
10# Expose the FastAPI app for gunicorn:
11# gunicorn app:app -k uvicorn.workers.UvicornWorker
12app = 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.