# Streaming resources

Copy for LLM

Certain resources, such as dataset items, key-value store records and logs, support streaming directly from the Apify API. You can then process large resources incrementally without downloading them entirely into memory, which suits large or continuously updated data.

Supported streaming methods:

* [`LogClient.stream()`](https://docs.apify.com/api/client/js/reference/class/LogClient.md#stream) - Stream a log in real time. Resolves to a web `ReadableStream`, or `undefined` when the log doesn't exist. A log client chained off a run or build, such as `client.run('run-id').log()`, throws a [`NotFoundError`](https://docs.apify.com/api/client/js/reference/class/NotFoundError.md) instead, since the run itself may be what is missing.
* [`DatasetClient.downloadItems()`](https://docs.apify.com/api/client/js/reference/class/DatasetClient.md#downloadItems) with the `stream: true` option - Stream dataset items serialized to a format such as CSV or XLSX. Resolves to a web `ReadableStream`.
* [`KeyValueStoreClient.getRecord()`](https://docs.apify.com/api/client/js/reference/class/KeyValueStoreClient.md#getRecord) with the `stream: true` option - Stream a key-value store record as raw data. Resolves to a record whose `value` is a web `ReadableStream`, or `undefined` when the record doesn't exist.
* [`RunClient.getStreamedLog()`](https://docs.apify.com/api/client/js/reference/class/RunClient.md#getStreamedLog) - Redirect the log of a run to a logger while the run is in progress. For details, see [Redirecting Actor run logs](https://docs.apify.com/api/client/js/docs/concepts/logging.md#redirecting-actor-run-logs).

A stream holds its connection open until you consume it to the end or cancel it. Iterating it with `for await` or passing it to `pipeline()` from `node:stream/promises` does either one for you, including when the processing throws.

The following example shows how to stream the log of an Actor run incrementally:

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



// Resolves to a web `ReadableStream` that follows the log while the run is in progress.

const logStream = await client.run('MY-RUN-ID').log().stream();



if (logStream) {

    for await (const chunk of logStream) {

        process.stdout.write(chunk);

    }

}
```

The next example streams a key-value store record straight to a file:

```
import { createWriteStream } from 'node:fs';

import { pipeline } from 'node:stream/promises';



import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



// With `stream: true`, the record value is a web `ReadableStream`.

const record = await client.keyValueStore('MY-KVS-ID').getRecord('video.mp4', { stream: true });



if (record) {

    await pipeline(record.value, createWriteStream('video.mp4'));

}
```

To export a large dataset without holding it in memory, stream its items to a file:

```
import { createWriteStream } from 'node:fs';

import { pipeline } from 'node:stream/promises';



import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



// Resolves to a web `ReadableStream` with the items serialized to CSV.

const itemsStream = await client.dataset('MY-DATASET-ID').downloadItems('csv', { stream: true });



await pipeline(itemsStream, createWriteStream('items.csv'));
```

The streams are web streams, so these methods work the same way in every runtime, except for `getStreamedLog()`, which needs Node.js. For details, see [Bundled environments](https://docs.apify.com/api/client/js/docs/concepts/bundled-environments.md).

## Upload a stream[](#upload-a-stream)

A request body can be a stream too: [`KeyValueStoreClient.setRecord()`](https://docs.apify.com/api/client/js/reference/class/KeyValueStoreClient.md#setRecord) takes a web `ReadableStream` or a Node.js `Readable` as the record `value`, and sends it without holding it in memory. That includes the stream another call returned, so a record can be copied without being read first:

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



const record = await client.keyValueStore('source-store').getRecord('video.mp4', { stream: true });



if (record) {

    // The stream goes straight from one store to the other.

    await client.keyValueStore('target-store').setRecord({

        key: record.key,

        value: record.value,

        contentType: record.contentType,

    });

}
```

A stream can be sent only once, so the client neither retries such a request nor follows a redirect with it. To upload a file with retries, pass a `Blob` instead. For details, see [Retry a request body read from a file](https://docs.apify.com/api/client/js/docs/concepts/retries.md#retry-a-request-body-read-from-a-file).

Node.js, Bun and Deno send a stream of either type, and Cloudflare Workers send a web `ReadableStream`. A browser sends a web `ReadableStream` only if it supports streaming request bodies, and only over HTTP/2 or later. In a browser without that support, such as Firefox and Safari, the call throws a `TypeError`, so read the stream into a `Blob` first, for example with `await new Response(stream).blob()`.
