Skip to navigation

Helper Functions

View as MarkdownOpen in Claude
import { createSimpleApiTool } from '@signalwire/sdk';
const weatherTool = createSimpleApiTool({
name: 'get_weather',
url: 'https://api.weather.com/v1/current?q=${args.location}',
responseTemplate: 'Weather: ${current.condition.text}, ${current.temp_f}F',
parameters: {
location: { type: 'string', description: 'City name', required: true },
},
});

Create a DataMap for a straightforward single-endpoint API call with minimal configuration.

Parameters

name
stringRequired

Function name.

url
stringRequired

API endpoint URL. Reads the arguments as ${args.name}.

responseTemplate
stringRequired

Template string for formatting the response; it becomes the webhook’s output. Reads the response’s fields from the root, such as ${current.temp_f}, and the arguments as ${input.args.name}.

parameters
Record<string, { type?: string; description?: string; required?: boolean }>

Parameter definitions. Keys are parameter names, values are objects with "type", "description", and optional "required" keys.

method
stringDefaults to GET

HTTP method.

headers
Record<string, string>

HTTP headers, sent as written.

body
Record<string, unknown>

JSON request body, set as the webhook’s params, so a tool with a body is sent as a POST. An empty object is left out.

errorKeys
string[]

Keys indicating an error response.

Returns

DataMap — A fully configured DataMap ready for further chaining or conversion.


import { createExpressionTool, FunctionResult } from '@signalwire/sdk';
const controlTool = createExpressionTool({
name: 'playback_control',
patterns: {
'${args.command}': ['play.*', new FunctionResult('Playing.')],
},
parameters: {
command: { type: 'string', description: 'Playback command', required: true },
},
});

Create a DataMap for pattern-matching responses without API calls.

Parameters

name
stringRequired

Function name.

patterns
Record<string, [string, FunctionResult]>Required

Object mapping test values to [pattern, FunctionResult] tuples.

parameters
Record<string, { type?: string; description?: string; required?: boolean }>

Parameter definitions (same format as createSimpleApiTool).

Returns

DataMap — A fully configured DataMap.

Example

import {
AgentBase,
FunctionResult,
createSimpleApiTool,
createExpressionTool,
} from '@signalwire/sdk';
// Simple API tool -- one line instead of a full DataMap chain
const weather = createSimpleApiTool({
name: 'get_weather',
url: 'https://api.weatherapi.com/v1/current.json?key=KEY&q=${enc:args.city}',
responseTemplate: 'Weather in ${input.args.city}: ${current.condition.text}',
parameters: {
city: { type: 'string', description: 'City name', required: true },
},
errorKeys: ['error'],
});
// Expression tool -- pattern matching without API calls
const greeting = createExpressionTool({
name: 'greet',
patterns: {
'${args.language}': ['spanish|espanol', new FunctionResult('Hola!')],
},
parameters: {
language: { type: 'string', description: 'Language to greet in' },
},
});
const agent = new AgentBase({ name: 'helper-demo' });
agent.setPromptText('You are a helpful assistant.');
agent.registerSwaigFunction(weather.toSwaigFunction());
agent.registerSwaigFunction(greeting.toSwaigFunction());
agent.run();