Setting the file. One moment.
Chapter 01 · Apify Actor Development
Subchapter 1.5
references/key-value-store-schema.mdMarkdown4 KBView on GitHub
The key-value store schema organizes keys into logical groups called collections for easier data management.
Consider an example Actor that calls Actor.setValue() to save records into the key-value store:
import { Actor } from 'apify';
// Initialize the JavaScript SDK
await Actor.init();
/**
* Actor code
*/
await Actor.setValue('document-1', 'my text data', { contentType: 'text/plain' });
await Actor.setValue(`image-${imageID}`, imageBuffer, { contentType: 'image/jpeg' });
// Exit successfully
await Actor.exit();Consider an example Actor that calls Actor.set_value() to save records into the key-value store:
# Key-Value Store set example (Python)
import asyncio
from apify import Actor
async def main():
await Actor.init()
# Actor code
await Actor.set_value('document-1', 'my text data', content_type='text/plain')
image_id = '123' # example placeholder
image_buffer = b'...' # bytes buffer with image data
await Actor.set_value(f'image-{image_id}', image_buffer, content_type='image/jpeg')
# Exit successfully
await Actor.exit()
if __name__ == '__main__':
asyncio.run(main())To configure the key-value store schema, reference a schema file in .actor/actor.json:
{
"actorSpecification": 1,
"name": "data-collector",
"title": "Data Collector",
"version": "1.0.0",
"storages": {
"keyValueStore": "./key_value_store_schema.json"
}
}Then create the key-value store schema in .actor/key_value_store_schema.json:
{
"actorKeyValueStoreSchemaVersion": 1,
"title": "Key-Value Store Schema",
"collections": {
"documents": {
"title": "Documents",
"description": "Text documents stored by the Actor",
"keyPrefix": "document-"
},
"images": {
"title": "Images",
"description": "Images stored by the Actor",
"keyPrefix": "image-",
"contentTypes": ["image/jpeg"]
}
}
}{
"actorKeyValueStoreSchemaVersion": 1,
"title": "string (required)",
"description": "string (optional)",
"collections": {
"<COLLECTION_NAME>": {
"title": "string (required)",
"description": "string (optional)",
"key": "string (conditional - use key OR keyPrefix)",
"keyPrefix": "string (conditional - use key OR keyPrefix)",
"contentTypes": ["string (optional)"],
"jsonSchema": "object (optional)"
}
}
}actorKeyValueStoreSchemaVersion (integer, required) - Version of key-value store schema structure document (currently only version 1)title (string, required) - Title of the schemadescription (string, optional) - Description of the schemacollections (Object, required) - Object where each key is a collection ID and value is a Collection objecttitle (string, required) - Collection title shown in UI tabsdescription (string, optional) - Description appearing in UI tooltipskey (string, conditional) - Single specific key for this collectionkeyPrefix (string, conditional) - Prefix for keys included in this collectioncontentTypes (string[], optional) - Allowed content types for validationjsonSchema (object, optional) - JSON Schema Draft 07 format for application/json content type validationEither key or keyPrefix must be specified for each collection, but not both.