SWAIG
SWAIG is the system that lets the AI call your functions during a conversation. You define functions, SignalWire calls them via webhooks, and your responses guide the AI.
What is SWAIG?
SWAIG (SignalWire AI Gateway) connects the AI conversation to your backend logic. When the AI decides it needs to perform an action (like looking up an order or checking a balance), it calls a SWAIG function that you’ve defined.

SWAIG in SWML
When your agent generates SWML, it includes SWAIG function definitions in the ai verb:
Defining SWAIG Functions
There are three ways to define SWAIG functions in your agent:
Method 1: define_tool()
The most explicit way to register a function:
Method 2: @AgentBase.tool Decorator
A cleaner approach using decorators:
Method 3: DataMap (Serverless)
For direct API integration without code:
Function Handler Signature
Every SWAIG function handler receives two arguments:
The raw_data Payload
The raw_data contains rich context about the call:
SwaigFunctionResult
Always return a SwaigFunctionResult from your handlers:
Common Actions
SWAIG Request Flow

SWAIG Request Format
SignalWire sends a POST request with this structure:
Important Note on Request Structure:
- Call information (caller/callee numbers, call_id, direction) is nested under the
callkey - Always use defensive access:
call_data = raw_data.get("call", {}) - Some fields may also appear at the top level for backwards compatibility
- Use the pattern shown in “Accessing Call Information” above for robust code
SWAIG Response Format
Your agent responds with:
Or for a transfer:
Function Parameters (JSON Schema)
SWAIG functions use JSON Schema for parameter definitions:
Webhook Security
SWAIG endpoints support multiple security layers:
- Basic Authentication: HTTP Basic Auth on all requests
- Function Tokens: Per-function security tokens
- HTTPS: TLS encryption in transit
Testing SWAIG Functions
Use swaig-test to test functions locally:
Best Practices
- Keep functions focused: One function, one purpose
- Write clear descriptions: Help the AI understand when to use each function
- Validate inputs: Check for required arguments
- Handle errors gracefully: Return helpful error messages
- Use global_data: Share state between function calls
- Log for debugging: Track function calls and responses
Next Steps
Now that you understand how SWAIG connects AI to your code, let’s trace the complete lifecycle of a request through the system.