Skip to content

Commit 7c5c09b

Browse files
committed
apify_api_and_client
1 parent 181ebde commit 7c5c09b

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

content/academy/expert_scraping_with_apify/apify_api_and_client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ There are two main ways to programmatically interact with the Apify platform: by
1616

1717
- Scroll through the [Apify API docs](https://docs.apify.com/api/v2) (there's a whole lot there, so you're not expected to memorize everything).
1818
- Read about the Apify client in [Apify's docs](https://docs.apify.com/apify-client-js). It can also be seen on [Github](https://github.com/apify/apify-client-js) and [NPM](https://www.npmjs.com/package/apify-client).
19-
- Learn about the [`Apify.newClient()`](https://sdk.apify.com/docs/api/apify#apifynewclientoptions) function in the Apify SDK.
19+
- Learn about the [`Actor.newClient()`](https://sdk.apify.com/docs/api/apify#apifynewclientoptions) function in the Apify SDK.
2020
- Skim through [this article](https://help.apify.com/en/articles/2868670-how-to-pass-data-from-web-scraper-to-another-actor) about API integration (this article is old; however, still relevant).
2121

2222
## [](#quiz) Knowledge check 📝

content/academy/expert_scraping_with_apify/solutions/using_api_and_client.md

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@ This time, let's call our project **actor-caller**.
1717
Let's also set up some boilerplate, grabbing our inputs and creating a constant variable for the task:
1818

1919
```JavaScript
20-
const Apify = require('apify');
21-
const axios = require('axios');
22-
const { URLSearchParams } = require('url');
20+
import { Actor } from 'apify';
21+
import axios from 'axios';
2322

24-
Apify.main(async () => {
25-
const { useClient, memory, fields, maxItems } = await Apify.getInput();
23+
await Actor.init();
2624

27-
const TASK = 'YOUR_USERNAME~demo-actor-task';
25+
const { useClient, memory, fields, maxItems } = await Actor.getInput();
2826

29-
// our future code will go here
30-
});
27+
const TASK = 'YOUR_USERNAME~demo-actor-task';
28+
29+
// our future code will go here
30+
31+
await Actor.exit();
3132
```
3233

3334
## [](#calling-a-task-via-client) Calling a task via JavaScript client
3435

35-
When using the `apify-client` package, you can create a new client instance by using `new ApifyClient()`. Within the Apify SDK however, it is not necessary to even install the `apify-client` package, as the `Apify.newClient()` function is available for use.
36+
When using the `apify-client` package, you can create a new client instance by using `new ApifyClient()`. Within the Apify SDK however, it is not necessary to even install the `apify-client` package, as the `Actor.newClient()` function is available for use.
3637

3738
We'll start by creating a function called `withClient()` and creating a new client, then calling the task:
3839

3940
```JavaScript
4041
const withClient = async () => {
41-
const client = Apify.newClient();
42+
const client = Actor.newClient();
4243
const task = client.task(TASK);
4344

4445
const { id } = await task.call({ memory });
@@ -49,7 +50,7 @@ After the task has run, we'll grab hold of its dataset, then attempt to download
4950

5051
```JavaScript
5152
const withClient = async () => {
52-
const client = Apify.newClient();
53+
const client = Actor.newClient();
5354
const task = client.task(TASK);
5455

5556
const { id } = await task.call({ memory });
@@ -63,7 +64,7 @@ const withClient = async () => {
6364

6465
// If the content type is anything other than JSON, it must
6566
// be specified within the third options parameter
66-
return Apify.setValue('OUTPUT', items, { contentType: 'text/csv' });
67+
return Actor.setValue('OUTPUT', items, { contentType: 'text/csv' });
6768
};
6869
```
6970

@@ -111,7 +112,7 @@ const withAPI = async () => {
111112

112113
const { data } = await axios.post(url.toString());
113114

114-
return Apify.setValue('OUTPUT', data, { contentType: 'text/csv' });
115+
return Actor.setValue('OUTPUT', data, { contentType: 'text/csv' });
115116
};
116117
```
117118

@@ -169,51 +170,52 @@ And before we push to the platform, let's not forget to write an input schema in
169170
To ensure we're on the same page, here is what the final code looks like:
170171

171172
```JavaScript
172-
const Apify = require('apify');
173-
const axios = require('axios');
174-
const { URLSearchParams } = require('url');
173+
import { Actor } from 'apify';
174+
import axios from 'axios';
175175

176-
Apify.main(async () => {
177-
const { useClient, memory, fields, maxItems } = await Apify.getInput();
176+
await Actor.init();
178177

179-
const TASK = 'YOUR_USERNAME~demo-actor-task';
178+
const { useClient, memory, fields, maxItems } = await Actor.getInput();
180179

181-
const withClient = async () => {
182-
const client = Apify.newClient();
183-
const task = client.task(TASK);
180+
const TASK = 'YOUR_USERNAME~demo-actor-task';
184181

185-
const { id } = await task.call({ memory });
182+
const withClient = async () => {
183+
const client = Actor.newClient();
184+
const task = client.task(TASK);
186185

187-
const dataset = client.run(id).dataset();
186+
const { id } = await task.call({ memory });
188187

189-
const items = await dataset.downloadItems('csv', {
190-
limit: maxItems,
191-
fields,
192-
});
188+
const dataset = client.run(id).dataset();
193189

194-
return Apify.setValue('OUTPUT', items, { contentType: 'text/csv' });
195-
};
190+
const items = await dataset.downloadItems('csv', {
191+
limit: maxItems,
192+
fields,
193+
});
196194

197-
const withAPI = async () => {
198-
const uri = `https://api.apify.com/v2/actor-tasks/${TASK}/run-sync-get-dataset-items?`;
199-
const url = new URL(uri);
195+
return Actor.setValue('OUTPUT', items, { contentType: 'text/csv' });
196+
};
200197

201-
url.search = new URLSearchParams({
202-
memory,
203-
format: 'csv',
204-
limit: maxItems,
205-
fields: fields.join(','),
206-
token: process.env.APIFY_TOKEN,
207-
});
198+
const withAPI = async () => {
199+
const uri = `https://api.apify.com/v2/actor-tasks/${TASK}/run-sync-get-dataset-items?`;
200+
const url = new URL(uri);
208201

209-
const { data } = await axios.post(url.toString());
202+
url.search = new URLSearchParams({
203+
memory,
204+
format: 'csv',
205+
limit: maxItems,
206+
fields: fields.join(','),
207+
token: process.env.APIFY_TOKEN,
208+
});
210209

211-
return Apify.setValue('OUTPUT', data, { contentType: 'text/csv' });
212-
};
210+
const { data } = await axios.post(url.toString());
211+
212+
return Actor.setValue('OUTPUT', data, { contentType: 'text/csv' });
213+
};
214+
215+
if (useClient) await withClient();
216+
else await withAPI();
213217

214-
if (useClient) await withClient();
215-
else await withAPI();
216-
});
218+
await Actor.exit();
217219
```
218220

219221
## [](#quiz-answers) Quiz answers 📝
@@ -230,7 +232,7 @@ The one main difference is that the Apify client automatically uses [**exponenti
230232

231233
**Q: Do you need to install the `apify-client` NPM package when already using the `apify` package?**
232234

233-
**A:** No. The Apify client is available right in the SDK with the `Apify.newClient()` function.
235+
**A:** No. The Apify client is available right in the SDK with the `Actor.newClient()` function.
234236

235237
## [](#wrap-up) Wrap up
236238

0 commit comments

Comments
 (0)