|
| 1 | +--- |
| 2 | +id: overview |
| 3 | +title: Overview |
| 4 | +--- |
| 5 | + |
| 6 | +import Tabs from '@theme/Tabs'; |
| 7 | +import TabItem from '@theme/TabItem'; |
| 8 | +import CodeBlock from '@theme/CodeBlock'; |
| 9 | + |
| 10 | +import AuthAsyncExample from '!!raw-loader!./code/02_auth_async.py'; |
| 11 | +import AuthSyncExample from '!!raw-loader!./code/02_auth_sync.py'; |
| 12 | +import UsageAsyncExample from '!!raw-loader!./code/01_usage_async.py'; |
| 13 | +import UsageSyncExample from '!!raw-loader!./code/01_usage_sync.py'; |
| 14 | +import InputAsyncExample from '!!raw-loader!./code/03_input_async.py'; |
| 15 | +import InputSyncExample from '!!raw-loader!./code/03_input_sync.py'; |
| 16 | +import DatasetAsyncExample from '!!raw-loader!./code/03_dataset_async.py'; |
| 17 | +import DatasetSyncExample from '!!raw-loader!./code/03_dataset_sync.py'; |
| 18 | + |
| 19 | +## Introduction |
| 20 | + |
| 21 | +The [Apify client for Python](https://github.com/apify/apify-client-python) is the official library to access the [Apify REST API](/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. |
| 22 | + |
| 23 | +Key features: |
| 24 | + |
| 25 | +- Synchronous and asynchronous interfaces for flexible integration |
| 26 | +- Automatic retries for improved reliability |
| 27 | +- JSON encoding with UTF-8 for all requests and responses |
| 28 | +- Comprehensive API coverage for [Actors](/platform/actors), [datasets](/platform/storage/dataset), [key-value stores](/platform/storage/key-value-store), and more |
| 29 | + |
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +Before installing the Apify client, ensure your system meets the following requirements: |
| 33 | + |
| 34 | +- _An Apify account_ |
| 35 | +- _Python 3.10 or higher_: You can download Python from the [official website](https://www.python.org/downloads/). |
| 36 | +- _Python package manager_: While this guide uses [pip](https://pip.pypa.io/en/stable/), you can also use any package manager you want. |
| 37 | + |
| 38 | +To verify that Python and pip are installed, run the following commands: |
| 39 | + |
| 40 | +```sh |
| 41 | +python --version |
| 42 | +``` |
| 43 | + |
| 44 | +```sh |
| 45 | +pip --version |
| 46 | +``` |
| 47 | + |
| 48 | +If these commands return the respective versions, you're ready to continue. |
| 49 | + |
| 50 | +## Installation |
| 51 | + |
| 52 | +The Apify client is available as the [`apify-client`](https://pypi.org/project/apify-client/) package on PyPI. To install it, run: |
| 53 | + |
| 54 | +```sh |
| 55 | +pip install apify-client |
| 56 | +``` |
| 57 | + |
| 58 | +After installation, verify that the client is installed correctly by checking its version: |
| 59 | + |
| 60 | +```sh |
| 61 | +python -c 'import apify_client; print(apify_client.__version__)' |
| 62 | +``` |
| 63 | + |
| 64 | +## Authentication and initialization |
| 65 | + |
| 66 | +To use the client, you need an [API token](/platform/integrations/api#api-token). You can find your token under the [Integrations](https://console.apify.com/account/integrations) tab in Apify Console. Copy the token and initialize the client by providing it as a parameter to the `ApifyClient` constructor. |
| 67 | + |
| 68 | +<Tabs> |
| 69 | + <TabItem value="AsyncExample" label="Async client" default> |
| 70 | + <CodeBlock className="language-python"> |
| 71 | + {AuthAsyncExample} |
| 72 | + </CodeBlock> |
| 73 | + </TabItem> |
| 74 | + <TabItem value="SyncExample" label="Sync client"> |
| 75 | + <CodeBlock className="language-python"> |
| 76 | + {AuthSyncExample} |
| 77 | + </CodeBlock> |
| 78 | + </TabItem> |
| 79 | +</Tabs> |
| 80 | + |
| 81 | +:::warning Secure access |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +::: |
| 86 | + |
| 87 | +## Quick start |
| 88 | + |
| 89 | +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. |
| 90 | + |
| 91 | +### Running your first Actor |
| 92 | + |
| 93 | +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`](/reference/class/ActorClient) to run the Actor and wait for it to complete. You can run both your own Actors and [Actors from Apify Store](https://docs.apify.com/platform/actors/running/actors-in-store). |
| 94 | + |
| 95 | +<Tabs> |
| 96 | + <TabItem value="AsyncExample" label="Async client" default> |
| 97 | + <CodeBlock className="language-python"> |
| 98 | + {UsageAsyncExample} |
| 99 | + </CodeBlock> |
| 100 | + </TabItem> |
| 101 | + <TabItem value="SyncExample" label="Sync client"> |
| 102 | + <CodeBlock className="language-python"> |
| 103 | + {UsageSyncExample} |
| 104 | + </CodeBlock> |
| 105 | + </TabItem> |
| 106 | +</Tabs> |
| 107 | + |
| 108 | +### Providing input to Actor |
| 109 | + |
| 110 | +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`](/reference/class/ActorClient#call) method. Actors respect the input schema defined in the Actor's [input schema](https://docs.apify.com/platform/actors/development/actor-definition/input-schema). |
| 111 | + |
| 112 | +<Tabs> |
| 113 | + <TabItem value="AsyncExample" label="Async client" default> |
| 114 | + <CodeBlock className="language-python"> |
| 115 | + {InputAsyncExample} |
| 116 | + </CodeBlock> |
| 117 | + </TabItem> |
| 118 | + <TabItem value="SyncExample" label="Sync client"> |
| 119 | + <CodeBlock className="language-python"> |
| 120 | + {InputSyncExample} |
| 121 | + </CodeBlock> |
| 122 | + </TabItem> |
| 123 | +</Tabs> |
| 124 | + |
| 125 | +### Getting results from the dataset |
| 126 | + |
| 127 | +To get the results from the dataset, you can use the [`DatasetClient`](/reference/class/DatasetClient) ([`ApifyClient.dataset`](/reference/class/ApifyClient#dataset)) and [`DatasetClient.list_items`](/reference/class/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`). |
| 128 | + |
| 129 | +<Tabs> |
| 130 | + <TabItem value="AsyncExample" label="Async client" default> |
| 131 | + <CodeBlock className="language-python"> |
| 132 | + {DatasetAsyncExample} |
| 133 | + </CodeBlock> |
| 134 | + </TabItem> |
| 135 | + <TabItem value="SyncExample" label="Sync client"> |
| 136 | + <CodeBlock className="language-python"> |
| 137 | + {DatasetSyncExample} |
| 138 | + </CodeBlock> |
| 139 | + </TabItem> |
| 140 | +</Tabs> |
| 141 | + |
| 142 | +:::note Dataset access |
| 143 | + |
| 144 | +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](https://docs.apify.com/platform/actors/running/runs-and-builds#runs). |
| 145 | + |
| 146 | +::: |
| 147 | + |
| 148 | +## Next steps |
| 149 | + |
| 150 | +Now that you're familiar with the basics, explore more advanced features: |
| 151 | + |
| 152 | +- [Asyncio support](/concepts/asyncio-support) - Learn about asynchronous programming with the client |
| 153 | +- Common use-case examples like: |
| 154 | + - [Passing an input to Actor](/api/client/python/docs/examples/passing-input-to-actor) |
| 155 | + - [Retrieve Actor data](/api/client/python/docs/examples/retrieve-actor-data) |
| 156 | +- [API Reference](/api/client/python/reference) - Browse the complete API documentation |
0 commit comments