Index of every page on this site: llms.txt
All skills
Run it yourself

Production-ready patterns for Apify SDK and apify-client in TypeScript. Use when building Actors with Crawlee, managing datasets/KV stores, or implementing robust client wrappers with retry and validation. Trigger with "apify SDK patterns", "apify best practices", "apify client wrapper", "crawlee patterns", "idiomatic apify".

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/apify-sdk-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesexamples.md

≈852 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Apify SDK Patterns — Worked Examples

Two end-to-end scenarios that compose the patterns from SKILL.md and patterns.md into runnable flows.

Example A: Call a remote Actor safely from an app

Goal: an external Node service runs the hosted apify/web-scraper Actor, reads its dataset, and never throws on failure. It combines the typed client singleton (Pattern 1) with the safe result wrapper (Pattern 8).

// src/apify/client.ts
import { ApifyClient } from 'apify-client';

let instance: ApifyClient | null = null;

export function getApifyClient(): ApifyClient {
  if (!instance) {
    const token = process.env.APIFY_TOKEN;
    if (!token) throw new Error('APIFY_TOKEN is required');
    instance = new ApifyClient({ token });
  }
  return instance;
}
// src/scrape.ts
import { getApifyClient } from './apify/client';

type Result<T> = { data: T; error: null } | { data: null; error: Error };

async function safeActorCall<T>(
  actorId: string,
  input: Record<string, unknown>,
): Promise<Result<T[]>> {
  try {
    const client = getApifyClient();
    const run = await client.actor(actorId).call(input, { timeout: 300 });

    if (run.status !== 'SUCCEEDED') {
      return { data: null, error: new Error(`Run ${run.status}: ${run.statusMessage}`) };
    }

    const { items } = await client.dataset(run.defaultDatasetId).listItems();
    return { data: items as T[], error: null };
  } catch (err) {
    return { data: null, error: err as Error };
  }
}

const result = await safeActorCall<{ url: string; title: string }>(
  'apify/web-scraper', { startUrls: [{ url: 'https://example.com' }] }
);

if (result.error) {
  console.error('Actor call failed:', result.error.message);
} else {
  console.log(`Got ${result.data.length} items`);
}

Expected outcome: on success, a count of scraped items; on any failure (non-SUCCEEDED run status or thrown exception), a single logged error message — no uncaught rejection.

Example B: A two-tier product scraper Actor

Goal: an Actor that crawls product listing pages, follows links to detail pages, and pushes structured product records. It combines crawler selection (Pattern 2), the router (Pattern 7), and dataset writes (Pattern 4).

import { Actor } from 'apify';
import { CheerioCrawler, createCheerioRouter } from 'crawlee';

const router = createCheerioRouter();

// Listing pages → enqueue detail links
router.addDefaultHandler(async ({ $, enqueueLinks }) => {
  const detailLinks = $('a.product-link')
    .map((_, el) => $(el).attr('href'))
    .get();

  await enqueueLinks({ urls: detailLinks, label: 'DETAIL' });
});

// Detail pages → extract and store one product
router.addHandler('DETAIL', async ({ request, $ }) => {
  await Actor.pushData({
    url: request.url,
    name: $('h1.product-name').text().trim(),
    price: parseFloat($('.price').text().replace('$', '')),
    description: $('div.description').text().trim(),
  });
});

await Actor.main(async () => {
  const crawler = new CheerioCrawler({ requestHandler: router });
  await crawler.run(['https://example-store.com/products']);
});

Expected outcome: the default dataset fills with one row per product (url, name, price, description), which you can then download as CSV or JSON via the apify-client dataset calls in patterns.md.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides production-ready TypeScript patterns for Apify and Crawlee, emphasizing secure handling of API tokens via environment variables. It includes robust error handling and safe execution wrappers. The patterns involve ingesting untrusted web data, which is an inherent part of scraping functionality, but no malicious intent or vulnerabilities beyond the expected surface were found.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: MEDIUM · 1 issue

Signed by skilld at 820dfc8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 4 months ago
What it can do
Reads files Edits files
version
1.5.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
tags
[
  "saas",
  "scraping",
  "automation",
  "apify"
]
All 3 allowed tools
ReadWriteEdit
Other metadata
compatibility
Designed for Claude Code

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/apify-sdk-patterns