Streaming resources
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()- Stream a log in real time. Resolves to a webReadableStream, orundefinedwhen the log doesn't exist. A log client chained off a run or build, such asclient.run('run-id').log(), throws aNotFoundErrorinstead, since the run itself may be what is missing.DatasetClient.downloadItems()with thestream: trueoption - Stream dataset items serialized to a format such as CSV or XLSX. Resolves to a webReadableStream.KeyValueStoreClient.getRecord()with thestream: trueoption - Stream a key-value store record as raw data. Resolves to a record whosevalueis a webReadableStream, orundefinedwhen the record doesn't exist.RunClient.getStreamedLog()- Redirect the log of a run to a logger while the run is in progress. For details, see 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.
Upload a stream
A request body can be a stream too: KeyValueStoreClient.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.
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().