SwmlBuilder
SwmlBuilder provides a fluent interface for constructing SWML documents by chaining
verb method calls. It produces documents of the form
{ version: "1.0.0", sections: { main: [...verbs] } }. Use SwmlBuilder when you
want concise, readable document construction in a single expression.
In addition to the explicitly defined methods below, SwmlBuilder automatically generates
methods for every SWML verb in the bundled schema (e.g., connect(), record(),
denoise(), goto()). These dynamic methods accept an optional
config?: Record<string, unknown> parameter and return this for chaining.
SwmlBuilder constructs SWML documents programmatically. See the SWML reference for the full specification of all supported verbs.
Constructor
Initializes an empty SWML document and installs dynamic verb methods from the bundled schema. All constructor options are optional.
SwmlBuilderOptions
initialDocument
Seed the builder with an existing document instead of creating an empty one.
Enables document injection — mirrors the Python SDK’s SWMLService injection pattern.
enableValidation
Explicit override for verb schema validation. Defaults to true unless
SWML_SKIP_SCHEMA_VALIDATION=true is set in the environment.
schemaPath
Path to a custom SWML schema JSON file. When set, the builder uses a
per-instance SchemaUtils loaded from this path instead of the bundled schema.
Dynamic Verb Methods
SwmlBuilder auto-generates chaining methods for every verb defined in the SWML schema.
These dynamic methods accept an optional config object matching the verb’s SWML
parameters and return this for chaining.
Examples of dynamic verbs: connect(), record(), recordCall(), denoise(),
transfer(), goto(), cond(), execute(), tap(), and many more.
The sleep verb is a special case — it accepts either a number (duration) directly
or a config object:
Methods
Append a verb to the main section.
Append a verb to a named section.
Add an AI verb to start an AI-powered conversation.
Add an answer verb to the SWML document.
Return the raw SWML document object.
Get the shared SchemaUtils singleton (static).
Add a hangup verb to end the current call.
Add a play verb to play audio or text-to-speech.
Serialize the SWML document to a JSON string.
Reset the SWML document to an empty state.
Add a new named section to the document.
Build and return the document (Python-compat alias for getDocument).
Render the document as JSON (Python-compat alias for renderDocument).
Property-style read-only accessor for the underlying document.
Enable or disable verb schema validation.