# downloadAndValidate

> **downloadAndValidate**(`response`, `expectedPieceCid`): [`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`\>\>

Defined in: [packages/synapse-core/src/piece.ts:292](https://github.com/FilOzone/synapse-sdk/blob/2c515fb6328c4f7a68ed82653d96ab11299e177d/packages/synapse-core/src/piece.ts#L292)

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`](https://developer.mozilla.org/docs/Web/API/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...')
```