Skip to main content
Version: 1.7

Actor

{"content": ["The main class of the SDK, through which all the actor operations should be done."]}

Index

Constructors

__init__

  • __init__(config): None
  • {"content": ["Create an Actor instance.\n\nNote that you don't have to do this, all the methods on this class function as classmethods too,\nand that is their preferred usage.\n", {"


    Parameters

    • config: Configuration | None = None

    Returns None

Properties

apify_client

apify_client:

{"content": ["The ApifyClientAsync instance the Actor instance uses."]}

config

config:

{"content": ["The Configuration instance the Actor instance uses."]}

event_manager

event_manager:

{"content": ["The EventManager instance the Actor instance uses."]}

log

log:

{"content": ["The logging.Logger instance the Actor uses."]}

Methods

abort

  • async abort(run_id, *, token, gracefully): dict
  • {"content": ["Abort given actor run on the Apify platform using the current user account (determined by the APIFY_TOKEN environment variable).\n", {"


    Parameters

    • run_id: str
    • keyword-onlytoken: str | None = None
    • keyword-onlygracefully: bool | None = None

    Returns dict

add_webhook

  • async add_webhook(*, event_types, request_url, payload_template, ignore_ssl_errors, do_not_retry, idempotency_key): dict
  • {"content": ["Create an ad-hoc webhook for the current actor run.\n\nThis webhook lets you receive a notification when the actor run finished or failed.\n\nNote that webhooks are only supported for actors running on the Apify platform.\nWhen running the actor locally, the function will print a warning and have no effect.\n\nFor more information about Apify actor webhooks, please see the documentation.\n", {"


    Parameters

    • keyword-onlyevent_types: list[WebhookEventType]
    • keyword-onlyrequest_url: str
    • keyword-onlypayload_template: str | None = None
    • keyword-onlyignore_ssl_errors: bool | None = None
    • keyword-onlydo_not_retry: bool | None = None
    • keyword-onlyidempotency_key: str | None = None

    Returns dict

call

  • async call(actor_id, run_input, *, token, content_type, build, memory_mbytes, timeout_secs, webhooks, wait_secs): dict | None
  • {"content": ["Start an actor on the Apify Platform and wait for it to finish before returning.\n\nIt waits indefinitely, unless the wait_secs argument is provided.\n", {"


    Parameters

    • actor_id: str
    • run_input: Any = None
    • keyword-onlytoken: str | None = None
    • keyword-onlycontent_type: str | None = None
    • keyword-onlybuild: str | None = None
    • keyword-onlymemory_mbytes: int | None = None
    • keyword-onlytimeout_secs: int | None = None
    • keyword-onlywebhooks: list[dict] | None = None
    • keyword-onlywait_secs: int | None = None

    Returns dict | None

call_task

  • async call_task(task_id, task_input, *, build, memory_mbytes, timeout_secs, webhooks, wait_secs, token): dict | None
  • {"content": ["Start an actor task on the Apify Platform and wait for it to finish before returning.\n\nIt waits indefinitely, unless the wait_secs argument is provided.\n\nNote that an actor task is a saved input configuration and options for an actor.\nIf you want to run an actor directly rather than an actor task, please use the Actor.call\n", {"


    Parameters

    • task_id: str
    • task_input: dict | None = None
    • keyword-onlybuild: str | None = None
    • keyword-onlymemory_mbytes: int | None = None
    • keyword-onlytimeout_secs: int | None = None
    • keyword-onlywebhooks: list[dict] | None = None
    • keyword-onlywait_secs: int | None = None
    • keyword-onlytoken: str | None = None

    Returns dict | None

create_proxy_configuration

  • async create_proxy_configuration(*, actor_proxy_input, password, groups, country_code, proxy_urls, new_url_function): ProxyConfiguration | None
  • {"content": ["Create a ProxyConfiguration object with the passed proxy configuration.\n\nConfigures connection to a proxy server with the provided options.\nProxy servers are used to prevent target websites from blocking your crawlers based on IP address rate limits or blacklists.\n\nFor more details and code examples, see the ProxyConfiguration class.\n", {"


    Parameters

    • keyword-onlyactor_proxy_input: dict | None = None
    • keyword-onlypassword: str | None = None
    • keyword-onlygroups: list[str] | None = None
    • keyword-onlycountry_code: str | None = None
    • keyword-onlyproxy_urls: list[str] | None = None
    • keyword-onlynew_url_function: Callable[[str | None], str] | Callable[[str | None], Awaitable[str]] | None = None

    Returns ProxyConfiguration | None

exit

  • async exit(*, exit_code, event_listeners_timeout_secs, status_message, cleanup_timeout): None
  • {"content": ["Exit the actor instance.\n\nThis stops the Actor instance.\nIt cancels all the intervals for regularly sending PERSIST_STATE events,\nsends a final PERSIST_STATE event,\nwaits for all the event listeners to finish,\nand stops the event manager.\n", {"


    Parameters

    • keyword-onlyexit_code: int = 0
    • keyword-onlyevent_listeners_timeout_secs: float | None = EVENT_LISTENERS_TIMEOUT_SECS
    • keyword-onlystatus_message: str | None = None
    • keyword-onlycleanup_timeout: timedelta = timedelta(seconds=30)

    Returns None

fail

  • async fail(*, exit_code, exception, status_message): None
  • {"content": ["Fail the actor instance.\n\nThis performs all the same steps as Actor.exit(),\nbut it additionally sets the exit code to 1 (by default).\n", {"


    Parameters

    • keyword-onlyexit_code: int = 1
    • keyword-onlyexception: BaseException | None = None
    • keyword-onlystatus_message: str | None = None

    Returns None

get_env

  • get_env(): dict
  • {"content": ["Return a dictionary with information parsed from all the APIFY_XXX environment variables.\n\nFor a list of all the environment variables,\nsee the Actor documentation.\nIf some variables are not defined or are invalid, the corresponding value in the resulting dictionary will be None."]}


    Returns dict

get_input

  • async get_input(): Any
  • {"content": ["Get the actor input value from the default key-value store associated with the current actor run."]}


    Returns Any

get_system_info

  • get_system_info(): dict
  • {"content": ["Get the current system info."]}


    Returns dict

get_value

  • async get_value(key, default_value): Any
  • {"content": ["Get a value from the default key-value store associated with the current actor run.\n", {"


    Parameters

    • key: str
    • default_value: Any = None

    Returns Any

init

  • async init(): None
  • {"content": ["Initialize the actor instance.\n\nThis initializes the Actor instance.\nIt configures the right storage client based on whether the actor is running locally or on the Apify platform,\nit initializes the event manager for processing actor events,\nand starts an interval for regularly sending PERSIST_STATE events,\nso that the actor can regularly persist its state in response to these events.\n\nThis method should be called immediately before performing any additional actor actions,\nand it should be called only once."]}


    Returns None

is_at_home

  • is_at_home(): bool
  • {"content": ["Return True when the actor is running on the Apify platform, and False otherwise (for example when running locally)."]}


    Returns bool

main

  • async main(main_actor_function): MainReturnType | None
  • {"content": ["Initialize the actor, run the passed function and finish the actor cleanly.\n\nThe Actor.main() function is optional and is provided merely for your convenience.\nIt is mainly useful when you're running your code as an actor on the Apify platform.\n\nThe Actor.main() function performs the following actions:\n\n- When running on the Apify platform (i.e. APIFY_IS_AT_HOME environment variable is set),\nit sets up a connection to listen for platform events.\nFor example, to get a notification about an imminent migration to another server.\n- It invokes the user function passed as the main_actor_function parameter.\n- If the user function was an async function, it awaits it.\n- If the user function throws an exception or some other error is encountered,\nit prints error details to console so that they are stored to the log,\nand finishes the actor cleanly.\n- Finally, it exits the Python process, with zero exit code on success and non-zero on errors.\n", {"


    Parameters

    • main_actor_function: Callable[[], MainReturnType]

    Returns MainReturnType | None

metamorph

  • async metamorph(target_actor_id, run_input, *, target_actor_build, content_type, custom_after_sleep_millis): None
  • {"content": ["Transform this actor run to an actor run of a different actor.\n\nThe platform stops the current actor container and starts a new container with the new actor instead.\nAll the default storages are preserved,\nand the new input is stored under the INPUT-METAMORPH-1 key in the same default key-value store.\n", {"


    Parameters

    • target_actor_id: str
    • run_input: Any = None
    • keyword-onlytarget_actor_build: str | None = None
    • keyword-onlycontent_type: str | None = None
    • keyword-onlycustom_after_sleep_millis: int | None = None

    Returns None

new_client

  • new_client(*, token, api_url, max_retries, min_delay_between_retries_millis, timeout_secs): ApifyClientAsync
  • {"content": ["Return a new instance of the Apify API client.\n\nThe ApifyClientAsync class is provided by the apify-client package,\nand it is automatically configured using the APIFY_API_BASE_URL and APIFY_TOKEN environment variables.\n\nYou can override the token via the available options.\nThat's useful if you want to use the client as a different Apify user than the SDK internals are using.\n", {"


    Parameters

    • keyword-onlytoken: str | None = None
    • keyword-onlyapi_url: str | None = None
    • keyword-onlymax_retries: int | None = None
    • keyword-onlymin_delay_between_retries_millis: int | None = None
    • keyword-onlytimeout_secs: int | None = None

    Returns ApifyClientAsync

off

  • off(event_name, listener): None
  • {"content": ["Remove a listener, or all listeners, from an actor event.\n", {"


    Parameters

    • event_name: ActorEventTypes
    • listener: Callable | None = None

    Returns None

on

  • on(event_name, listener): Callable
  • {"content": ["Add an event listener to the actor's event manager.\n\nThe following events can be emitted:\n- ActorEventTypes.SYSTEM_INFO:\nEmitted every minute, the event data contains info about the resource usage of the actor.\n- ActorEventTypes.MIGRATING:\nEmitted when the actor running on the Apify platform is going to be migrated to another worker server soon.\nYou can use it to persist the state of the actor and gracefully stop your in-progress tasks,\nso that they are not interrupted by the migration..\n- ActorEventTypes.PERSIST_STATE:\nEmitted in regular intervals (by default 60 seconds) to notify the actor that it should persist its state,\nin order to avoid repeating all work when the actor restarts.\nThis event is automatically emitted together with the migrating event,\nin which case the isMigrating flag in the event data is set to True, otherwise the flag is False.\nNote that this event is provided merely for your convenience,\nyou can achieve the same effect using an interval and listening for the migrating event.\n- ActorEventTypes.ABORTING:\nWhen a user aborts an actor run on the Apify platform,\nthey can choose to abort it gracefully, to allow the actor some time before getting terminated.\nThis graceful abort emits the aborting event, which you can use to clean up the actor state.\n", {"


    Parameters

    • event_name: ActorEventTypes
    • listener: Callable

    Returns Callable

open_dataset

  • async open_dataset(*, id, name, force_cloud): Dataset
  • {"content": ["Open a dataset.\n\nDatasets are used to store structured data where each object stored has the same attributes,\nsuch as online store products or real estate offers.\nThe actual data is stored either on the local filesystem or in the Apify cloud.\n", {"


    Parameters

    • keyword-onlyid: str | None = None
    • keyword-onlyname: str | None = None
    • keyword-onlyforce_cloud: bool = False

    Returns Dataset

open_key_value_store

  • async open_key_value_store(*, id, name, force_cloud): KeyValueStore
  • {"content": ["Open a key-value store.\n\nKey-value stores are used to store records or files, along with their MIME content type.\nThe records are stored and retrieved using a unique key.\nThe actual data is stored either on a local filesystem or in the Apify cloud.\n", {"


    Parameters

    • keyword-onlyid: str | None = None
    • keyword-onlyname: str | None = None
    • keyword-onlyforce_cloud: bool = False

    Returns KeyValueStore

open_request_queue

  • async open_request_queue(*, id, name, force_cloud): RequestQueue
  • {"content": ["Open a request queue.\n\nRequest queue represents a queue of URLs to crawl, which is stored either on local filesystem or in the Apify cloud.\nThe queue is used for deep crawling of websites, where you start with several URLs and then\nrecursively follow links to other pages. The data structure supports both breadth-first\nand depth-first crawling orders.\n", {"


    Parameters

    • keyword-onlyid: str | None = None
    • keyword-onlyname: str | None = None
    • keyword-onlyforce_cloud: bool = False

    Returns RequestQueue

push_data

  • async push_data(data): None
  • {"content": ["Store an object or a list of objects to the default dataset of the current actor run.\n", {"


    Parameters

    • data: Any

    Returns None

reboot

  • async reboot(*, event_listeners_timeout_secs, custom_after_sleep_millis): None
  • {"content": ["Internally reboot this actor.\n\nThe system stops the current container and starts a new one, with the same run ID and default storages.\n", {"


    Parameters

    • keyword-onlyevent_listeners_timeout_secs: int | None = EVENT_LISTENERS_TIMEOUT_SECS
    • keyword-onlycustom_after_sleep_millis: int | None = None

    Returns None

set_status_message

  • async set_status_message(status_message, *, is_terminal): dict | None
  • {"content": ["Set the status message for the current actor run.\n", {"


    Parameters

    • status_message: str
    • keyword-onlyis_terminal: bool | None = None

    Returns dict | None

set_value

  • async set_value(key, value, *, content_type): None
  • {"content": ["Set or delete a value in the default key-value store associated with the current actor run.\n", {"


    Parameters

    • key: str
    • value: Any
    • keyword-onlycontent_type: str | None = None

    Returns None

start

  • async start(actor_id, run_input, *, token, content_type, build, memory_mbytes, timeout_secs, wait_for_finish, webhooks): dict
  • {"content": ["Run an actor on the Apify platform.\n\nUnlike Actor.call, this method just starts the run without waiting for finish.\n", {"


    Parameters

    • actor_id: str
    • run_input: Any = None
    • keyword-onlytoken: str | None = None
    • keyword-onlycontent_type: str | None = None
    • keyword-onlybuild: str | None = None
    • keyword-onlymemory_mbytes: int | None = None
    • keyword-onlytimeout_secs: int | None = None
    • keyword-onlywait_for_finish: int | None = None
    • keyword-onlywebhooks: list[dict] | None = None

    Returns dict