Swml Service

View as Markdown

SWMLService API

API reference for SWMLService, the base class for creating and serving SWML documents.

Class Definition

1from signalwire_agents.core.swml_service import SWMLService
2
3class SWMLService:
4 """Base class for creating and serving SWML documents."""

Constructor

1SWMLService(
2 name: str, # Service name (required)
3 route: str = "/", # HTTP route path
4 host: str = "0.0.0.0", # Host to bind
5 port: int = 3000, # Port to bind
6 basic_auth: Optional[Tuple[str, str]] = None, # (username, password)
7 schema_path: Optional[str] = None, # SWML schema path
8 config_file: Optional[str] = None # Config file path
9)

Core Responsibilities

SWML Generation:

  • Create and validate SWML documents
  • Add verbs to document sections
  • Render complete SWML JSON output

Web Server:

  • Serve SWML documents via FastAPI
  • Handle SWAIG webhook callbacks
  • Manage authentication

Schema Validation:

  • Load and validate SWML schema
  • Auto-generate verb methods from schema
  • Validate document structure

Document Methods

reset_document

1def reset_document(self) -> None

Reset the SWML document to a clean state.

add_verb

1def add_verb(
2 self,
3 verb_name: str, # Verb name (e.g., "ai", "play")
4 params: Dict[str, Any] # Verb parameters
5) -> 'SWMLService'

Add a verb to the current document section.

get_document

1def get_document(self) -> Dict[str, Any]

Get the current SWML document as a dictionary.

render

1def render(self) -> str

Render the SWML document as a JSON string.

Auto-Generated Verb Methods

SWMLService automatically generates methods for all SWML verbs defined in the schema:

1## These methods are auto-generated from schema
2service.ai(...) # AI verb
3service.play(...) # Play audio
4service.record(...) # Record audio
5service.connect(...) # Connect call
6service.transfer(...) # Transfer call
7service.hangup(...) # End call
8service.sleep(...) # Pause execution
9## ... many more

Server Methods

run

1def run(
2 self,
3 host: str = None, # Override host
4 port: int = None # Override port
5) -> None

Start the development server.

get_app

1def get_app(self) -> FastAPI

Get the FastAPI application instance.

Authentication Methods

get_basic_auth_credentials

1def get_basic_auth_credentials(self) -> Tuple[str, str]

Get the current basic auth credentials.

URL Building Methods

_build_full_url

1def _build_full_url(
2 self,
3 endpoint: str = "", # Endpoint path
4 include_auth: bool = False # Include credentials
5) -> str

Build a full URL for an endpoint.

_build_webhook_url

1def _build_webhook_url(
2 self,
3 endpoint: str, # Endpoint path
4 query_params: Dict[str, str] = None # Query parameters
5) -> str

Build a webhook URL with authentication.

Routing Methods

register_routing_callback

1def register_routing_callback(
2 self,
3 callback: Callable, # Routing callback
4 path: str = "/" # Path to register
5) -> None

Register a routing callback for dynamic request handling.

Security Configuration

AttributeTypeDescription
ssl_enabledboolWhether SSL is enabled
domainstrDomain for SSL certificates
ssl_cert_pathstrPath to SSL certificate
ssl_key_pathstrPath to SSL private key
securitySecurityConfigUnified security configuration

Schema Utils

The schema_utils attribute provides access to SWML schema validation:

1## Access schema utilities
2service.schema_utils.validate(document)
3service.schema_utils.get_all_verb_names()
4service.schema_utils.get_verb_schema("ai")

Verb Registry

The verb_registry manages SWML verb handlers:

1## Access verb registry
2service.verb_registry.register_handler("custom_verb", handler)
3service.verb_registry.get_handler("ai")

Instance Attributes

AttributeTypeDescription
namestrService name
routestrHTTP route path
hoststrBind host
portintBind port
schema_utilsSchemaUtilsSchema validation utilities
verb_registryVerbRegistryVerb handler registry
logLoggerStructured logger

Usage Example

1from signalwire_agents.core.swml_service import SWMLService
2
3
4## Create a basic SWML service
5service = SWMLService(
6 name="my-service",
7 route="/swml",
8 port=8080
9)
10
11## Add verbs to build a document
12service.reset_document()
13service.play(url="https://example.com/welcome.mp3")
14service.ai(
15 prompt={"text": "You are a helpful assistant"},
16 SWAIG={"functions": []}
17)
18
19## Get the rendered SWML
20swml_json = service.render()
21print(swml_json)

Relationship to AgentBase

AgentBase extends SWMLService with higher-level abstractions:

SWMLService provides:

  • SWML document generation
  • Schema validation
  • Basic web server
  • Authentication

AgentBase adds:

  • Prompt management (POM)
  • Tool/function definitions
  • Skills system
  • AI configuration
  • Serverless support
  • State management