SWAIG (SignalWire AI Gateway)
SWAIG (SignalWire AI Gateway)
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:
Python
TypeScript
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 parsed arguments and the full request payload:
The raw_data Payload
The raw_data contains rich context about the call:
FunctionResult
Always return a FunctionResult from your handlers. The class name varies by language:
Python
TypeScript
Common Actions
SWAIG Request Flow

SWAIG Request Format
SignalWire sends a POST request with this structure:
Call information (caller/callee numbers, call_id, direction) is nested under the call key. Always use defensive access: call_data = raw_data.get("call", {}). Some fields may also appear at the top level for backwards compatibility.
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.