# prepareAddPieces

> **prepareAddPieces**(`client`, `options`): [`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<[`AddPiecesCommit`](/reference/filoz/synapse-core/sp/type-aliases/addpiecescommit/)\>

Defined in: [packages/synapse-core/src/sp/commit.ts:192](https://github.com/FilOzone/synapse-sdk/blob/5989c4b03ed2fe4cf8b1ef29efe1d4ff1ad4c7bc/packages/synapse-core/src/sp/commit.ts#L192)

Sign an add pieces commit and return it as JSON-safe data, without sending it.

Checks the pieces fit in one message first, so a commit that can't be sent fails before it is saved. Save the
commit, then send it with [submitCommit](/reference/filoz/synapse-core/sp/functions/submitcommit/). Resubmitting the same commit can never add the pieces twice:
FWSS rejects a reused nonce.

## Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `client` | `Client`\<`Transport`, `Chain`, `Account`\> | The client that signs the commit. |
| `options` | [`OptionsType`](/reference/filoz/synapse-core/sp/namespaces/prepareaddpieces/type-aliases/optionstype/) | [prepareAddPieces.OptionsType](/reference/filoz/synapse-core/sp/namespaces/prepareaddpieces/type-aliases/optionstype/) |

## Returns

[`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<[`AddPiecesCommit`](/reference/filoz/synapse-core/sp/type-aliases/addpiecescommit/)\>

The signed commit [prepareAddPieces.OutputType](/reference/filoz/synapse-core/sp/namespaces/prepareaddpieces/type-aliases/outputtype/)

## Example

```ts
import * as SP from '@filoz/synapse-core/sp'
import * as Piece from '@filoz/synapse-core/piece'
import { calibration } from '@filoz/synapse-core/chains'
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

const client = createWalletClient({
  account: privateKeyToAccount('0x...'),
  chain: calibration,
  transport: http(),
})
const serviceURL = 'https://pdp.example.com'
const pieceCid = Piece.from('bafkzcibcaabffs4jcd4iheeo5wisbmurjb7l4xgpmzgyzrenebvjjhsbwgx4smy')

const commit = await SP.prepareAddPieces(client, {
  dataSetId: 1n,
  clientDataSetId: 1n,
  pieces: [{ pieceCid, metadata: { name: 'photo.jpg' } }],
})

// Save the commit before sending it, so it can be resubmitted after a crash.
const saved = JSON.stringify(commit)

const result = await SP.submitCommit(client, { serviceURL, commit: JSON.parse(saved) })
const { pieceIds } = await SP.waitForAddPieces({ client, payer: commit.payer, ...result })
console.log(pieceIds)
```

## Throws

Errors [prepareAddPieces.ErrorType](/reference/filoz/synapse-core/sp/namespaces/prepareaddpieces/type-aliases/errortype/)