Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Session.vim

# Docs
docs/changelog.md
website/versioned_docs/*/changelog.md

# Website build artifacts, node dependencies
website/build
Expand Down
3 changes: 2 additions & 1 deletion typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ extend-exclude = [
"*.lock",
"*.min.js",
"*.min.css",
"CHANGELOG.md",
"**/CHANGELOG.md",
"**/changelog.md",
]

[default.extend-words]
Expand Down
41 changes: 41 additions & 0 deletions website/versioned_docs/version-0.6/01_introduction/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
id: introduction
title: Overview
sidebar_label: Overview
slug: /
description: "The official Python library to access the Apify API, with automatic retries and convenience functions."
---

The [Apify client for Python](https://github.com/apify/apify-client-python) (`apify_client`) is the official library to access the [Apify REST API](https://docs.apify.com/api/v2) from your Python applications. It provides useful features like automatic retries and convenience functions that improve the experience of using the Apify API.

## Prerequisites

`apify-client` requires Python 3.7 or higher. Python is available for download on the [official website](https://www.python.org/downloads/). Check your current Python version by running:

```bash
python --version
```

## Installation

The Apify client is available as the [`apify-client`](https://pypi.org/project/apify-client/) package on PyPI.

```bash
pip install apify-client
```

## Quick example

```python
from apify_client import ApifyClient

apify_client = ApifyClient('MY-APIFY-TOKEN')

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

# Fetch results from the actor's default dataset
dataset_items = apify_client.dataset(actor_call['defaultDatasetId']).list_items().items
```

> You can find your API token in the [Integrations section](https://console.apify.com/account/integrations) of Apify Console. See the [Quick start guide](./quick-start) for more details.
51 changes: 51 additions & 0 deletions website/versioned_docs/version-0.6/01_introduction/quick-start.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
id: quick-start
title: Quick start
sidebar_label: Quick start
description: "Get started with the Apify client for Python."
---

## Installation

```bash
pip install apify-client
```

## Authentication

To use the Apify API, you need an API token. You can find your token in the [Integrations section](https://console.apify.com/account/integrations) of Apify Console.

```python
from apify_client import ApifyClient

apify_client = ApifyClient('MY-APIFY-TOKEN')
```

## Running an Actor

The simplest way to run an Actor is to use the `call` method, which starts the Actor and waits for it to finish.

```python
from apify_client import ApifyClient

apify_client = ApifyClient('MY-APIFY-TOKEN')

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

# Fetch results from the actor's default dataset
dataset_items = apify_client.dataset(actor_call['defaultDatasetId']).list_items().items
```

## Working with datasets

```python
# Get (or create, if it doesn't exist) a dataset with the name of my-dataset
my_dataset = apify_client.datasets().get_or_create(name='my-dataset')

# Append items to the end of the dataset
apify_client.dataset(my_dataset['id']).push_items([{'foo': 1}, {'bar': 2}])

# List items in the dataset
items = apify_client.dataset(my_dataset['id']).list_items().items
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: Single and collection clients
---

The `ApifyClient` interface follows a generic pattern that is applicable to all of its components. By calling individual methods of `ApifyClient`, specific clients which target individual API resources are created. There are two types of those clients. A client for management of a single resource and a client for a collection of resources.

### Collection clients

```python
from apify_client import ApifyClient
apify_client = ApifyClient('MY-APIFY-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
```

```python
# Collection clients do not require a parameter
dataset_collection_client = apify_client.datasets()
# Get (or create, if it doesn't exist) a dataset with the name of my-dataset
my_dataset = dataset_collection_client.get_or_create(name='my-dataset')
```

### Resource clients

```python
# Resource clients accept an ID of the resource
actor_client = apify_client.actor('john-doe/my-actor')
# Fetch the john-doe/my-actor object from the API
my_actor = actor_client.get()
# Start the run of john-doe/my-actor and return the Run object
my_actor_run = actor_client.start()
```

```python
# Resource clients accept an ID of the resource
dataset_client = apify_client.dataset('john-doe/my-dataset')
# Append items to the end of john-doe/my-dataset
dataset_client.push_items([{'foo': 1}, {'bar': 2}])
```

> The ID of the resource can be either the `id` of the said resource, or a combination of your `username/resource-name`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Nested clients
---

Sometimes clients return other clients. That's to simplify working with nested collections, such as runs of a given actor.

```python
from apify_client import ApifyClient
apify_client = ApifyClient('MY-APIFY-TOKEN')

actor_client = apify_client.actor('john-doe/my-actor')
runs_client = actor_client.runs()
# List the last 10 runs of the john-doe/hello-world actor
actor_runs = runs_client.list(limit=10, desc=True).items

# Select the last run of the john-doe/hello-world actor that finished
# with a SUCCEEDED status
last_succeeded_run_client = actor_client.last_run(status='SUCCEEDED')
# Fetch items from the run's dataset
dataset_items = last_succeeded_run_client.dataset().list_items().items
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Error handling
---

Based on the endpoint, the client automatically extracts the relevant data and returns it in the expected format. Date strings are automatically converted to `datetime.datetime` objects.

For exceptions, we throw an `ApifyApiError`, which wraps the plain JSON errors returned by API and enriches them with other context for easier debugging.

```python
from apify_client import ApifyClient

apify_client = ApifyClient('MY-APIFY-TOKEN')

try:
actor = apify_client.actor('non-existent-actor').get()
except Exception as e:
print(f'Error: {e}')
```
15 changes: 15 additions & 0 deletions website/versioned_docs/version-0.6/02_concepts/04_retries.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Retries with exponential backoff
---

Network communication sometimes fails. The client will automatically retry requests that failed due to a network error, an internal error of the Apify API (HTTP 500+) or rate limit error (HTTP 429). By default, it will retry up to 8 times. First retry will be attempted after ~500ms, second after ~1000ms and so on. You can configure those parameters using the `max_retries` and `min_delay_between_retries_millis` options of the `ApifyClient` constructor.

```python
from apify_client import ApifyClient

apify_client = ApifyClient(
'MY-APIFY-TOKEN',
max_retries=8,
min_delay_between_retries_millis=500,
)
```
18 changes: 18 additions & 0 deletions website/versioned_docs/version-0.6/02_concepts/05_pagination.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Pagination
---

Most methods named `list` or `list_something` return a `ListPage` object, containing properties `items`, `total`, `offset`, `count` and `limit`. There are some exceptions though, like `list_keys` or `list_head` which paginate differently. The results you're looking for are always stored under `items` and you can use the `limit` property to get only a subset of results. Other properties can be available depending on the method.

```python
from apify_client import ApifyClient

apify_client = ApifyClient('MY-APIFY-TOKEN')

# List first 10 actors
actors = apify_client.actors().list(limit=10)
print(f'Total actors: {actors.total}')
print(f'Returned: {actors.count}')
for actor in actors.items:
print(actor['name'])
```
10 changes: 10 additions & 0 deletions website/versioned_docs/version-0.6/api-packages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"entryPoints": { "index": { "label": "Index", "path": "src/apify_client" } },
"packageRoot": "..",
"packagePath": ".",
"packageSlug": ".",
"packageName": "apify-client",
"packageVersion": "0.6.0"
}
]
Loading