> For the complete documentation index, see [llms.txt](https://docs.mediarithmics.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mediarithmics.io/setup-and-monitor/extend-the-platform/build-a-custom-feed-plugin/batching-for-feeds.md).

# Batching for feeds

Aggregate unit segment updates into batches when your partner needs batched input.

## Your goal

Deliver batched data to a partner that requires it, even though the platform sends your plugin **unit updates** - one UserPoint at a time.

## Expected outcome

A plugin whose `/user_segment_update` route formats one record, and whose `/batch_update` route receives those records aggregated into batches by the platform.

{% hint style="info" %}
Batching is currently only available for plugins of type `AUDIENCE_SEGMENT_EXTERNAL_FEED`.
{% endhint %}

## Why it exists

The platform provides unit updates to an external feed plugin, one UserPoint at a time, through the `/user_segment_update` route. Many partner APIs expect a payload containing many users. Rather than buffering in your own plugin - which would have to survive restarts and scaling across [workers](/setup-and-monitor/extend-the-platform/plugin-architecture.md#workers) - you let the platform do the aggregation.

```mermaid
flowchart LR
    P1["/user_segment_update<br/>one UserPoint"]
    P2["/user_segment_update<br/>one UserPoint"]
    P3["/user_segment_update<br/>one UserPoint"]
    AGG["Platform aggregates<br/>into batches"]
    B["/batch_update<br/>a batch of lines"]
    PART["Partner"]
    P1 --> AGG
    P2 --> AGG
    P3 --> AGG
    AGG --> B --> PART
```

**The output of a `/user_segment_update` call becomes an element of the `/batch_update` input.**

## Create a batching configuration

{% tabs %}
{% tab title="In the interface" %}

1. Go to **Computing Console > Plugins**.
2. Find your plugin, select the version, and open the **Batching configuration** tab.
3. Create the batching configuration.
   {% endtab %}

{% tab title="By API" %}

```
POST https://api.mediarithmics.com/v1/plugins/:plugin_id/versions/:version_id/batching_configurations
```

| Field                  | Type    | Description                                                    |
| ---------------------- | ------- | -------------------------------------------------------------- |
| `plugin_version_id`    | String  | The plugin version to batch. **Required.**                     |
| `enable_batching`      | Boolean | Enables or disables batching. **Required.**                    |
| `batch_flush_max_size` | Integer | The maximum size of a batch, in number of lines. **Required.** |
| {% endtab %}           |         |                                                                |
| {% endtabs %}          |         |                                                                |

## Split the plugin in two

{% stepper %}
{% step %}

### The data formatting and gathering phase

Your `/user_segment_update` implementation. It formats **one line** - the record for a single UserPoint - and returns it. It does not call the partner.
{% endstep %}

{% step %}

### The batch delivery phase

Your `/batch_update` implementation. It receives a batch of the lines returned above, and calls the partner once for the batch.
{% endstep %}
{% endstepper %}

## Implementation

{% hint style="info" %}
You need version **0.14 or later** of the Node.js Plugin SDK.
{% endhint %}

Rewrite your plugin to extend `BatchedAudienceFeedConnectorBasePlugin<T>`, where `T` is the type of one batched line. Doing so:

* forces your plugin to implement the `/batch_update` route,
* forces the `/segment_update` response type to be `BatchedUserSegmentUpdatePluginResponse<T>`.

## Grouping records

The response carries a **`grouping_key`** field, for when your data has to be regrouped by a specific criterion.

For example, a partner may require **only one identifier type per payload**. In that case, use the identifier type as the grouping key.

{% hint style="warning" %}
The platform does **not interpret** `grouping_key` - it uses **exact matching**. `EMAIL` and `email` are two different groups.
{% endhint %}

The same concept appears on the file side, where `${GROUPING_KEY}` can be used in a file name macro. See [File destination](/setup-and-monitor/collect-and-manage-your-data/export-your-data/file-destination.md#the-grouping-key).

## Verify

* `/user_segment_update` returns a single formatted line, and makes no partner call.
* `/batch_update` receives batches no larger than `batch_flush_max_size`.
* Records with different grouping keys never arrive in the same batch.
* The partner accepts the batched payload.

## Related pages

<table data-view="cards" data-search="false"><thead><tr><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Build a custom feed plugin</strong></td><td>Creating and deploying the feed.</td><td><a href="/setup-and-monitor/extend-the-platform/build-a-custom-feed-plugin.md">Build a custom feed plugin</a></td></tr><tr><td><strong>Plugin architecture</strong></td><td>Workers, instances, and throttlings.</td><td><a href="/setup-and-monitor/extend-the-platform/plugin-architecture.md">Plugin architecture</a></td></tr><tr><td><strong>File destination</strong></td><td>The grouping key in file delivery.</td><td><a href="/setup-and-monitor/collect-and-manage-your-data/export-your-data/file-destination.md">File destination</a></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mediarithmics.io/setup-and-monitor/extend-the-platform/build-a-custom-feed-plugin/batching-for-feeds.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
