Single and collection clients
The Apify client interface is designed to be consistent and intuitive across all of its components. When you call specific methods on the main client, you create specialized clients to manage individual API resources. There are two main types of clients:
ActorClient
- Manages a single resource.ActorCollectionClient
- Manages a collection of resources.
- Async client
- Sync client
from apify_client import ApifyClientAsync
TOKEN = 'MY-APIFY-TOKEN'
async def main() -> None:
apify_client = ApifyClientAsync(TOKEN)
# Collection clients do not require a parameter
actor_collection_client = apify_client.actors()
# Create an Actor with the name: my-actor
my_actor = await actor_collection_client.create(name='my-actor')
# List all of your Actors
actor_list = (await actor_collection_client.list()).items
from apify_client import ApifyClient
TOKEN = 'MY-APIFY-TOKEN'
def main() -> None:
apify_client = ApifyClient(TOKEN)
# Collection clients do not require a parameter
actor_collection_client = apify_client.actors()
# Create an Actor with the name: my-actor
my_actor = actor_collection_client.create(name='my-actor')
# List all of your Actors
actor_list = actor_collection_client.list().items
The resource ID can be the resource's id
or a combination of username/resource-name
.
- Async client
- Sync client
from apify_client import ApifyClientAsync
TOKEN = 'MY-APIFY-TOKEN'
async def main() -> None:
apify_client = ApifyClientAsync(TOKEN)
# Resource clients accept an ID of the resource
actor_client = apify_client.actor('username/actor-name')
# Fetch the 'username/actor-name' object from the API
my_actor = await actor_client.get()
# Start the run of 'username/actor-name' and return the Run object
my_actor_run = await actor_client.start()
from apify_client import ApifyClient
TOKEN = 'MY-APIFY-TOKEN'
def main() -> None:
apify_client = ApifyClient(TOKEN)
# Resource clients accept an ID of the resource
actor_client = apify_client.actor('username/actor-name')
# Fetch the 'username/actor-name' object from the API
my_actor = actor_client.get()
# Start the run of 'username/actor-name' and return the Run object
my_actor_run = actor_client.start()
By utilizing the appropriate collection or resource client, you can simplify how you interact with the Apify API.