Input & output
Read input, including secrets, and write results to default storage.
An Actor reads one input object and writes results to its default dataset and default key-value store. This page covers how Actor.get_input() resolves input, how secret input fields arrive, and the shortcuts for writing output: push_data(), get_value() and set_value().
Input
Actor.get_input()
await Actor.get_input() returns the input as a dict, or None when the run has no input. It checks these sources in order:
- If
ACTOR_METAMORPH_COUNTis greater than 0, theINPUT-METAMORPH-<count>record in the default key-value store. - The
ACTOR_INPUTenvironment variable, decoded as base64 JSON and, failing that, as plain JSON. - The record named by
ACTOR_INPUT_KEY, normallyINPUT, in the default key-value store.
The SDK caches the result, so later calls return the same object.
actor_input = await Actor.get_input() or {}
url = actor_input.get("url", "https://example.com")Actor.get_input_or_throw()
actor_input = await Actor.get_input_or_throw()Raises ValueError("Input does not exist") when get_input() returns None. It doesn't validate the input's shape. Use an input schema, or check required fields yourself.
Actor.input
Actor.input is a synchronous property. It returns the cached input, or it parses ACTOR_INPUT. It never reads the key-value store, so use get_input() in normal code.
Secret inputs
Fields marked isSecret in the input schema are stored encrypted. When a run starts, the platform decrypts them on the host and passes the plaintext input to the container in ACTOR_INPUT. The INPUT record keeps them encrypted, but reads with the run's own token return them decrypted. The private key never enters the container. So get_input() returns secret fields as plain values, and your code doesn't need to do anything extra. See Secret input fields.
The SDK still contains a fallback decryptor for compatibility. It runs only when APIFY_INPUT_SECRETS_PRIVATE_KEY_FILE and APIFY_INPUT_SECRETS_PRIVATE_KEY_PASSPHRASE are set, which Scrape.ly doesn't do.
Output
Actor.push_data()
await Actor.push_data(data, charged_event_name=None, *, charges=None, charge_per="row", dataset=None)Writes one JSON-serializable object, or a list of them, to the default dataset. It returns a ChargeResult.
await Actor.push_data({"url": "https://example.com", "title": "Example"})
await Actor.push_data([
{"page": 1, "value": "a"},
{"page": 2, "value": "b"},
])For pay-per-event Actors, pass charged_event_name to charge one event per item:
result = await Actor.push_data(items, charged_event_name="result-item")
if result.event_charge_limit_reached:
Actor.log.info("Budget reached, stopping")When the run's maximum charge only covers part of the batch, the SDK pushes and charges only the items that fit. It drops the rest without writing them. For Actors that aren't pay-per-event, push_data() writes everything and returns an empty result.
For several events per item, a whole batch, or a named dataset, use the keyword arguments:
await Actor.push_data(items, charges={"result": 1, "page": 3})
await Actor.push_data(page_rows, charges={"document": 1}, charge_per="batch")
await Actor.push_data(comments, charged_event_name="comment", dataset="comments")chargescharges several events per item together: an item is written only when all of its events were charged. It can't be combined withcharged_event_name.charge_per="batch"chargeschargesonce for the whole push. All items are written if that charge is accepted, none if it's refused.datasetwrites to a named dataset (a name, or aDatasetfromActor.open_dataset()) instead of the default one, with the same charge-first rules. Items in a named dataset carry no default dataset item event.
When the run's limit (the account balance, maxTotalChargeUsd or maxItems) covers only part of an item's events, that item isn't written and nothing is charged for it, and the log says so. The result's covered_counts tells how many events of each type the limit covered, for example {"page": 2}, so you can write a smaller item that fits:
result = await Actor.push_data(item, charges={"page": 5})
if result.charged_count == 0 and result.covered_counts.get("page"):
... # e.g. keep only the first result.covered_counts["page"] pages and push againevent_name= is a deprecated alias of charged_event_name. Passing both with different values raises ValueError. Passing one of the synthetic default-dataset event names raises ValueError too. See Charging.
Large pushes
The API accepts at most 10 MB (10,485,760 bytes) of JSON per request. Since SDK 0.1.45, a push that fits is sent as one request, as before, and a larger push is sent in pieces of at most 9 MB, in order. This applies to Actor.push_data(), Dataset.push_data() and a crawler's context.push_data().
- A charged push (
charged_event_name, orchargesper item) is charged and stored piece by piece, each piece as a push of its own, so a piece is billed only once it's stored. If a piece fails, the earlier pieces stay stored and billed, and the error is raised. - A
charge_per="batch"push is charged once. If one of its pieces fails, that charge is given back. - An uncharged push whose later piece fails raises
DatasetPushIncompleteError. Itsstored_itemssays how many items, from the start of the list, were stored. - One item larger than 10 MB as JSON can't be stored. The push fails at once with
DatasetItemTooLargeError(status413, with the item's position, its size and the limit), and nothing of that push is sent or charged. Keep large fields in the key-value store instead.
Both errors are ApiClientError subclasses, importable from scrapely.storage.client. See Errors.
Actor.get_value() and Actor.set_value()
These are shortcuts for the default key-value store:
state = await Actor.get_value("STATE", default_value={"cursor": None})
await Actor.set_value("STATE", {"cursor": "next"})
await Actor.set_value("PAGE_HTML", "<h1>Hello</h1>", content_type="text/html")| Parameter | Meaning |
|---|---|
key | Record key |
value | Record value. Without content_type, the value is stored as JSON |
content_type | MIME type to store the record with |
To delete a record, open the store and call delete_value():
store = await Actor.open_key_value_store()
await store.delete_value("TEMPORARY")On platform storage, set_value(key, None) also deletes the record.
Always await writes. For other stores and for reading results back, see Storages.