Apify Client

raw JSON →
2.5.0 verified Thu May 14 auth: no python

The Apify Client for Python allows users to interact with the Apify platform, managing actors, datasets, key-value stores, and more. It provides both synchronous and asynchronous interfaces for web scraping and automation tasks. The current stable version is 2.5.0, with regular updates.

pip install apify-client
error TypeError: 'NoneType' object is not callable
cause This error occurs when importing 'apify_client' inside a Docker container due to compatibility issues with Python versions.
fix
Ensure that the Docker container is using Python 3.10 or higher, as 'apify-client' requires Python 3.10 or higher. ([docs.apify.com](https://docs.apify.com/api/client/python/?utm_source=openai))
error ApifyApiError: Actor 'non-existent-actor' does not exist
cause This error occurs when attempting to access an Actor that does not exist on the Apify platform.
fix
Verify that the Actor ID is correct and that the Actor exists on the Apify platform. ([docs.apify.com](https://docs.apify.com/api/client/python/docs/concepts/error-handling?utm_source=openai))
error ApifyApiError: Request failed with status code 429
cause This error occurs when the Apify API rate limit is exceeded.
fix
Implement exponential backoff and retry logic to handle rate limit errors. ([docs.apify.com](https://docs.apify.com/api/client/python/docs/concepts/retries?utm_source=openai))
error ModuleNotFoundError: No module named 'apify-client'
cause The 'apify-client' library is not installed in your Python environment.
fix
Run pip install apify-client in your terminal to install the library.
error ApifyApiError: Authentication token was not provided.
cause The Apify client was initialized without an API token, or the token provided is invalid or expired, which is required for most API operations.
fix
Initialize the ApifyClient with a valid API token: apify_client = ApifyClient('YOUR_API_TOKEN'). You can find your API token in the Apify Console under 'Integrations'.
breaking Breaking Change: V1 to V2 API Client Initialization. In `v2.x`, the `ApifyClient` constructor directly accepts `token='YOUR_TOKEN'`, unifying the previous `ApifyClientSync` and `ApifyClientAsync` classes from `v1.x`. Users migrating from `v1.x` must adapt to this single client and direct token parameter.
fix Replace `ApifyClientSync(token='...')` or `ApifyClientAsync(token='...')` with `ApifyClient(token='...')`.
breaking Breaking Change: V1 to V2 Resource Access Patterns. Accessing Apify resources (Actors, Datasets, Key-Value Stores, etc.) moved from methods like `client.actors().get_or_create(...)` in `v1.x` to a more direct chainable pattern like `client.actor(...).get_or_create()` in `v2.x`.
fix Update resource access calls. For example, `client.actors().get('id')` becomes `client.actor('id').get()`.
gotcha API Token Requirement: Most Apify API operations require an API token for authentication. If the `APIFY_TOKEN` environment variable is not set or the `token` parameter is not explicitly passed to the `ApifyClient` constructor, operations will typically result in authentication errors (e.g., 401 Unauthorized).
fix Set the `APIFY_TOKEN` environment variable or pass `token='YOUR_APIFY_TOKEN'` directly to `ApifyClient()`.
gotcha Pagination for Large Datasets: When retrieving a large number of items from datasets or key-value stores, fetching all items at once can lead to memory exhaustion. Always use iterative methods or pagination parameters.
fix Use `list_items()` with `limit` and `offset` parameters or, preferably, `iterate_items()` for efficient, paginated access to large datasets.
python os / libc status wheel install import disk mem side effects
3.10 alpine (musl) wheel - 0.27s 28.5M 9.5M clean
3.10 alpine (musl) - - 0.30s 28.5M 9.5M -
3.10 slim (glibc) wheel 2.0s 0.21s 28M 9.5M clean
3.10 slim (glibc) - - 0.23s 28M 9.5M -
3.11 alpine (musl) wheel - 0.40s 30.6M 10.9M clean
3.11 alpine (musl) - - 0.45s 30.6M 10.9M -
3.11 slim (glibc) wheel 2.0s 0.37s 31M 10.9M clean
3.11 slim (glibc) - - 0.34s 31M 10.9M -
3.12 alpine (musl) wheel - 0.58s 22.4M 12.3M clean
3.12 alpine (musl) - - 0.65s 22.4M 12.3M -
3.12 slim (glibc) wheel 1.9s 0.59s 22M 12.3M clean
3.12 slim (glibc) - - 0.66s 22M 12.3M -
3.13 alpine (musl) wheel - 0.56s 22.1M 12.8M clean
3.13 alpine (musl) - - 0.69s 22.0M 12.8M -
3.13 slim (glibc) wheel 1.9s 0.56s 22M 12.8M clean
3.13 slim (glibc) - - 0.66s 22M 12.8M -
3.9 alpine (musl) wheel - 0.26s 22.9M 10.3M clean
3.9 alpine (musl) - - 0.31s 22.9M 10.3M -
3.9 slim (glibc) wheel 2.8s 0.26s 23M 10.3M clean
3.9 slim (glibc) - - 0.31s 23M 10.3M -

This quickstart demonstrates how to initialize the Apify client, run a 'Hello World' actor, and retrieve its results from the default dataset. It emphasizes secure API token handling via environment variables.

import os
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token.
# It's recommended to store your token in an environment variable named APIFY_TOKEN.
# Alternatively, pass it directly: apify_client = ApifyClient(token='YOUR_APIFY_TOKEN')
apify_client = ApifyClient(token=os.environ.get('APIFY_TOKEN', ''))

# Ensure an API token is provided
if not apify_client.token:
    print("Warning: APIFY_TOKEN not found. Most operations will fail without authentication.")
    # In a real application, you might raise an error or exit here.
    # For this quickstart, we'll continue but expect failures for authenticated calls.

# Run a specific Actor (replace 'apify/hello-world' with your Actor ID)
try:
    actor_run = apify_client.actor('apify/hello-world').call()

    # Fetch results from the Actor's default dataset
    print(f'Actor run data from dataset: {apify_client.dataset(actor_run["defaultDatasetId"]).list_items().items}')

    # Or to fetch items one by one:
    # for item in apify_client.dataset(actor_run["defaultDatasetId"]).iterate_items():
    #     print(item)
except Exception as e:
    print(f"An error occurred while running the Actor or fetching data: {e}")
    print("Please ensure your APIFY_TOKEN is valid and the 'apify/hello-world' actor is accessible.")