***

title: foreach
slug: /reference/typescript/agents/data-map/foreach
description: Process an array from the webhook response.
max-toc-depth: 3
---------------------

For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

[ref-datamap]: /docs/server-sdks/reference/typescript/agents/data-map

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

## **Parameters**

<ParamField path="config" type="{ input_key: string; output_key: string; append: string; max?: number }" required={true} toc={true}>
  Configuration object with the following keys:
</ParamField>

<Indent>
  <ParamField path="config.input_key" type="string" required={true} toc={true}>
    Key in the API response containing the array.
  </ParamField>

  <ParamField path="config.output_key" type="string" required={true} toc={true}>
    Variable name for the built string (reference as `${output_key}` in output).
  </ParamField>

  <ParamField path="config.max" type="number" toc={true}>
    Maximum number of items to process.
  </ParamField>

  <ParamField path="config.append" type="string" required={true} toc={true}>
    Template string to append for each item. Use `${this.field}` to reference
    fields on the current array element.
  </ParamField>
</Indent>

## **Returns**

[`DataMap`][ref-datamap] -- Self for method chaining. Throws an `Error` if no webhook has been
added yet.

## **Example**

```typescript {5}
import { DataMap } from '@signalwire/sdk';

const dm = new DataMap('list_items');
dm.webhook('GET', 'https://api.example.com/items');
dm.foreach({
  input_key: 'response.items',
  output_key: 'item_list',
  append: '${this.name}: ${this.price}',
  max: 5,
});
```