AgentsDataMap

foreach

View as MarkdownOpen in Claude

Process an array from the webhook response, building a formatted string from each element.

Parameters

foreach_config
dict[str, Any]Required

Configuration dictionary with the following keys:

foreach_config.input_key
strRequired

Key in the API response containing the array.

foreach_config.output_key
strRequired

Variable name for the built string (reference as ${output_key} in output).

foreach_config.max
int

Maximum number of items to process.

foreach_config.append
strRequired

Template string to append for each item. Use ${this.field} to reference fields on the current array element.

Returns

DataMap — Self for method chaining. Raises ValueError if no webhook has been added yet.

Example

from signalwire import AgentBase, DataMap
from signalwire import FunctionResult
search_docs = (
DataMap("search_docs")
.purpose("Search documentation")
.parameter("query", "string", "Search query", required=True)
.webhook(
"POST",
"https://api.docs.example.com/search",
headers={"Authorization": "Bearer TOKEN"}
)
.body({"query": "${args.query}", "limit": 3})
.foreach({
"input_key": "results",
"output_key": "formatted_results",
"max": 3,
"append": "- ${this.title}: ${this.summary}\n"
})
.output(FunctionResult("Found:\n${formatted_results}"))
.fallback_output(FunctionResult("Search is currently unavailable."))
)
agent = AgentBase(name="docs-agent")
agent.set_prompt_text("You are a helpful assistant.")
agent.register_swaig_function(search_docs.to_swaig_function())
if __name__ == "__main__":
agent.run()