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