# downloadAndValidate

```ts
function downloadAndValidate(response, expectedPieceCid): Promise<Uint8Array<ArrayBufferLike>>;
```

Defined in: [packages/synapse-core/src/piece.ts:408](https://github.com/FilOzone/synapse-sdk/blob/1d6c4b9fe34534bf1087dfe41491b72be0b46858/packages/synapse-core/src/piece.ts#L408)

Download data from a Response object, validate its PieceCID, and return as Uint8Array

This function:
1. Streams data from the Response body
2. Calculates PieceCID during streaming
3. Collects all chunks into a Uint8Array
4. Validates the calculated PieceCID matches the expected value

## Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `response` | `Response` | The Response object from a fetch() call |
| `expectedPieceCid` | `string` \| `PieceLink` | The expected PieceCID to validate against |

## Returns

[`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<[`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)\<`ArrayBufferLike`\>\>

The downloaded data as a Uint8Array

## Throws

Error if PieceCID validation fails or download errors occur

## Example

```typescript
const response = await fetch(url)
const data = await downloadAndValidate(response, 'bafkzcib...')
```