Skip to content
JavaScript SDK

API reference

Every Actor member and root export of the JavaScript SDK.

A complete listing of the Actor API and the root exports of scrapely. For explanations and examples, follow the topic pages linked from the SDK overview.

Lifecycle

MemberSignatureNotes
initinit(options?: InitOptions): Promise<void>Connects storage, events, and charging
mainmain<T>(userFunc: UserFunc<T>, options?: MainOptions): Promise<T>init, run, shut down, process.exit()
exitexit(messageOrOptions?: string | ExitOptions, options?: ExitOptions): Promise<void>Exit code 0 by default
failfail(messageOrOptions?: string | ExitOptions, options?: ExitOptions): Promise<void>Exit code 1 by default
rebootreboot(options?: RebootOptions): Promise<void>Restarts the container, keeps the run; doesn't return
abortabort(runId?: string, options?: AbortOptions): Promise<ActorRun>Aborts this run or another
metamorphmetamorph(targetActorId: string, input?: Record<string, any> | string | Buffer, options?: MetamorphOptions): Promise<void>Hands the run to another Actor; doesn't return
TypeFields
InitOptionsconfiguration?, storage?: ApiStorageConfig | StorageClient, gracefulShutdown? (true), gracefulShutdownDelayMillis? (0)
ExitOptionsstatusMessage?, timeoutSecs? (30), exitCode?, exit? (true)
MainOptionsExitOptions & InitOptions; exit is ignored
AbortOptionsgracefully?, statusMessage?, token?
MetamorphOptionsbuild? (ignored on someone else's Actor, which runs the User Build), contentType? (not sent)
RebootOptionsNo public fields
UserFunc<T>() => T | Promise<T>

Input and output

MemberSignature
getInputgetInput<T = ActorInput>(): Promise<T | null>
getInputOrThrowgetInputOrThrow<T = ActorInput>(): Promise<T>
inputstatic get input(): Record<string, any> | undefined
pushDatapushData<Data extends Record<string, any>>(item: Data | Data[]): Promise<void>
pushDatapushData<Data extends Record<string, any>>(item: Data | Data[], eventName: string): Promise<ChargeResult>
getValuegetValue<T>(key: string): Promise<T | null>
setValuesetValue<T>(key: string, value: T | null, options?: RecordOptions): Promise<void>

ActorInput is Record<string, any> | string | Buffer. RecordOptions has contentType?, doNotRetryTimeouts?, and timeoutSecs?. Passing null to setValue() deletes the record. On pay-per-event Actors with a default dataset item event, pushData(item) also returns a ChargeResult.

Storages

MemberSignature
openDatasetopenDataset<T>(nameOrIdOrOptions?: string | null | StorageIdentifier, openOptions?: OpenStorageOptions): Promise<Dataset<T>>
openKeyValueStoreopenKeyValueStore(nameOrIdOrOptions?: string | null | StorageIdentifier, openOptions?: OpenStorageOptions): Promise<KeyValueStore>
openRequestQueueopenRequestQueue(nameOrIdOrOptions?: string | null | StorageIdentifier, openOptions?: OpenStorageOptions): Promise<RequestQueue>

StorageIdentifier is { id }, { name }, or { alias }. OpenStorageOptions has config? and forceCloud?. With no argument, each method opens the run's default storage.

Events and state

MemberSignature
onon(event: EventTypeName | 'cpuInfo', listener: (...args: any[]) => void): void
offoff(event: EventTypeName | 'cpuInfo', listener?: (...args: any[]) => void): void
eventsstatic get events(): EventEmitter
useStateuseState<State>(nameOrInitialState?: string | State, defaultValue?: State, options?: { keyValueStoreName?: string; config?: Configuration }): Promise<State>
setStatusMessagesetStatusMessage(message: string, options?: SetStatusMessageOptions): Promise<ActorRun>

SetStatusMessageOptions has isStatusMessageTerminal? and level?: 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR'. off() with no listener removes every listener for that event.

Orchestration and webhooks

MemberSignature
callcall(actorId: string, input?: unknown, options?: CallOptions): Promise<ActorRun>
callTaskcallTask(taskId: string, input?: Record<string, any>, options?: CallTaskOptions): Promise<ActorRun>
startstart(actorId: string, input?: Record<string, any>, options?: StartOptions): Promise<ActorRun>
addWebhookaddWebhook(options: WebhookOptions): Promise<Webhook | undefined>

CallOptions, CallTaskOptions, and StartOptions are the scrapely-client call and start options plus token? and timeout?: number | 'inherit'. WebhookOptions has eventTypes, requestUrl, and optional payloadTemplate, headersTemplate, description, idempotencyKey, ignoreSslErrors, doNotRetry, shouldInterpolateStrings, and isApifyIntegration.

Charging

MemberSignature
chargecharge(options: ChargeOptions | string, count?: number): Promise<ChargeResult>
getChargingManagergetChargingManager(): ChargingManager
TypeFields
ChargeOptionseventName: string, count?: number (1)
ChargeResulteventChargeLimitReached: boolean, chargedCount: number, chargeableWithinLimit: Record<string, number>
ActorPricingInfopricingModel?, isPayPerEvent, maxTotalChargeUsd, perEventPrices: Record<string, number>

ChargingManager has getPricingInfo(), getChargedEventCount(eventName), getMaxTotalChargeUsd(), calculateMaxEventChargeCountWithinLimit(eventName), calculatePushDataLimits({ eventName, isDefaultDataset, items }) (returns { eventsToCharge, limitedItems }), and charge(options). See Charging.

Proxy and browser

MemberSignature
createProxyConfigurationcreateProxyConfiguration(options?: CreateProxyConfigurationOptions): Promise<ProxyConfiguration | undefined>
createBrowserConfigurationcreateBrowserConfiguration(options?: BrowserConfigurationOptions): Promise<BrowserConfiguration>

CreateProxyConfigurationOptions adds useScrapelyProxy? and checkAccess? (true) to ProxyConfigurationOptions. That type adds groups?, countryCode?, subdivisionCode?, password?, tieredProxyConfig?, and the scrapelyProxyGroups? / scrapelyProxyCountry? / scrapelyProxySubdivision? input-field options to Crawlee's proxy options (proxyUrls, newUrlFunction, tieredProxyUrls).

BrowserConfigurationOptions has countryCode?, state?, city?, os?: 'windows' | 'macos' | 'android', timezone?, language?, adblock?, solveCloudflare?, and the browserCountry? / browserState? / browserCity? aliases. See Proxy & events.

Environment and clients

MemberSignature
isAtHomeisAtHome(): boolean
isStandbyisStandby(): boolean
getEnvgetEnv(): ApifyEnv
getenvstatic getenv(key: string): string | undefined
getRemainingTimestatic getRemainingTime(): number | undefined
configConfiguration (static getter and instance property)
apifyClientget apifyClient(): ApifyClient
newClientnewClient(options?: { token?: string; baseUrl?: string }): ApifyClient
getDefaultInstancestatic getDefaultInstance(): Actor
utilsstatic readonly utils: { enqueueLinks }

getRemainingTime() returns milliseconds until the run times out, or undefined when there's no timeout. ApifyClient is the scrapely-client client; see JavaScript client.

Run metadata

All of these are static getters.

GetterType
fullNamestring | undefined
taskIdstring | undefined
buildIdstring | undefined
buildNumberstring | undefined
buildTagsstring[]
startedAtstring | undefined
timeoutAtstring | undefined
tokenstring | undefined
userIdstring | undefined
isPayingboolean
apiPublicBaseUrlstring | undefined

There are no web server or standby getters. Use Actor.config.get('containerPort'), Actor.config.get('containerUrl'), and Actor.config.get('standbyUrl'); see Standby mode.

Static-only members

events, input, getenv(), getRemainingTime(), getDefaultInstance(), utils, and the run metadata getters exist only as statics. Everything else, including getEnv(), isAtHome(), and isStandby(), also exists on instances (new Actor() or Actor.getDefaultInstance()).

Root exports

Actor

Actor, ACTOR_PERMISSION_LEVEL (NONE, RUN, WRITE, ADMIN), EXIT_CODES (SUCCESS: 0, ERROR_USER_FUNCTION_THREW: 91, ERROR_UNKNOWN: 92)

Types: ActorInput, InitOptions, MainOptions, ExitOptions, RebootOptions, AbortOptions, MetamorphOptions, RecordOptions, OpenStorageOptions, CallOptions, CallTaskOptions, StartOptions, SetStatusMessageOptions, WebhookOptions, ApifyEnv, UserFunc, Token, Timeout, StorageAlias, StorageId, StorageName, StorageIdentifier, StorageIdentifierWithoutAlias

Configuration and events

Configuration, ConfigurationOptions (type), PlatformEventManager

API client (from scrapely-client)

ApifyClient, ApifyClientOptions (type), ActorRun (type)

Charging

ChargingManager, ChargeUncertainError, ChargeRejectedError, DEFAULT_DATASET_ITEM_EVENT, APIFY_DEFAULT_DATASET_ITEM_EVENT, SCRAPELY_DEFAULT_DATASET_ITEM_EVENT, hasDefaultDatasetItemEventInPricing, isDefaultDatasetItemEvent, resolveDefaultDatasetItemEventName, mergeChargeResults, pushDataAndCharge, ChargeOptions, ChargeResult, ActorPricingInfo (types)

Lower-level charging and storage internals are exported too: createChargingClient, pushDataChargingContext, USES_PUSH_DATA_INTERCEPTION, PpeAwarePushDataContext, BillingTracker, BillingClient, ApiStorageClientFactory, ApiStorageConfig. Most Actors don't need them.

Proxy

ProxyConfiguration, createProxyConfiguration, isPlatformProxySentinel, validateProxySessionId, getPlatformProxyEnv, and the types ProxyConfigurationOptions, CreateProxyConfigurationOptions, PlatformProxyEnv, ProxyInfo, ProxyConfigurationFunction

Browser

BrowserConfiguration, createBrowserConfiguration, BrowserConfigurationOptions (type), setCdpUrl

Storage and requests

Dataset, KeyValueStore, RequestQueue, ScrapelyRequestList, RequestListSource (type), KeyConsumer (type), and the Crawlee types KeyValueStoreIteratorOptions, KeyValueStoreOptions, DatasetConsumer, DatasetContent, DatasetDataOptions, DatasetIteratorOptions, DatasetMapper, DatasetOptions, DatasetReducer, QueueOperationInfo, RequestQueueOperationOptions, RequestQueueOptions

Crawlee re-exports

Crawlers (BasicCrawler, BrowserCrawler, CheerioCrawler, HttpCrawler, JSDOMCrawler, LinkeDOMCrawler, PlaywrightCrawler, PuppeteerCrawler), browser utilities (playwrightUtils, puppeteerUtils), and logging (log, Log, LogLevel, Logger, LoggerJson, LoggerText, LoggerOptions)

Helpers

utils, enqueueLinks, EnqueueLinksOptions (type)

Request and RequestList aren't re-exported; import them from crawlee when you need them.

Import side effects

Importing the package does two things:

  • It copies ACTOR_LOG_LEVEL and ACTOR_LOG_FORMAT to APIFY_LOG_LEVEL and APIFY_LOG_FORMAT when those aren't set. The log library reads them once when it loads, so import scrapely before crawlee or any @crawlee/* package.
  • It patches Crawlee's browser pool and the Playwright and Puppeteer launchers to connect to the Scrape.ly browser when the Actor's build has it turned on (SCRAPELY_BROWSER_PATCH isn't 0). This also covers Crawlee copies that were loaded earlier.

What's next