Skip to main content

Overview

Introduction

The Apify client for Python is the official library to access the Apify REST API from your Python applications. It provides useful features like automatic retries and convenience functions that improve the experience of using the Apify API.

Key features:

  • Synchronous and asynchronous interfaces for flexible integration
  • Automatic retries for improved reliability
  • JSON encoding with UTF-8 for all requests and responses
  • Comprehensive API coverage for Actors, datasets, key-value stores, and more

Prerequisites

Before installing the Apify client, ensure your system meets the following requirements:

  • An Apify account
  • Python 3.10 or higher: You can download Python from the official website.
  • Python package manager: While this guide uses pip, you can also use any package manager you want.

To verify that Python and pip are installed, run the following commands:

python --version
pip --version

If these commands return the respective versions, you're ready to continue.

Installation

The Apify client is available as the apify-client package on PyPI. To install it, run:

pip install apify-client

After installation, verify that the client is installed correctly by checking its version:

python -c 'import apify_client; print(apify_client.__version__)'

Authentication and initialization

To use the client, you need an API token. You can find your token under the Integrations tab in Apify Console. Copy the token and initialize the client by providing it as a parameter to the ApifyClient constructor.

from apify_client import ApifyClientAsync

TOKEN = 'MY-APIFY-TOKEN'


async def main() -> None:
# Client initialization with the API token.
apify_client = ApifyClientAsync(TOKEN)
Secure access

The API token is used to authorize your requests to the Apify API. You can be charged for the usage of the underlying services, so do not share your API token with untrusted parties or expose it on the client side of your applications.

Quick start

Now that you have the client set up, let's explore how to run Actors on the Apify platform, provide input to them, and retrieve their results.

Running your first Actor

To start an Actor, you need its ID (e.g., john-doe/my-cool-actor) and an API token. The Actor's ID is a combination of the Actor name and the Actor owner's username. Use the ActorClient to run the Actor and wait for it to complete. You can run both your own Actors and Actors from Apify Store.

from apify_client import ApifyClientAsync

# You can find your API token at https://console.apify.com/settings/integrations.
TOKEN = 'MY-APIFY-TOKEN'


async def main() -> None:
apify_client = ApifyClientAsync(TOKEN)

# Start an Actor and wait for it to finish.
actor_client = apify_client.actor('john-doe/my-cool-actor')
call_result = await actor_client.call()

if call_result is None:
print('Actor run failed.')
return

# Fetch results from the Actor run's default dataset.
dataset_client = apify_client.dataset(call_result['defaultDatasetId'])
list_items_result = await dataset_client.list_items()
print(f'Dataset: {list_items_result}')

Providing input to Actor

Actors often require input, such as URLs to scrape, search terms, or other configuration data. You can pass input as a JSON object when starting the Actor using the ActorClient.call method. Actors respect the input schema defined in the Actor's input schema.

from apify_client import ApifyClientAsync

TOKEN = 'MY-APIFY-TOKEN'


async def main() -> None:
apify_client = ApifyClientAsync(TOKEN)
actor_client = apify_client.actor('username/actor-name')

# Define the input for the Actor.
run_input = {
'some': 'input',
}

# Start an Actor and waits for it to finish.
call_result = await actor_client.call(run_input=run_input)

Getting results from the dataset

To get the results from the dataset, you can use the DatasetClient (ApifyClient.dataset) and DatasetClient.list_items method. You need to pass the dataset ID to define which dataset you want to access. You can get the dataset ID from the Actor's run dictionary (represented by defaultDatasetId).

from apify_client import ApifyClientAsync

TOKEN = 'MY-APIFY-TOKEN'


async def main() -> None:
apify_client = ApifyClientAsync(TOKEN)
dataset_client = apify_client.dataset('dataset-id')

# Lists items from the Actor's dataset.
dataset_items = (await dataset_client.list_items()).items
Dataset access

Running an Actor might take time, depending on the Actor's complexity and the amount of data it processes. If you want only to get data and have an immediate response, you should access the existing dataset of the finished Actor run.

Next steps

Now that you're familiar with the basics, explore more advanced features: