Agents

SWMLService

View as MarkdownOpen in Claude

SWMLService is the foundation class for creating and serving SWML (SignalWire Markup Language) documents. It provides SWML document generation with schema validation, a built-in FastAPI web server for serving documents over HTTP, and basic authentication for securing endpoints. Most developers will use AgentBase (which extends SWMLService) rather than working with SWMLService directly. Use SWMLService when you need low-level control over SWML document construction without the AI agent abstractions.

AgentBase extends SWMLService — it inherits all document generation, serving, and authentication capabilities documented here.

SWMLService generates and serves SWML documents over HTTP. See the SWML reference for the full document specification.

Properties

name
str

Service name/identifier used in logging and server startup messages.

route
str

HTTP route path where this service is accessible. Trailing slashes are stripped automatically during initialization.

host
str

Host address the web server binds to.

port
int

Port number the web server binds to.

schema_utils
SchemaUtils

Schema validation utilities for SWML documents. Provides verb validation, verb name enumeration, and schema property lookups.

verb_registry
VerbHandlerRegistry

Registry of specialized verb handlers. Manages custom handlers for complex SWML verbs that require logic beyond generic schema validation (e.g., the ai verb).

log
Logger

Structured logger instance bound to this service name.

security
SecurityConfig

Unified security configuration loaded from environment variables and optional config file. Controls SSL, CORS, and host allowlist settings.

ssl_enabled
bool

Whether SSL/HTTPS is enabled. Mirrors security.ssl_enabled for backward compatibility.

ssl_cert_path
str

Path to the SSL certificate file. Mirrors security.ssl_cert_path for backward compatibility.

ssl_key_path
str

Path to the SSL private key file. Mirrors security.ssl_key_path for backward compatibility.

domain
str

Domain name for SSL certificates. Mirrors security.domain for backward compatibility.

full_validation_enabled
bool

Read-only property indicating whether full JSON Schema validation is active. Controlled by the schema_validation property or the SWML_SKIP_SCHEMA_VALIDATION environment variable.

The constructor also accepts basic_auth, schema_path, config_file, and schema_validation parameters. These are not exposed as public instance attributes after initialization. Use get_basic_auth_credentials() to retrieve auth credentials and full_validation_enabled to check validation status.

Methods

Example

1from signalwire import SWMLService
2
3# Basic service with defaults
4service = SWMLService(name="ivr")
5
6# Service with custom route and auth
7service = SWMLService(
8 name="ivr",
9 route="/swml",
10 port=8080,
11 basic_auth=("admin", "secret123")
12)
13
14# Build a SWML document
15service.add_verb("answer", {})
16service.add_verb("play", {"url": "https://example.com/welcome.mp3"})
17service.add_verb("hangup", {})
18
19# Serve it
20service.serve()