Skip to main content

Basic commands

Learn how to use basic commands of the Apify SDK for both JavaScript and Python.


This page covers essential commands for the Apify SDK in JavaScript & Python. These commands are designed to be used within a running Actor, either in a local environment or on the Apify platform.

Initialize your Actor

Before using any Apify SDK methods, initialize your Actor. This step prepares the Actor to receive events from the Apify platform, sets up machine and storage configurations, and clears previous local storage states.

Use the init() method to initialize your Actor. Pair it with exit() to properly terminate the Actor. For more information on exit(), go to Exit Actor.

import { Actor } from 'apify';

await Actor.init();
console.log('Actor starting...');
// ...
await Actor.exit();

Alternatively, use the main() function for environments that don't support top-level awaits. The main() function is syntax-sugar for init() and exit(). It will call init() before it executes its callback and exit() after the callback resolves.

import { Actor } from 'apify';

Actor.main(async () => {
console.log('Actor starting...');
// ...
});

Get input

Access the Actor's input object, which is stored as a JSON file in the Actor's default key-value store. The input is an object with properties. If the Actor defines the input schema, the input object is guaranteed to conform to it.

import { Actor } from 'apify';

await Actor.init();

const input = await Actor.getInput();
console.log(input);
// prints: {'option1': 'aaa', 'option2': 456}

await Actor.exit();

Usually, the file is called INPUT, but the exact key is defined in the ACTOR_INPUT_KEY environment variable.

Key-value store access

Use the Key-value store to read and write arbitrary files

import { Actor } from 'apify';

await Actor.init();

// Save object to store (stringified to JSON)
await Actor.setValue('my_state', { something: 123 });

// Save binary file to store with content type
await Actor.setValue('screenshot.png', buffer, { contentType: 'image/png' });

// Get a record from the store (automatically parsed from JSON)
const value = await Actor.getValue('my_state');

// Access another key-value store by its name
const store = await Actor.openKeyValueStore('screenshots-store');
await store.setValue('screenshot.png', buffer, { contentType: 'image/png' });

await Actor.exit();

Push results to the dataset

Store larger results in a Dataset, an append-only object storage

Note that Datasets can optionally be equipped with the schema that ensures only certain kinds of objects are stored in them.

import { Actor } from 'apify';

await Actor.init();

// Append result object to the default dataset associated with the run
await Actor.pushData({ someResult: 123 });

await Actor.exit();

Exit Actor

When an Actor's main process terminates, the Actor run is considered finished. The process exit code determines Actor's final status:

  • Exit code 0: Status SUCCEEDED
  • Exit code not equal to 0: Status FAILED

By default, the platform sets a generic status message like Actor exit with exit code 0. However, you can provide more informative message using the SDK's exit methods.

Basic exit

Use the exit() method to terminate the Actor with a custom status message:

import { Actor } from 'apify';

await Actor.init();
// ...
// Actor will finish with 'SUCCEEDED' status
await Actor.exit('Succeeded, crawled 50 pages');

Immediate exit

To exit immediately without calling exit handlers:

import { Actor } from 'apify';

await Actor.init();
// ...
// Exit right away without calling `exit` handlers at all
await Actor.exit('Done right now', { timeoutSecs: 0 });

Failed exit

To indicate a failed run:

import { Actor } from 'apify';

await Actor.init();
// ...
// Actor will finish with 'FAILED' status
await Actor.exit('Could not finish the crawl, try increasing memory', { exitCode: 1 });

Preferred exit methods

The SDK provides convenient methods for exiting Actors:

  1. Use exit() with custom messages to inform users about the Actor's achievements or issues.

  2. The exit() method emits exit event allowing components to perform cleanup or state persistence.

Example of a failed exit using a shorthand method:

import { Actor } from 'apify';

await Actor.init();
// ...
// Or nicer way using this syntactic sugar:
await Actor.fail('Could not finish the crawl, try increasing memory');

Exit event handlers (JavaScript only)

In JavaScript, you can register handlers for the exit event:

import { Actor } from 'apify';

await Actor.init();

// Register a handler to be called on exit.
// Note that the handler has `timeoutSecs` to finish its job.
Actor.on('exit', ({ statusMessage, exitCode, timeoutSecs }) => {
// Perform cleanup...
});

await Actor.exit();