# createPieceBatcher

> **createPieceBatcher**(`client`, `options`): [`PieceBatcher`](/reference/filoz/synapse-core/sp/type-aliases/piecebatcher/)

Defined in: [packages/synapse-core/src/sp/create-piece-batcher.ts:232](https://github.com/FilOzone/synapse-sdk/blob/840d7b2c6263846ac51b7558000e62dd68d33a44/packages/synapse-core/src/sp/create-piece-batcher.ts#L232)

Create a stateful piece batcher that parks/pulls immediately and coalesces addPieces.

`upload` runs per-piece I/O right away (streaming CommP-last protocol for
bytes and streams). `pull` calls are coalesced into pull requests of up to
`SIZE_CONSTANTS.PULL_BATCH_SIZE` pieces, each with one signature and
one polling loop; each `pull` promise still settles with its own piece's
status, and a full SP pull queue (429) is waited out. The tumbling window
batches the on-chain addPieces (or createDataSetAndAddPieces) call.
Pull authorization matches the operation for the pieces in that pull
request; flush signs new extraData for the whole window.

Invalid metadata or an out-of-bounds PieceCID size rejects only that piece
when it is passed to `upload`/`pull`/`enqueue`, before any network work; it
never joins a pull request or a window. A failed flush (signing, RPC, SP or
chain errors) rejects every piece in the window with [AddPiecesFlushError](/reference/filoz/synapse-core/errors/classes/addpiecesflusherror/).

For crash recovery, persist each window from `onSubmitted` and pass it to
`resume()` after a restart instead of adding the same pieces again.

## Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `client` | `Client`\<`Transport`, `Chain`, `Account`\> | Wallet client used to sign and submit. |
| `options` | [`OptionsType`](/reference/filoz/synapse-core/sp/namespaces/createpiecebatcher/type-aliases/optionstype/) | [createPieceBatcher.OptionsType](/reference/filoz/synapse-core/sp/namespaces/createpiecebatcher/type-aliases/optionstype/) |

## Returns

[`PieceBatcher`](/reference/filoz/synapse-core/sp/type-aliases/piecebatcher/)

Batcher [createPieceBatcher.ReturnType](/reference/filoz/synapse-core/sp/namespaces/createpiecebatcher/type-aliases/returntype/)

## Example

```ts
import { createPieceBatcher } from '@filoz/synapse-core/sp'

const batcher = createPieceBatcher(client, { dataSet })
await Promise.all([
  batcher.upload({ data: fileA }),
  batcher.pull({ pieceCid, sourceUrl }),
])
await batcher.close()
```