KeyValueStoreClient
Hierarchy
- ResourceClient
- KeyValueStoreClient
Index
Properties
inheritedapifyClient
inheritedbaseUrl
inheritedhttpClient
optionalinheritedid
optionalinheritedparams
inheritedpublicBaseUrl
Added in 2.17.0
inheritedresourcePath
optionalinheritedsafeId
inheritedurl
Methods
createKeysPublicUrl
Generates a public URL for accessing the list of keys in the key-value store.
If the client has permission to access the key-value store's URL signing key, the URL will include a cryptographic signature which allows access without authentication.
The signature grants access to the list of all keys in the store until it expires. The list options are added to the URL as plain query parameters that are not covered by the signature, so they only set the default shape of the response; anyone holding the URL can change or remove them.
Parameters
options: KeyValueClientCreateKeysUrlOptions = {}
URL generation options (extends all options from listKeys)
Returns Promise<string>
A public URL string for accessing the keys list
// Create a URL that expires in 1 hour and lists keys starting with `image-` by default.// The URL still grants access to the list of all keys until it expires.const url = await client.keyValueStore('my-store').createKeysPublicUrl({expiresInSecs: 3600,prefix: 'image-'});Added in 2.13.0
delete
Deletes the key-value store.
Parameters
options: TimeoutOptions = {}
Request options
Returns Promise<void>
deleteRecord
Deletes a record from the key-value store.
Parameters
key: string
The record key to delete
options: TimeoutOptions = {}
Request options
Returns Promise<void>
await client.keyValueStore('my-store').deleteRecord('temp-data');
get
Gets the key-value store object from the Apify API.
Parameters
options: TimeoutOptions = {}
Request options
Returns Promise<undefined | KeyValueStoreResource>
The KeyValueStore object, or
undefinedif it does not exist
getRecord
By default, the value is parsed by the record's content type:
application/jsonis parsed into a value,text/*andapplication/*xmlare decoded to a string, and anything else is returned as a Uint8Array. A record whose body does not parse as its content type claims, such as a non-JSON string stored underapplication/json, makes the call throw an InvalidResponseBodyError, which carries the raw body inerror.response.You can use the
bufferoption to get the value as a Uint8Array, which skips the parsing. You can also use thestreamoption to get the value unread, as a web ReadableStream.When the record does not exist, the function resolves to
undefined. It does NOT resolve to aKeyValueStoreRecordwith anundefinedvalue.Parameters
key: string
The record key
Returns Promise<undefined | KeyValueStoreRecord<JsonValue>>
getRecordPublicUrl
Generates a public URL for accessing a specific record in the key-value store.
If the client has permission to access the key-value store's URL signing key, the URL will include a cryptographic signature for authenticated access without requiring an API token.
Parameters
key: string
The record key
options: TimeoutOptions = {}
Request options
Returns Promise<string>
A public URL string for accessing the record
const url = await client.keyValueStore('my-store').getRecordPublicUrl('OUTPUT');console.log(`Public URL: ${url}`);// You can now share this URL or use it in a browserAdded in 2.14.0
listKeys
Lists all keys in the key-value store.
Returns a paginated list of all record keys in the store. Use pagination parameters to retrieve large lists efficiently.
Parameters
options: KeyValueClientListKeysOptions = {}
Listing options
Returns Promise<ListOfKeys> & AsyncIterable<KeyValueStoreKey, any, any>
Object containing
itemsarray of key metadata, pagination info (count,limit,isTruncated,nextExclusiveStartKey)// List all keysconst { items, isTruncated } = await client.keyValueStore('my-store').listKeys();items.forEach(item => console.log(`${item.key}: ${item.size} bytes`));// List keys with prefixconst { items } = await client.keyValueStore('my-store').listKeys({ prefix: 'user-' });// Paginate through all keyslet exclusiveStartKey;do {const result = await client.keyValueStore('my-store').listKeys({limit: 100,exclusiveStartKey});// Process result.items...exclusiveStartKey = result.nextExclusiveStartKey;} while (result.isTruncated);
recordExists
Tests whether a record with the given key exists in the key-value store without retrieving its value.
This is more efficient than getRecord when you only need to check for existence.
Parameters
key: string
The record key to check
options: TimeoutOptions = {}
Request options
Returns Promise<boolean>
trueif the record exists,falseif it does notconst exists = await client.keyValueStore('my-store').recordExists('OUTPUT');if (exists) {console.log('OUTPUT record exists');}Added in 2.9.0
setRecord
Stores a record in the key-value store.
The record value can be any JSON-serializable object, a string, or a Buffer/Stream. The content type is automatically determined based on the value type, but can be overridden using the
contentTypeproperty.A string is sent as it is, even under
application/json, so a string stored under a JSON content type has to be valid JSON already, or getRecord fails to parse it back.Note about streams: If the value is a stream, either a Node.js
Readableor a webReadableStreamsuch as thevaluethat getRecord returns with thestreamoption, the upload cannot be retried on failure or follow redirects. For reliable uploads of a file, pass aBlobfromfs.openAsBlob()instead: it is read again for every attempt and never held in memory whole. A browser sends a webReadableStreamonly over HTTP/2 or later, and only if it supports streaming request bodies at all: where it doesn't, the call throws aTypeError.Parameters
record: KeyValueStoreRecordInput
The record to store
options: KeyValueStoreRecordOptions = {}
Storage options
Returns Promise<void>
// Store JSON objectawait client.keyValueStore('my-store').setRecord({key: 'OUTPUT',value: { crawledUrls: 100, items: [...] }});// Store textawait client.keyValueStore('my-store').setRecord({key: 'README',value: 'This is my readme text',contentType: 'text/plain'});// Store binary dataconst imageBuffer = await fetchImageBuffer();await client.keyValueStore('my-store').setRecord({key: 'screenshot.png',value: imageBuffer,contentType: 'image/png'});// Store a gzipped file without compressing it againconst gzippedReport = await readFile('report.json.gz');await client.keyValueStore('my-store').setRecord({key: 'report.json',value: gzippedReport,contentType: 'application/json',contentEncoding: 'gzip'});
update
Updates the key-value store with specified fields.
Parameters
newFields: KeyValueClientUpdateOptions
Fields to update in the key-value store
options: TimeoutOptions = {}
Request options
Returns Promise<KeyValueStoreResource>
The updated KeyValueStore object
Client for managing a specific key-value store.
Key-value stores are used to store arbitrary data records or files. Each record is identified by a unique key and can contain any type of data. This client provides methods to get, set, and delete records, list keys, and manage the store.