|
| 1 | +--- |
| 2 | +title: Getting started with Apify API |
| 3 | +sidebar_label: Getting started |
| 4 | +displayed_sidebar: api |
| 5 | +slug: /getting-started |
| 6 | +--- |
| 7 | + |
| 8 | +The Apify API provides programmatic access to the [Apify platform](https://docs.apify.com). The API is organized around [RESTful](https://en.wikipedia.org/wiki/Representational_state_transfer) HTTP endpoints. |
| 9 | + |
| 10 | +```mermaid |
| 11 | +graph LR |
| 12 | + A[HTTP Client] <--> B[Apify API] |
| 13 | + B --> C[Actor Run] |
| 14 | + C --> D1[Dataset] |
| 15 | + C --> D2[Key-Value Store] |
| 16 | +``` |
| 17 | + |
| 18 | +The diagram illustrates the basic workflow when using the Apify API: |
| 19 | + |
| 20 | +1. Your application communicates with the Apify API by sending requests to run Actors and receiving results back. |
| 21 | +1. When you request to run an Actor, the Apify API creates and manages an Actor run instance on the platform. |
| 22 | +1. The Actor processes data and stores results in Apify's storage systems: |
| 23 | + * **Dataset**: Structured storage optimized for tabular or list-type data, ideal for scraped items or processed results. |
| 24 | + * **Key-Value Store**: Flexible storage for various data types (including images, JSON, HTML, and text), perfect for configuration settings and non-tabular outputs. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +Before you can start using the API, check if you have all the necessary prerequisites: |
| 29 | + |
| 30 | +* An Apify account with an API token. |
| 31 | +* A tool to make HTTP requests (cURL, Postman, or your preferred programming language). |
| 32 | + |
| 33 | +## Authentication |
| 34 | + |
| 35 | +You must authenticate all API requests presented on this page. You can authenticate using your API token: |
| 36 | + |
| 37 | +`Authorization: Bearer YOUR_API_TOKEN` |
| 38 | + |
| 39 | +You can find your API token in the Apify Console under **[Settings > Integrations](https://console.apify.com/settings/integrations)**. |
| 40 | + |
| 41 | +## Basic workflow |
| 42 | + |
| 43 | +A most common workflow involving Apify API consists of the following steps: |
| 44 | + |
| 45 | +1. Running an Actor. |
| 46 | +1. Monitoring the status of a run. |
| 47 | +1. Retrieving the results. |
| 48 | +1. Retrieving log for troubleshooting purposes. - optional |
| 49 | + |
| 50 | +### 1. Run an Actor |
| 51 | + |
| 52 | +#### Asynchronously |
| 53 | + |
| 54 | +```http title="Endpoint" |
| 55 | +POST https://api.apify.com/v2/acts/{actorId}/runs |
| 56 | +``` |
| 57 | + |
| 58 | +```json title="Request body" |
| 59 | +TODO |
| 60 | +``` |
| 61 | + |
| 62 | +```json title="Response" |
| 63 | +TODO |
| 64 | +``` |
| 65 | + |
| 66 | +Expected response codes: |
| 67 | + |
| 68 | +* 201 |
| 69 | + |
| 70 | +#### Synchronously |
| 71 | + |
| 72 | +For shorter runs where you need immediate results: |
| 73 | + |
| 74 | +```http title="Endpoint" |
| 75 | +POST https://api.apify.com/v2/acts/{actorId}/run-sync |
| 76 | +``` |
| 77 | + |
| 78 | +The request body is the same as for asynchronous runs, but the response will include the complete results once the Actor finishes. |
| 79 | + |
| 80 | +Expected response codes: |
| 81 | + |
| 82 | +* 201 |
| 83 | +* 400 |
| 84 | +* 408 |
| 85 | + |
| 86 | +### 2. Monitor run status |
| 87 | + |
| 88 | +```http title="Endpoint" |
| 89 | +GET https://api.apify.com/v2/actor-runs/:runId |
| 90 | +``` |
| 91 | + |
| 92 | +``` json title="Response" |
| 93 | +TODO |
| 94 | +``` |
| 95 | + |
| 96 | +Expected response code: |
| 97 | + |
| 98 | +* 200 |
| 99 | + |
| 100 | +### 3. Retrieve results |
| 101 | + |
| 102 | +#### From a Dataset |
| 103 | + |
| 104 | +Most Actors store their results in a dataset: |
| 105 | + |
| 106 | +```http title="Endpoint" |
| 107 | +GET https://api.apify.com/v2/datasets/{datasetId}/items |
| 108 | +``` |
| 109 | + |
| 110 | +Optional query parameters: |
| 111 | + |
| 112 | +* `format=json` (default), other possible formats are: |
| 113 | + * jsonl |
| 114 | + * xml |
| 115 | + * html |
| 116 | + * csv |
| 117 | + * xlsx |
| 118 | + * rss |
| 119 | +* `limit=100` (number of items to retrieve) |
| 120 | +* `offset=0` (pagination offset) |
| 121 | + |
| 122 | +Expected code responses: |
| 123 | + |
| 124 | +* 200 |
| 125 | + |
| 126 | +#### From a Key-value store |
| 127 | + |
| 128 | +```http title="Endpoint" |
| 129 | +GET https://api.apify.com/v2/key-value-stores/{storeId}/records/{recordKey} |
| 130 | +``` |
| 131 | + |
| 132 | +Expected response codes: |
| 133 | + |
| 134 | +* 200 |
| 135 | +* 302 |
| 136 | + |
| 137 | +### Get log |
| 138 | + |
| 139 | +You can get a log for a specific run or build of an Actor. |
| 140 | + |
| 141 | +```http title="Endpoint" |
| 142 | +GET https://api.apify.com/v2/logs/:buildOrRunId |
| 143 | +``` |
| 144 | + |
| 145 | +Expected code responses: |
| 146 | + |
| 147 | +* 200 |
| 148 | + |
| 149 | +### Additional operations |
| 150 | + |
| 151 | +#### Store data in Key-value store |
| 152 | + |
| 153 | +To store your own data in a Key-value store: |
| 154 | + |
| 155 | +```http title="Endpoint" |
| 156 | +PUT https://api.apify.com/v2/key-value-stores/{storeId}/records/{recordKey} |
| 157 | +``` |
| 158 | + |
| 159 | +Include your data in the request body and set the appropriate `Content-Type` header. |
| 160 | + |
| 161 | +Expected response codes: |
| 162 | + |
| 163 | +* 201 |
| 164 | + |
| 165 | +#### Verify your account |
| 166 | + |
| 167 | +To check your API credentials or account details: |
| 168 | + |
| 169 | +```http title="Endpoint" |
| 170 | +GET https://api.apify.com/v2/users/me |
| 171 | +``` |
| 172 | + |
| 173 | +Expected response codes: |
| 174 | + |
| 175 | +* 200 |
| 176 | + |
| 177 | +## Next steps |
| 178 | + |
| 179 | +* Explore more advanced API endpoints in our full [API reference](/api/v2). |
| 180 | +* Learn about webhooks to get notified when your runs finish. |
| 181 | +* Check out Apify client libraries for the following programming languages: |
| 182 | + * [JavaScript](/api/client/js/) |
| 183 | + * [Python](/api/client/python) |
0 commit comments