Actor
Index
Methods
- __aenter__
- __aexit__
- __call__
- __init__
- __repr__
- abort
- add_webhook
- call
- call_task
- charge
- configuration
- create_proxy_configuration
- event_manager
- exit
- exit_code
- fail
- get_charging_manager
- get_env
- get_input
- get_value
- init
- is_at_home
- metamorph
- new_client
- off
- on
- open_dataset
- open_key_value_store
- open_request_queue
- push_data
- reboot
- set_status_message
- set_value
- start
- status_message
Properties
Methods
__aenter__
- Enter the Actor context. - Initializes the Actor when used in an - async withblock. This method:- Sets up local or cloud storage clients depending on whether the Actor runs locally or on the Apify platform.
- Configures the event manager and starts periodic state persistence.
- Initializes the charging manager for handling charging events.
- Configures logging after all core services are registered.
 - This method must be called exactly once per Actor instance. Re-initializing an Actor or having multiple active Actor instances is not standard usage and may lead to warnings or unexpected behavior. - Returns Self
__aexit__
- Exit the Actor context. - If the block exits with an exception, the Actor fails with a non-zero exit code. Otherwise, it exits cleanly. In both cases the Actor: - Cancels periodic PERSIST_STATEevents.
- Sends a final PERSIST_STATEevent.
- Waits for all event listeners to finish.
- Stops the event manager and the charging manager.
- Optionally terminates the process with the selected exit code.
 - Parameters- exc_type: type[BaseException] | None
- exc_value: BaseException | None
- exc_traceback: TracebackType | None
 - Returns None
- Cancels periodic 
__call__
- Make a new Actor instance with a non-default configuration. - This is necessary due to the lazy object proxying of the global - Actorinstance.- Parameters- optionalconfiguration: Configuration | None = None
- optionalkeyword-onlyconfigure_logging: bool = True
- optionalkeyword-onlyexit_process: bool | None = None
- optionalkeyword-onlyexit_code: int = 0
- optionalkeyword-onlyevent_listeners_timeout: timedelta | None = EVENT_LISTENERS_TIMEOUT
- optionalkeyword-onlystatus_message: str | None = None
- optionalkeyword-onlycleanup_timeout: timedelta = timedelta(seconds=30)
 - Returns Self
__init__
- Initialize a new instance. - Parameters- optionalconfiguration: Configuration | None = None- The Actor configuration to use. If not provided, a default configuration is created. 
- optionalkeyword-onlyconfigure_logging: bool = True- Whether to set up the default logging configuration. 
- optionalkeyword-onlyexit_process: bool | None = None- Whether the Actor should call - sys.exitwhen the context manager exits. Defaults to True, except in IPython, Pytest, and Scrapy environments.
- optionalkeyword-onlyexit_code: int = 0- The exit code the Actor should use when exiting. 
- optionalkeyword-onlystatus_message: str | None = None- Final status message to display upon Actor termination. 
- optionalkeyword-onlyevent_listeners_timeout: timedelta | None = EVENT_LISTENERS_TIMEOUT- Maximum time to wait for Actor event listeners to complete before exiting. 
- optionalkeyword-onlycleanup_timeout: timedelta = timedelta(seconds=30)- Maximum time to wait for cleanup tasks to finish. 
 - Returns None
__repr__
- Returns str
abort
- Abort given Actor run on the Apify platform using the current user account. - The user account is determined by the - APIFY_TOKENenvironment variable.- Parameters- run_id: str- The ID of the Actor run to be aborted. 
- optionalkeyword-onlytoken: str | None = None- The Apify API token to use for this request (defaults to the - APIFY_TOKENenvironment variable).
- optionalkeyword-onlystatus_message: str | None = None- Status message of the Actor to be set on the platform. 
- optionalkeyword-onlygracefully: bool | None = None- If True, the Actor run will abort gracefully. It will send - abortingand- persistStateevents into the run and force-stop the run after 30 seconds. It is helpful in cases where you plan to resurrect the run later.
 - Returns ActorRun
add_webhook
- Create an ad-hoc webhook for the current Actor run. - This webhook lets you receive a notification when the Actor run finished or failed. - Note that webhooks are only supported for Actors running on the Apify platform. When running the Actor locally, the function will print a warning and have no effect. - For more information about Apify Actor webhooks, please see the documentation. - Parameters- webhook: Webhook- The webhook to be added 
- optionalkeyword-onlyignore_ssl_errors: bool | None = None- Whether the webhook should ignore SSL errors returned by request_url 
- optionalkeyword-onlydo_not_retry: bool | None = None- Whether the webhook should retry sending the payload to request_url upon failure. 
- optionalkeyword-onlyidempotency_key: str | None = None- A unique identifier of a webhook. You can use it to ensure that you won't create the same webhook multiple times. 
 - Returns None
call
- Start an Actor on the Apify Platform and wait for it to finish before returning. - It waits indefinitely, unless the wait argument is provided. - Parameters- actor_id: str- The ID of the Actor to be run. 
- optionalrun_input: Any = None- The input to pass to the Actor run. 
- optionalkeyword-onlytoken: str | None = None- The Apify API token to use for this request (defaults to the - APIFY_TOKENenvironment variable).
- optionalkeyword-onlycontent_type: str | None = None- The content type of the input. 
- optionalkeyword-onlybuild: str | None = None- Specifies the Actor build to run. It can be either a build tag or build number. By default, the run uses the build specified in the default run configuration for the Actor (typically latest). 
- optionalkeyword-onlymemory_mbytes: int | None = None- Memory limit for the run, in megabytes. By default, the run uses a memory limit specified in the default run configuration for the Actor. 
- optionalkeyword-onlytimeout: (timedelta | None) | Literal[RemainingTime] = None- Optional timeout for the run, in seconds. By default, the run uses timeout specified in the default run configuration for the Actor. Using - RemainingTimewill set timeout of the other Actor to the time remaining from this Actor timeout.
- optionalkeyword-onlywebhooks: list[Webhook] | None = None- Optional webhooks (https://docs.apify.com/webhooks) associated with the Actor run, which can be used to receive a notification, e.g. when the Actor finished or failed. If you already have a webhook set up for the Actor, you do not have to add it again here. 
- optionalkeyword-onlywait: timedelta | None = None- The maximum number of seconds the server waits for the run to finish. If not provided, waits indefinitely. 
- optionalkeyword-onlylogger: (logging.Logger | None) | Literal[default] = 'default'- Logger used to redirect logs from the Actor run. Using "default" literal means that a predefined default logger will be used. Setting - Nonewill disable any log propagation. Passing custom logger will redirect logs to the provided logger.
 - Returns ActorRun | None
call_task
- Start an Actor task on the Apify Platform and wait for it to finish before returning. - It waits indefinitely, unless the wait argument is provided. - Note that an Actor task is a saved input configuration and options for an Actor. If you want to run an Actor directly rather than an Actor task, please use the - Actor.call- Parameters- task_id: str- The ID of the Actor to be run. 
- optionaltask_input: dict | None = None- Overrides the input to pass to the Actor run. 
- optionalkeyword-onlybuild: str | None = None- Specifies the Actor build to run. It can be either a build tag or build number. By default, the run uses the build specified in the default run configuration for the Actor (typically latest). 
- optionalkeyword-onlymemory_mbytes: int | None = None- Memory limit for the run, in megabytes. By default, the run uses a memory limit specified in the default run configuration for the Actor. 
- optionalkeyword-onlytimeout: timedelta | None = None- Optional timeout for the run, in seconds. By default, the run uses timeout specified in the default run configuration for the Actor. 
- optionalkeyword-onlywebhooks: list[Webhook] | None = None- Optional webhooks (https://docs.apify.com/webhooks) associated with the Actor run, which can be used to receive a notification, e.g. when the Actor finished or failed. If you already have a webhook set up for the Actor, you do not have to add it again here. 
- optionalkeyword-onlywait: timedelta | None = None- The maximum number of seconds the server waits for the run to finish. If not provided, waits indefinitely. 
- optionalkeyword-onlytoken: str | None = None- The Apify API token to use for this request (defaults to the - APIFY_TOKENenvironment variable).
 - Returns ActorRun | None
charge
- Charge for a specified number of events - sub-operations of the Actor. - This is relevant only for the pay-per-event pricing model. - Parameters- event_name: str- Name of the event to be charged for. 
- optionalcount: int = 1- Number of events to charge for. 
 - Returns ChargeResult
configuration
- Actor configuration, uses the default instance if not explicitly set. - Returns Configuration
create_proxy_configuration
- Create a ProxyConfiguration object with the passed proxy configuration. - Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking your crawlers based on IP address rate limits or blacklists. - For more details and code examples, see the - ProxyConfigurationclass.- Parameters- optionalkeyword-onlyactor_proxy_input: dict | None = None- Proxy configuration field from the Actor input, if input has such input field. If you pass this argument, all the other arguments will be inferred from it. 
- optionalkeyword-onlypassword: str | None = None- Password for the Apify Proxy. If not provided, will use os.environ['APIFY_PROXY_PASSWORD'], if available. 
- optionalkeyword-onlygroups: list[str] | None = None- Proxy groups which the Apify Proxy should use, if provided. 
- optionalkeyword-onlycountry_code: str | None = None- Country which the Apify Proxy should use, if provided. 
- optionalkeyword-onlyproxy_urls: list[str | None] | None = None- Custom proxy server URLs which should be rotated through. 
- optionalkeyword-onlynew_url_function: _NewUrlFunction | None = None- Function which returns a custom proxy URL to be used. 
 - Returns ProxyConfiguration | None
event_manager
- Manages Apify platform events. - It uses - ApifyEventManageron the Apify platform and- LocalEventManagerotherwise.- Returns EventManager
exit
- Exit the Actor without using context-manager syntax. - Equivalent to - await Actor.__aexit__().- Parameters- optionalkeyword-onlyexit_code: int = 0- The exit code the Actor should use when exiting. 
- optionalkeyword-onlystatus_message: str | None = None- Final status message to display upon Actor termination. 
- optionalkeyword-onlyevent_listeners_timeout: timedelta | None = EVENT_LISTENERS_TIMEOUT- Maximum time to wait for Actor event listeners to complete before exiting. 
- optionalkeyword-onlycleanup_timeout: timedelta = timedelta(seconds=30)- Maximum time to wait for cleanup tasks to finish. 
 - Returns None
exit_code
- Parameters- value: int
 - Returns None
fail
- Fail the Actor instance without using context-manager syntax. - Equivalent to setting the - self.exit_codeand- self.status_messageproperties and using- await Actor.__aexit__().- Parameters- optionalkeyword-onlyexit_code: int = 1- The exit code with which the Actor should fail (defaults to - 1).
- optionalkeyword-onlyexception: BaseException | None = None- The exception with which the Actor failed. 
- optionalkeyword-onlystatus_message: str | None = None- The final status message that the Actor should display. 
 - Returns None
get_charging_manager
- Retrieve the charging manager to access granular pricing information. - Returns ChargingManager
get_env
- Return a dictionary with information parsed from all the - APIFY_XXXenvironment variables.- For a list of all the environment variables, see the Actor documentation. If some variables are not defined or are invalid, the corresponding value in the resulting dictionary will be None. - Returns dict
get_input
- Get the Actor input value from the default key-value store associated with the current Actor run. - Returns Any
get_value
- Get a value from the default key-value store associated with the current Actor run. - Parameters- key: str- The key of the record which to retrieve. 
- optionaldefault_value: Any = None- Default value returned in case the record does not exist. 
 - Returns Any
init
- Initialize the Actor without using context-manager syntax. - Equivalent to - await Actor.__aenter__().- Returns None
is_at_home
- Return - Truewhen the Actor is running on the Apify platform, and- Falseotherwise (e.g. local run).- Returns bool
metamorph
- Transform this Actor run to an Actor run of a different Actor. - The platform stops the current Actor container and starts a new container with the new Actor instead. All the default storages are preserved, and the new input is stored under the - INPUT-METAMORPH-1key in the same default key-value store.- Parameters- target_actor_id: str- ID of the target Actor that the run should be transformed into 
- optionalrun_input: Any = None- The input to pass to the new run. 
- optionalkeyword-onlytarget_actor_build: str | None = None- The build of the target Actor. It can be either a build tag or build number. By default, the run uses the build specified in the default run configuration for the target Actor (typically the latest build). 
- optionalkeyword-onlycontent_type: str | None = None- The content type of the input. 
- optionalkeyword-onlycustom_after_sleep: timedelta | None = None- How long to sleep for after the metamorph, to wait for the container to be stopped. 
 - Returns None
new_client
- Return a new instance of the Apify API client. - The - ApifyClientAsyncclass is provided by the apify-client package, and it is automatically configured using the- APIFY_API_BASE_URLand- APIFY_TOKENenvironment variables.- You can override the token via the available options. That's useful if you want to use the client as a different Apify user than the SDK internals are using. - Parameters- optionalkeyword-onlytoken: str | None = None- The Apify API token. 
- optionalkeyword-onlyapi_url: str | None = None- The URL of the Apify API server to which to connect to. Defaults to https://api.apify.com. 
- optionalkeyword-onlymax_retries: int | None = None- How many times to retry a failed request at most. 
- optionalkeyword-onlymin_delay_between_retries: timedelta | None = None- How long will the client wait between retrying requests (increases exponentially from this value). 
- optionalkeyword-onlytimeout: timedelta | None = None- The socket timeout of the HTTP requests sent to the Apify API. 
 - Returns ApifyClientAsync
off
- Remove a listener, or all listeners, from an Actor event. - Parameters- event_name: Event- The Actor event for which to remove listeners. 
- optionallistener: Callable | None = None- The listener which is supposed to be removed. If not passed, all listeners of this event are removed. 
 - Returns None
on
- Add an event listener to the Actor's event manager. - The following events can be emitted: - Event.SYSTEM_INFO: Emitted every minute; the event data contains information about the Actor's resource usage.
- Event.MIGRATING: Emitted when the Actor on the Apify platform is about to be migrated to another worker server. Use this event to persist the Actor's state and gracefully stop in-progress tasks, preventing disruption.
- Event.PERSIST_STATE: Emitted regularly (default: 60 seconds) to notify the Actor to persist its state, preventing work repetition after a restart. This event is emitted together with the- MIGRATINGevent, where the- isMigratingflag in the event data is- True; otherwise, the flag is- False. This event is for convenience; the same effect can be achieved by setting an interval and listening for the- MIGRATINGevent.
- Event.ABORTING: Emitted when a user aborts an Actor run on the Apify platform, allowing the Actor time to clean up its state if the abort is graceful.
 - Parameters- event_name: Event- The Actor event to listen for. 
- listener: EventListener[Any]- The function to be called when the event is emitted (can be async). 
 - Returns EventListener[Any]
open_dataset
- Open a dataset. - Datasets are used to store structured data where each object stored has the same attributes, such as online store products or real estate offers. The actual data is stored either on the local filesystem or in the Apify cloud. - Parameters- optionalkeyword-onlyid: str | None = None- The ID of the dataset to open. If provided, searches for existing dataset by ID. Mutually exclusive with name and alias. 
- optionalkeyword-onlyalias: str | None = None- The alias of the dataset to open (run scope, creates unnamed storage). Mutually exclusive with id and name. 
- optionalkeyword-onlyname: str | None = None- The name of the dataset to open (global scope, persists across runs). Mutually exclusive with id and alias. 
- optionalkeyword-onlyforce_cloud: bool = False- If set to - Truethen the Apify cloud storage is always used. This way it is possible to combine local and cloud storage.
 - Returns Dataset
open_key_value_store
- Open a key-value store. - Key-value stores are used to store records or files, along with their MIME content type. The records are stored and retrieved using a unique key. The actual data is stored either on a local filesystem or in the Apify cloud. - Parameters- optionalkeyword-onlyid: str | None = None- The ID of the KVS to open. If provided, searches for existing KVS by ID. Mutually exclusive with name and alias. 
- optionalkeyword-onlyalias: str | None = None- The alias of the KVS to open (run scope, creates unnamed storage). Mutually exclusive with id and name. 
- optionalkeyword-onlyname: str | None = None- The name of the KVS to open (global scope, persists across runs). Mutually exclusive with id and alias. 
- optionalkeyword-onlyforce_cloud: bool = False- If set to - Truethen the Apify cloud storage is always used. This way it is possible to combine local and cloud storage.
 - Returns KeyValueStore
open_request_queue
- Open a request queue. - Request queue represents a queue of URLs to crawl, which is stored either on local filesystem or in the Apify cloud. The queue is used for deep crawling of websites, where you start with several URLs and then recursively follow links to other pages. The data structure supports both breadth-first and depth-first crawling orders. - Parameters- optionalkeyword-onlyid: str | None = None- The ID of the RQ to open. If provided, searches for existing RQ by ID. Mutually exclusive with name and alias. 
- optionalkeyword-onlyalias: str | None = None- The alias of the RQ to open (run scope, creates unnamed storage). Mutually exclusive with id and name. 
- optionalkeyword-onlyname: str | None = None- The name of the RQ to open (global scope, persists across runs). Mutually exclusive with id and alias. 
- optionalkeyword-onlyforce_cloud: bool = False- If set to - Truethen the Apify cloud storage is always used. This way it is possible to combine local and cloud storage.
 - Returns RequestQueue
push_data
- Store an object or a list of objects to the default dataset of the current Actor run. - Parameters- data: dict | list[dict]- The data to push to the default dataset. 
- optionalcharged_event_name: str | None = None- If provided and if the Actor uses the pay-per-event pricing model, the method will attempt to charge for the event for each pushed item. 
 - Returns ChargeResult | None
reboot
- Internally reboot this Actor. - The system stops the current container and starts a new one, with the same run ID and default storages. - Parameters- optionalkeyword-onlyevent_listeners_timeout: timedelta | None = EVENT_LISTENERS_TIMEOUT- How long should the Actor wait for Actor event listeners to finish before exiting. 
- optionalkeyword-onlycustom_after_sleep: timedelta | None = None- How long to sleep for after the reboot, to wait for the container to be stopped. 
 - Returns None
set_status_message
- Set the status message for the current Actor run. - Parameters- status_message: str- The status message to set to the run. 
- optionalkeyword-onlyis_terminal: bool | None = None- Set this flag to True if this is the final status message of the Actor run. 
 - Returns ActorRun | None
set_value
- Set or delete a value in the default key-value store associated with the current Actor run. - Parameters- key: str- The key of the record which to set. 
- value: Any- The value of the record which to set, or None, if the record should be deleted. 
- optionalkeyword-onlycontent_type: str | None = None- The content type which should be set to the value. 
 - Returns None
start
- Run an Actor on the Apify platform. - Unlike - Actor.call, this method just starts the run without waiting for finish.- Parameters- actor_id: str- The ID of the Actor to be run. 
- optionalrun_input: Any = None- The input to pass to the Actor run. 
- optionalkeyword-onlytoken: str | None = None- The Apify API token to use for this request (defaults to the - APIFY_TOKENenvironment variable).
- optionalkeyword-onlycontent_type: str | None = None- The content type of the input. 
- optionalkeyword-onlybuild: str | None = None- Specifies the Actor build to run. It can be either a build tag or build number. By default, the run uses the build specified in the default run configuration for the Actor (typically latest). 
- optionalkeyword-onlymemory_mbytes: int | None = None- Memory limit for the run, in megabytes. By default, the run uses a memory limit specified in the default run configuration for the Actor. 
- optionalkeyword-onlytimeout: (timedelta | None) | Literal[RemainingTime] = None- Optional timeout for the run, in seconds. By default, the run uses timeout specified in the default run configuration for the Actor. Using - RemainingTimewill set timeout of the other Actor to the time remaining from this Actor timeout.
- optionalkeyword-onlywait_for_finish: int | None = None- The maximum number of seconds the server waits for the run to finish. By default, it is 0, the maximum value is 300. 
- optionalkeyword-onlywebhooks: list[Webhook] | None = None- Optional ad-hoc webhooks (https://docs.apify.com/webhooks/ad-hoc-webhooks) associated with the Actor run which can be used to receive a notification, e.g. when the Actor finished or failed. If you already have a webhook set up for the Actor or task, you do not have to add it again here. 
 - Returns ActorRun
status_message
- Parameters- value: str | None
 - Returns None
Properties
apify_client
Asynchronous Apify client for interacting with the Apify API.
exit_code
The exit code the Actor will use when exiting.
log
Logger configured for this Actor.
status_message
The final status message that the Actor will display upon termination.
The core class for building Actors on the Apify platform.
Actors are serverless programs running in the cloud that can perform anything from simple actions (such as filling out a web form or sending an email) to complex operations (such as crawling an entire website or removing duplicates from a large dataset). They are packaged as Docker containers which accept well-defined JSON input, perform an action, and optionally produce well-defined output.
References
Usage