# Collections Blocks

# Collections Blocks
[Section titled “Collections Blocks”](#collections-blocks)
Collections blocks (Collect and Explode) manage and manipulate groups of events in your flows. They are important for batch operations, data aggregation, and processing lists of items.

## Collect Block
[Section titled “Collect Block”](#collect-block)
Gathers multiple incoming events and groups them together, emitting a consolidated list as a single event. The Collect block is highly flexible, supporting count-based, time-based, and conditional collection strategies.

### Configuration Fields
[Section titled “Configuration Fields”](#configuration-fields)
#### Group (optional)
[Section titled “Group (optional)”](#group-optional)
Evaluates to group key (string) for organizing events into separate collection buckets:


**

```
outputs.userSignup.department // Groups events by user department
```

**When to use:** When you need to maintain separate collections for different categories or entities (e.g., per-user queues, per-tenant batches).

#### Count (optional)
[Section titled “Count (optional)”](#count-optional)
Evaluates to number of events to collect before emitting:


**

```
5 // Collect exactly 5 events before emittingoutputs.webhook.body?.batchSize || 5  // Dynamic: use incoming batchSize, defaulting to 5
```

Evaluated for each received event when added to its group.

#### Delay (optional)
[Section titled “Delay (optional)”](#delay-optional)
Evaluates to seconds to wait before emitting collected events:


**

```
30 // Wait 30 seconds before emitting whatever has been collected
```

#### Item (optional)
[Section titled “Item (optional)”](#item-optional)
Transform each input event before storing in the collection:


**

```
// Extract and transform only needed fields({  userId: outputs.itemTransformInput.body?.userId,  action: outputs.itemTransformInput.body?.action,  timestamp: Date.now()})
```

Default: Whole event is stored if not defined. **When to use:**

 - Reduce memory usage by storing only necessary fields
 - Add computed fields (timestamps, IDs) to each item
 - Normalize data format across different event sources

#### Emit (optional)
[Section titled “Emit (optional)”](#emit-optional)
Evaluates to boolean determining if collected group should be emitted immediately.

Available variables:

 - `outputs` - Current incoming event
 - `items` - All events currently in collection group
 - `group` - Group key for current collection


**

```
// Emit immediately for urgent events or when reaching 10 itemsoutputs.priority === 'urgent' || items.length >= 10
// Emit when specific condition in accumulated dataitems.some(item => item.type === 'error')
// Complex conditional logicitems.length >= 5 && outputs.webhook.body?.force !== true
```

When this returns true, collection is emitted regardless of count or delay settings. **When to use:**

Emit immediately for high-priority events Trigger on dynamic conditions (thresholds, errors, specific values) Combine batch size with content-based logic

### Use Cases
[Section titled “Use Cases”](#use-cases)
**Batch Processing:** Collect multiple events before processing them together, reducing API calls or database operations. ![Batch Processing](/images/collect-batch-processing.png)

**Data Aggregation:** Gather events from different sources within a time window for consolidated reporting or analytics. ![Data Aggregation](/images/collect-data-aggregation.png)

**Time-Window Operations:** Process all events that occur within specific time periods (e.g., hourly summaries, daily digests). ![Time Window](/images/collect-time-window.png)

**Grouped Collections:** Maintain separate collections for different entities (per-user queues, per-tenant batches). ![Grouped Collection](/images/collect-grouped.png)

**Priority-Based Processing:** Use conditional logic to emit batches based on urgency or content. ![Priority-Based Processing](/images/collect-priority-based-processing.png)

## Explode Block
[Section titled “Explode Block”](#explode-block)
Takes a list from an input event and emits each item as a separate event.

### Configuration Fields
[Section titled “Configuration Fields”](#configuration-fields-1)
#### Expression
[Section titled “Expression”](#expression)
Evaluates to list of items to explode into separate events:


**

```
outputs.apiResponse.users // Explode a list of users from API response
```

Each item becomes the payload of a separate output event.

#### Limit
[Section titled “Limit”](#limit)
Maximum number of events to emit for a single input event:


**

```
100 // Limit to 100 output events per input event
```

Safety feature preventing accidental creation of too many events from large lists.

### Use Cases
[Section titled “Use Cases”](#use-cases-1)
 - **Fan-out Operations**: Send each list item to different downstream processes
 - **Individual Item Actions**: Perform actions on each item separately (e.g., send individual notifications)
 - **Data Distribution**: Split bulk data into individual processing streams

### Integration with Collect
[Section titled “Integration with Collect”](#integration-with-collect)
Collect and Explode are inverse operations:

 - Explode takes one event with a list and creates many events
 - Collect takes many events and creates one event with a list

![Explode and Collect](/images/explode-and-collect.png)