Skip to content

Commit e96c5be

Browse files
authored
docs: Add overview page (#783)
Part of the fix for apify/apify-docs#1869
1 parent 5f4f981 commit e96c5be

File tree

3 files changed

+315
-259
lines changed

3 files changed

+315
-259
lines changed

docs/getting-started.md

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
---
2+
sidebar_label: 'Getting started'
3+
title: 'Getting started with JavaScript client'
4+
---
5+
6+
import Tabs from '@theme/Tabs';
7+
import TabItem from '@theme/TabItem';
8+
9+
The Apify API client is the official library to access the [Apify REST API](https://docs.apify.com/api/v2) from your JavaScript/TypeScript applications. It runs both in Node.js and browser and provides useful features like automatic retries and convenience functions that improve the experience of using the Apify API. All requests and responses (including errors) are encoded in JSON format with UTF-8 encoding.
10+
11+
## Pre-requisites
12+
13+
`apify-client` requires Node.js version 16 or higher. Node.js is available for download on the [official website](https://nodejs.org/). Check for your current Node.js version by running:
14+
15+
```bash
16+
node -v
17+
```
18+
19+
## Installation
20+
21+
You can install the client via [NPM](https://www.npmjs.com/) or any other package manager of your choice.
22+
23+
<Tabs groupId="main">
24+
<TabItem value="npm" label="NPM">
25+
26+
```bash
27+
npm i apify-client
28+
```
29+
30+
</TabItem>
31+
<TabItem value="yarn" label="Yarn">
32+
33+
```bash
34+
yarn add apify-client
35+
```
36+
37+
</TabItem>
38+
<TabItem value="pnpm" label="PNPM">
39+
40+
```bash
41+
pnpm add apify-client
42+
```
43+
44+
</TabItem>
45+
<TabItem value="bun" label="Bun">
46+
47+
```bash
48+
bun add apify-client
49+
```
50+
51+
</TabItem>
52+
</Tabs>
53+
54+
## Authentication and Initialization
55+
56+
To use the client, you need an [API token](https://docs.apify.com/platform/integrations/api#api-token). You can find your token under [Integrations](https://console.apify.com/account/integrations) tab in Apify Console. Copy the token and initialize the client by providing the token (`MY-APIFY-TOKEN`) as a parameter to the `ApifyClient` constructor.
57+
58+
```js
59+
// import Apify client
60+
import { ApifyClient } from 'apify-client';
61+
62+
// Client initialization with the API token
63+
const client = new ApifyClient({
64+
token: 'MY-APIFY-TOKEN',
65+
});
66+
```
67+
68+
:::warning Secure access
69+
70+
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
71+
72+
:::
73+
74+
## Quick start
75+
76+
One of the most common use cases is starting [Actors](https://docs.apify.com/platform/actors) (serverless programs running in the [Apify platform](https://docs.apify.com/platform)) and getting results from their [datasets](https://docs.apify.com/platform/storage/dataset) after they finish the job (either scraping, automation processes or data processing).
77+
78+
```js
79+
import { ApifyClient } from 'apify-client';
80+
81+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
82+
83+
// Starts an Actor and waits for it to finish
84+
const { defaultDatasetId } = await client.actor('username/actor-name').call();
85+
86+
// Lists items from the Actor's dataset
87+
const { items } = await client.dataset(defaultDatasetId).listItems();
88+
```
89+
90+
### Running Actors
91+
92+
To start an Actor, call the [`client.actor()`](/reference/class/ActorClient) method with the Actor's ID (e.g. `john-doe/my-cool-actor`). The Actor's ID is a combination of the Actor name and the Actor owner's username. You can run both your own Actors and Actors from Apify Store.
93+
94+
#### Passing input to the Actor
95+
96+
To define the Actor's input, call the [`call()`](/reference/class/ActorClient#call) method with a JSON object that matches the Actor's [input schema](https://docs.apify.com/platform/actors/development/actor-definition/input-schema). The input can include URLs to scrape, search terms, or other configuration data.
97+
98+
```js
99+
import { ApifyClient } from 'apify-client';
100+
101+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
102+
103+
// Runs an Actor with an input and waits for it to finish.
104+
const { defaultDatasetId } = await client.actor('username/actor-name').call({
105+
some: 'input',
106+
});
107+
```
108+
109+
### Getting results from the dataset
110+
111+
To get the results from the dataset, call the [`client.dataset()`](/reference/class/DatasetClient) method with the dataset ID, then call [`listItems()`](/reference/class/DatasetClient#listItems) to retrieve the data. You can get the dataset ID from the Actor's run object (represented by `defaultDatasetId`).
112+
113+
```js
114+
import { ApifyClient } from 'apify-client';
115+
116+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
117+
118+
// Lists items from the Actor's dataset.
119+
const { items } = await client.dataset('dataset-id').listItems();
120+
```
121+
122+
:::note Dataset access
123+
124+
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).
125+
126+
:::
127+
128+
## Usage concepts
129+
130+
The `ApifyClient` interface follows a generic pattern that applies to all of its components. By calling individual methods of `ApifyClient`, specific clients that target individual API resources are created. There are two types of those clients:
131+
132+
- [`ActorClient`](/reference/class/ActorClient): a client for the management of a single resource
133+
- [`ActorCollectionClient`](/reference/class/ActorCollectionClient): a client for the collection of resources
134+
135+
```js
136+
import { ApifyClient } from 'apify-client';
137+
138+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
139+
140+
// Collection clients do not require a parameter.
141+
const actorCollectionClient = client.actors();
142+
// Creates an actor with the name: my-actor.
143+
const myActor = await actorCollectionClient.create({ name: 'my-actor-name' });
144+
// List all your used Actors (both own and from Apify Store)
145+
const { items } = await actorCollectionClient.list();
146+
```
147+
148+
:::note Resource identification
149+
150+
The resource ID can be either the `id` of the said resource, or a combination of your `username/resource-name`.
151+
152+
:::
153+
154+
```js
155+
import { ApifyClient } from 'apify-client';
156+
157+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
158+
159+
// Resource clients accept an ID of the resource.
160+
const actorClient = client.actor('username/actor-name');
161+
// Fetches the john-doe/my-actor object from the API.
162+
const myActor = await actorClient.get();
163+
// Starts the run of john-doe/my-actor and returns the Run object.
164+
const myActorRun = await actorClient.start();
165+
```
166+
167+
### Nested clients
168+
169+
Sometimes clients return other clients. That's to simplify working with nested collections, such as runs of a given Actor.
170+
171+
```js
172+
import { ApifyClient } from 'apify-client';
173+
174+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
175+
176+
const actorClient = client.actor('username/actor-name');
177+
const runsClient = actorClient.runs();
178+
// Lists the last 10 runs of your Actor.
179+
const { items } = await runsClient.list({
180+
limit: 10,
181+
desc: true,
182+
});
183+
184+
// Select the last run of your Actor that finished
185+
// with a SUCCEEDED status.
186+
const lastSucceededRunClient = actorClient.lastRun({ status: 'SUCCEEDED' });
187+
// Fetches items from the run's dataset.
188+
const { items } = await lastSucceededRunClient.dataset().listItems();
189+
```
190+
191+
The quick access to `dataset` and other storage directly from the run client can be used with the [`lastRun()`](/reference/class/ActorClient#lastRun) method.
192+
193+
## Features
194+
195+
Based on the endpoint, the client automatically extracts the relevant data and returns it in the expected format. Date strings are automatically converted to `Date` objects. For exceptions, the client throws an [`ApifyApiError`](/reference/class/ApifyApiError), which wraps the plain JSON errors returned by API and enriches them with other contexts for easier debugging.
196+
197+
```js
198+
import { ApifyClient } from 'apify-client';
199+
200+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
201+
202+
try {
203+
const { items } = await client.dataset('non-existing-dataset-id').listItems();
204+
} catch (error) {
205+
// The error is an instance of ApifyApiError
206+
const { message, type, statusCode, clientMethod, path } = error;
207+
// Log error for easier debugging
208+
console.log({ message, statusCode, clientMethod, type });
209+
}
210+
```
211+
212+
### Retries with exponential backoff
213+
214+
The client automatically retries requests that fail due to network errors, Apify API internal errors (HTTP 500+), or rate limit errors (HTTP 429). By default, the client retries up to 8 times with exponential backoff starting at 500ms.
215+
216+
To configure retry behavior, set the `maxRetries` and `minDelayBetweenRetriesMillis` options in the `ApifyClient` constructor:
217+
218+
```js
219+
import { ApifyClient } from 'apify-client';
220+
221+
const client = new ApifyClient({
222+
token: 'MY-APIFY-TOKEN',
223+
maxRetries: 8,
224+
minDelayBetweenRetriesMillis: 500, // 0.5s
225+
timeoutSecs: 360, // 6 mins
226+
});
227+
```
228+
229+
### Convenience functions and options
230+
231+
Some actions can't be performed by the API itself, such as indefinite waiting for an Actor run to finish (because of network timeouts). The client provides convenient `call()` and `waitForFinish()` functions that do that. If the limit is reached, the returned promise is resolved to a run object that will have status `READY` or `RUNNING` and it will not contain the Actor run output.
232+
233+
[Key-value store](https://docs.apify.com/platform/storage/key-value-store) records can be retrieved as objects, buffers, or streams via the respective options, dataset items can be fetched as individual objects or serialized data.
234+
235+
```js
236+
import { ApifyClient } from 'apify-client';
237+
238+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
239+
240+
// Starts an Actor and waits for it to finish.
241+
const finishedActorRun = await client.actor('username/actor-name').call();
242+
243+
// Starts an Actor and waits maximum 60s for the finish
244+
const { status } = await client.actor('username/actor-name').start({
245+
waitForFinish: 60, // 1 minute
246+
});
247+
```
248+
249+
### Pagination
250+
251+
Methods that return lists (such as `list()` or `listSomething()`) return a [`PaginatedList`](/reference/interface/PaginatedList) object. Exceptions include `listKeys()` and `listHead()`, which use different pagination mechanisms.
252+
253+
The list results are stored in the `items` property. Use the `limit` parameter to retrieve a specific number of results. Additional properties vary by method—see the method reference for details.
254+
255+
```js
256+
import { ApifyClient } from 'apify-client';
257+
258+
const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });
259+
260+
// Resource clients accept an ID of the resource.
261+
const datasetClient = client.dataset('dataset-id');
262+
263+
// Number of items per page
264+
const limit = 1000;
265+
// Initial offset
266+
let offset = 0;
267+
// Array to store all items
268+
let allItems = [];
269+
270+
while (true) {
271+
const { items, total } = await datasetClient.listItems({ limit, offset });
272+
273+
console.log(`Fetched ${items.length} items`);
274+
275+
// Merge new items with other already loaded items
276+
allItems.push(...items);
277+
278+
// If there are no more items to fetch, exit the loading
279+
if (offset + limit >= total) {
280+
break;
281+
}
282+
283+
offset += limit;
284+
}
285+
286+
console.log(`Overall fetched ${allItems.length} items`);
287+
```

0 commit comments

Comments
 (0)