Skip to content
CLI

Actor runtime

Commands for use inside a running Actor: input, output, storage, and charging.

The scrapely actor commands are for code running inside an Actor run. They read the run's input, write to its default dataset and key-value store, and charge pay-per-event events, using the environment variables the platform sets in the container. They let an Actor written as a shell script, or in a language without a Scrape.ly SDK, use the platform. generate-schema-types and calculate-memory are the exceptions: they are development tools you run on your machine.

In JavaScript and Python, use the SDK instead: Actor.getInput(), Actor.pushData(), Actor.setValue(), and so on. See the JavaScript SDK and the Python SDK.

scrapely actor is not scrapely actors. The plural group manages Actors in your account from your machine. See Actor management.

CommandPurpose
actor get-inputPrint the run's input
actor push-data [item]Append items to the default dataset
actor get-value <key>Print a record from the default key-value store
actor set-value <key> [value]Write or delete a record in the default key-value store
actor get-public-url <key>Print a signed URL of a record that works without a token
actor charge <eventName>Charge a pay-per-event event
actor generate-schema-types [path]Generate TypeScript types from the input schema
actor calculate-memoryEvaluate the memory expression in the Actor config. Same as scrapely calculate-memory.

Read the input

.bash
scrapely actor get-input
URL=$(scrapely actor get-input | jq -r '.startUrls[0].url')

get-input prints the input as JSON. On the platform, where ACTOR_DEFAULT_KEY_VALUE_STORE_ID is set, it reads the INPUT record from the run's default key-value store through the API. If that record doesn't exist or can't be read, it falls back to the ACTOR_INPUT environment variable, which can hold plain or base64-encoded JSON. Under scrapely run on your machine, it reads ACTOR_INPUT, which run sets. With neither available, it fails.

Write results

Push data

.bash
scrapely actor push-data '{"url":"https://example.com","title":"Example"}'
echo '[{"url":"https://a.example"},{"url":"https://b.example"}]' | scrapely actor push-data

push-data appends a JSON object, or an array of objects, to the run's default dataset. Without an argument it reads from stdin. Invalid JSON is rejected. The dataset is ACTOR_DEFAULT_DATASET_ID, or APIFY_DEFAULT_DATASET_ID.

To push many items, send one array rather than calling the command once per item. Each call is a separate API request.

Read a record

.bash
scrapely actor get-value STATE

get-value prints a record from the run's default key-value store. JSON records are pretty-printed. A missing key is an error.

Write a record

.bash
scrapely actor set-value OUTPUT '{"processed":42}'
scrapely actor set-value page.html "$(cat page.html)" -c text/html
OptionDescription
-c, --content-type <type>Content type of the record. Default application/json, which requires valid JSON.

An empty or missing value deletes the record:

.bash
scrapely actor set-value TEMPORARY

Get a record URL

.bash
scrapely actor get-public-url OUTPUT
scrapely actor get-public-url OUTPUT --expires-in 86400

get-public-url prints a signed URL of a record in the run's default store: <public API URL>/v2/key-value-stores/<storeId>/records/<key>?signature=…. Anyone with the URL can open the record without a token until it expires. The host is the public API URL the platform sets in the run (APIFY_API_PUBLIC_BASE_URL), or the API URL when that isn't set.

OptionDescription
--expires-in <seconds>How long the URL stays valid. Default 3600 (one hour). Must be a positive number.

The signature is made with the store's URL signing key, which the CLI reads from the store through the API. The run's own token can read it for its default store. If the token in use can't read the key, the command fails with Could not sign a public URL for key-value store … and exit code 1 instead of printing a link that doesn't work. Outside a run, log in as the store's owner. See Storage for other ways to share results.

Charge for events

.bash
scrapely actor charge result-scraped
scrapely actor charge result-scraped --count 25 --idempotency-key "batch-7"

charge charges an event of a pay-per-event Actor for the current run. The run is ACTOR_RUN_ID, or APIFY_ACTOR_RUN_ID.

OptionDescription
--count <number>Number of events. Default 1.
--idempotency-key <key>Key that makes a repeated charge count only once
--test-pay-per-eventPrint what would be charged and send nothing. Needs no run ID.

Without --idempotency-key, every call gets a new key, so retrying a call after a network error charges the event twice. Pass a stable key whenever a charge can be retried. Pricing setup is covered in Publishing.

Charges are only accepted while the run is RUNNING. charge checks the run's status first: on a run that has finished it stops at once with Run … has finished (status …) and exit code 1, without sending the charge. If the run ends while a charge is being sent, the error says so too.

Generate TypeScript types

.bash
scrapely actor generate-schema-types
scrapely actor generate-schema-types .actor/input_schema.json -o src/types

generate-schema-types turns the Actor's input schema into TypeScript types. Run it on your machine in the project folder and commit the output.

Without a path, it uses the first of these files that exists: INPUT_SCHEMA.json, .actor/INPUT_SCHEMA.json, .actor/input_schema.json, input_schema.json, .actor/actor.json, .actor/scrapely.json, scrapely.json, actor.json. For a config file, it uses the schema in the config's input field, inline or by path.

It writes two files to src/__generated__/actor/:

  • input.ts, with an interface named after the schema's title in PascalCase (ActorInput if there is no title), also exported as Input.
  • index.ts, which re-exports both names.
OptionDescription
--all-optionalMake every property optional
-o, --output <dir>Output folder. It must be inside the current folder.
--strictDon't add an index signature for objects that allow additional properties

Only the input schema is converted. Dataset and output schemas are ignored.

.ts
import { Actor } from 'scrapely';
import type { Input } from './__generated__/actor/index.js';

await Actor.init();
const input = (await Actor.getInput<Input>())!;

Calculate memory

.bash
scrapely actor calculate-memory --input input.json
scrapely calculate-memory --default-memory-mbytes "get(input, 'startUrls.length', 1) * 512"

calculate-memory evaluates a memory expression locally, without calling the API, and prints Calculated memory: <MB> MB. The expression is --default-memory-mbytes, else defaultMemoryMbytes from the Actor config. The input is --input <file>, else storage/key_value_stores/default/INPUT.json, else {}. The result is rounded to the closest power of 2, kept between 128 and 32768 MB, then within the config's minMemoryMbytes and maxMemoryMbytes. See Actors for how the platform uses the memory fields of the config.

OptionDescription
--default-memory-mbytes <value>Expression to evaluate instead of the one in the config
--input <value>Path to the input JSON file
--build <value>, --timeout-secs <value>, --max-items <value>, --max-total-charge-usd <value>Run options the expression can read as {{runOptions.…}}

Environment

The platform sets these variables in every run. The runtime commands read them.

VariableUsed by
ACTOR_RUN_ID, APIFY_ACTOR_RUN_IDcharge. push-data when no dataset ID is set.
ACTOR_DEFAULT_DATASET_ID, APIFY_DEFAULT_DATASET_IDpush-data
ACTOR_DEFAULT_KEY_VALUE_STORE_ID, APIFY_DEFAULT_KEY_VALUE_STORE_IDget-input, get-value, set-value, get-public-url
ACTOR_INPUTget-input when the INPUT record can't be read

The token comes from the first of:

  1. ACTOR_TOKEN
  2. APIFY_TOKEN (set by the platform in every run)
  3. SCRAPELY_TOKEN
  4. The token saved by scrapely login

The API URL comes from the first of:

  1. APIFY_API_BASE_URL (set by the platform in every run)
  2. SCRAPELY_API_BASE_URL
  3. SCRAPELY_API_URL
  4. The API URL resolved as for other commands. See Authentication.

If any of the three API URL variables is set, the saved token is never used. A run with an API URL but no token variable fails instead of sending the token saved on the machine to that URL.

To try these commands on your machine, set the variables yourself, for example ACTOR_DEFAULT_DATASET_ID to a dataset you own, and keep using your saved login.

What's next