1import { ApifyClient } from 'apify-client';
2
3
4
5const client = new ApifyClient({
6 token: '<YOUR_API_TOKEN>',
7});
8
9
10const input = {
11 "code": `# `input` holds the job's JSON input. Set `result` to return data (any JSON-serializable value).
12import statistics
13
14nums = input.get("numbers", [])
15print(f"Got {len(nums)} numbers")
16result = {
17 "count": len(nums),
18 "sum": sum(nums),
19 "mean": statistics.mean(nums) if nums else None,
20 "sorted": sorted(nums),
21}`,
22 "inputJson": {
23 "numbers": [
24 1,
25 2,
26 3
27 ]
28 },
29 "jobs": [
30 {
31 "id": "python-stats",
32 "input": {
33 "numbers": [
34 4,
35 8,
36 15,
37 16,
38 23,
39 42
40 ]
41 }
42 },
43 {
44 "id": "js-word-count",
45 "language": "javascript",
46 "code": "// `input` holds the job's JSON input. `return` a value (top-level await works).\nconst words = (input.text || '').toLowerCase().match(/[a-z']+/g) || [];\nconst counts = {};\nfor (const w of words) counts[w] = (counts[w] || 0) + 1;\nconsole.log(`Counted ${words.length} words`);\nreturn Object.entries(counts)\n .sort((a, b) => b[1] - a[1])\n .slice(0, 5)\n .map(([word, count]) => ({ word, count }));\n",
47 "input": {
48 "text": "The quick brown fox jumps over the lazy dog. The dog sleeps; the fox runs."
49 }
50 }
51 ]
52};
53
54
55const run = await client.actor("gazidev/code-runner-sandbox").call(input);
56
57
58console.log('Results from dataset');
59console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
60const { items } = await client.dataset(run.defaultDatasetId).listItems();
61items.forEach((item) => {
62 console.dir(item);
63});
64
65