Getting started
This guide will walk you through how to use the Apify Client for Python to run Actors on the Apify platform, provide input to them, and retrieve results from their datasets. You'll learn the basics of running serverless programs (we're calling them Actors) and managing their output efficiently.
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 username 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.
- Async client
- Sync client
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}')
from apify_client import ApifyClient
# You can find your API token at https://console.apify.com/settings/integrations.
TOKEN = 'MY-APIFY-TOKEN'
def main() -> None:
apify_client = ApifyClient(TOKEN)
# Start an Actor and wait for it to finish.
actor_client = apify_client.actor('john-doe/my-cool-actor')
call_result = 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 = 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.
- Async client
- Sync client
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)
from apify_client import ApifyClient
TOKEN = 'MY-APIFY-TOKEN'
def main() -> None:
apify_client = ApifyClient(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 = 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
).
- Async client
- Sync client
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)
from apify_client import ApifyClient
TOKEN = 'MY-APIFY-TOKEN'
def main() -> None:
apify_client = ApifyClient(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 = actor_client.call(run_input=run_input)
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.