SWAIG Functions

SWAIG

The SignalWire AI Gateway
View as MarkdownOpen in Claude

SWAIG is a JSON protocol over HTTP. It is how an AI Agent calls code you host: SignalWire POSTs a JSON object to your endpoint, and your endpoint returns a JSON object. This is the same idea as tool calling with any LLM API — see the tool calling guide for the architecture and the complete field-by-field protocol.

Each request body contains the function name (for example "search_movie"), the arguments the agent extracted in argument.parsed, the argument schema you declared in argument_desc, and the call’s session, caller, and project identifiers. Each reply contains response, the text added to the agent’s context, and optionally action, SWML-compatible objects that change what the call does next.

Remote functions

1

Accept a POST request

When a SWAIG function is invoked by the AI Agent, your server receives a JSON payload containing:

  • The function name to be executed.
  • The structured arguments in argument.parsed.
  • Contextual metadata (caller ID, project ID, session ID, etc.).

From this request, extract the function name and argument.parsed.

The following sample SWML creates the SWAIG function search_movie:

1{
2 "description": "Search for movies by title",
3 "function": "search_movie",
4 "parameters": {
5 "properties": {
6 "include_adult": {
7 "description": "Whether to include adult content",
8 "type": "boolean"
9 },
10 "language": {
11 "description": "Language of the results",
12 "type": "string"
13 },
14 "page": {
15 "description": "Page number for pagination",
16 "type": "integer"
17 },
18 "primary_release_year": {
19 "description": "Filter results by primary release year",
20 "type": "integer"
21 },
22 "query": {
23 "description": "The movie title to search for",
24 "type": "string"
25 },
26 "region": {
27 "description": "Specify a region to prioritize search results",
28 "type": "string"
29 },
30 "year": {
31 "description": "Filter results by release year",
32 "type": "integer"
33 }
34 },
35 "required": [],
36 "type": "object"
37 },
38 "web_hook_url": "https://username:password@moviebot.example.com/swaig"
39 }
Example request sent to server

When your SWAIG function executes, SignalWire sends a request like the following to your server.

1{
2 "function": "search_movie",
3 "argument": {
4 "parsed": [
5 {
6 "query": "Pretty Woman"
7 }
8 ],
9 "raw": "{\"query\":\"Pretty Woman\"}"
10 },
11 "argument_desc": {
12 "properties": {
13 "query": { "type": "string", "description": "The movie title to search for" },
14 "year": { "type": "integer" }
15 },
16 "type": "object"
17 },
18 "ai_session_id": "c960da54-3f09-4de6-8c84-49c1fcca704c",
19 "caller_id_num": "+19184249378",
20 "project_id": "625ceaeb-b27c-46b9-9b69-9d62286588ec"
21}
2

Execute business logic

On your server, perform the actions needed to generate the desired response using the extracted function name and arguments.

In this case, our application retrieves information about a selected movie from an external API.

3

Return a response message

The response can directly shape the AI Agent’s next moves using natural language and SWML instructions.

In reply, your server should return a JSON object with the following:

  • response (string): A message in Markdown format used by the LLM in its reply.
  • action (array): Optional list of SWML-compatible objects that can execute commands, play media, set metadata, or return inline SWML.

For example:

Response to SWAIG request

Note that this response includes both response and action sections. This means that our server has both updated the LLM’s context with the requested information from an external API, and handed off new call flow instructions in the form of valid SWML.

1{
2 "response": "**Pretty Woman** is a 1990 romantic comedy starring *Julia Roberts* as Vivian Ward, a spirited Hollywood escort, and *Richard Gere* as Edward Lewis, a wealthy businessman. Directed by Garry Marshall, the film tells the story of their unexpected romance that begins as a business deal and blossoms into a modern fairytale. \nSet against the glitz of Los Angeles, the movie features iconic moments—like the Rodeo Drive shopping spree and a memorable opera night. It explores themes of class, love, transformation, and empowerment. Julia Roberts' performance won her a Golden Globe and an Oscar nomination. It's now considered one of the most iconic romantic comedies ever made.",
3 "action": [
4 {
5 "set_meta_data": {
6 "title": "Pretty Woman",
7 "release_year": 1990,
8 "genre": ["Romance", "Comedy"],
9 "lead_actors": ["Julia Roberts", "Richard Gere"]
10 }
11 },
12 {
13 "SWML": {
14 "version": "1.0.0",
15 "sections": {
16 "main": [
17 {
18 "play": {
19 "url": "https://cdn.signalwire.com/swml/pretty-woman-theme.mp3"
20 }
21 }
22 ]
23 }
24 }
25 }
26 ]
27}