/apify-pack:apify-sdk-patternsfrom apify-pack
Provides production-ready patterns for Apify SDK and apify-client in TypeScript, including Crawlee crawler selection, typed client singletons, and Actor lifecycle with error handling.
Ships in apify-pack. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Production patterns for both the apify SDK (building Actors) and apify-client (calling Actors remotely). Covers Crawlee crawler selection, data storage, proxy configuration, and typed client wrappers.
apify-client and/or apify + crawlee installedAPIFY_TOKEN configured// 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;
}
// Reset for testing
export function resetClient(): void {
instance = null;
}
Choose the right crawler for the job:
import { CheerioCrawler, PlaywrightCrawler, PuppeteerCrawler } from 'crawlee';
// CHEERIO — Fast, lightweight, no JavaScript rendering
// Use for: static HTML, server-rendered pages, APIs
const cheerioCrawler = new CheerioCrawler({
async requestHandler({ request, $, enqueueLinks }) {
const title = $('title').text();
await Actor.pushData({ url: request.url, title });
await enqueueLinks({ strategy: 'same-domain' });
},
});
// PLAYWRIGHT — Full browser, all engines, modern API
// Use for: SPAs, JavaScript-heavy pages, complex interactions
const playwrightCrawler = new PlaywrightCrawler({
launchContext: { launchOptions: { headless: true } },
async requestHandler({ page, request, enqueueLinks }) {
await page.waitForSelector('h1');
const title = await page.title();
const content = await page.$eval('main', el => el.textContent);
await Actor.pushData({ url: request.url, title, content });
await enqueueLinks({ strategy: 'same-domain' });
},
});
// PUPPETEER — Chromium-only browser automation
// Use for: when you need Chromium specifically or legacy Puppeteer code
const puppeteerCrawler = new PuppeteerCrawler({
async requestHandler({ page, request }) {
const title = await page.title();
await Actor.pushData({ url: request.url, title });
},
});
import { Actor } from 'apify';
import { CheerioCrawler, log } from 'crawlee';
// Actor.main() wraps init + exit + error handling
await Actor.main(async () => {
const input = await Actor.getInput<{
startUrls: { url: string }[];
maxPages?: number;
proxyConfig?: { useApifyProxy: boolean; groups?: string[] };
}>();
if (!input?.startUrls?.length) {
throw new Error('Input must include at least one startUrl');
}
// Configure proxy if requested
const proxyConfiguration = input.proxyConfig?.useApifyProxy
? await Actor.createProxyConfiguration({
groups: input.proxyConfig.groups,
})
: undefined;
const crawler = new CheerioCrawler({
proxyConfiguration,
maxRequestsPerCrawl: input.maxPages ?? 50,
maxConcurrency: 10,
async requestHandler({ request, $, enqueueLinks }) {
log.info(`Processing ${request.url}`);
await Actor.pushData({
url: request.url,
title: $('title').text().trim(),
h1: $('h1').first().text().trim(),
paragraphs: $('p').map((_, el) => $(el).text().trim()).get(),
});
await enqueueLinks({ strategy: 'same-domain' });
},
async failedRequestHandler({ request }, error) {
log.error(`Request failed: ${request.url}`, { error: error.message });
await Actor.pushData({
url: request.url,
error: error.message,
'#isFailed': true,
});
},
});
await crawler.run(input.startUrls.map(s => s.url));
log.info(`Crawler finished. ${crawler.stats.state.requestsFinished} pages processed.`);
});
import { Actor } from 'apify';
import { ApifyClient } from 'apify-client';
// --- Inside an Actor (apify SDK) ---
// Push single item
await Actor.pushData({ url: 'https://example.com', title: 'Example' });
// Push batch
await Actor.pushData([
{ url: 'https://a.com', price: 10 },
{ url: 'https://b.com', price: 20 },
]);
// Store named output in key-value store
await Actor.setValue('SUMMARY', {
totalItems: 100,
avgPrice: 15.50,
crawledAt: new Date().toISOString(),
});
// Get value back
const summary = await Actor.getValue('SUMMARY');
// --- From external app (apify-client) ---
const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
// List dataset items with pagination
const { items, total } = await client
.dataset('DATASET_ID')
.listItems({ limit: 1000, offset: 0 });
// Push items to a named dataset
const dataset = await client.datasets().getOrCreate('my-results');
await client.dataset(dataset.id).pushItems([
{ url: 'https://example.com', data: 'scraped content' },
]);
// Download entire dataset
const csv = await client.dataset(dataset.id).downloadItems('csv');
const json = await client.dataset(dataset.id).downloadItems('json');
import { ApifyClient } from 'apify-client';
const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
// Create or get a named store
const store = await client.keyValueStores().getOrCreate('my-config');
const storeClient = client.keyValueStore(store.id);
// Set a record (any content type)
await storeClient.setRecord({
key: 'CONFIG',
value: { retries: 3, timeout: 30000 },
contentType: 'application/json',
});
// Get a record
const record = await storeClient.getRecord('CONFIG');
console.log(record?.value); // { retries: 3, timeout: 30000 }
// Store binary data (screenshots, PDFs)
await storeClient.setRecord({
key: 'screenshot.png',
value: screenshotBuffer,
contentType: 'image/png',
});
// List all keys
const { items: keys } = await storeClient.listKeys();
import { Actor } from 'apify';
// Datacenter proxy (included in subscription, fast)
const dcProxy = await Actor.createProxyConfiguration({
groups: ['BUYPROXIES94952'],
});
// Residential proxy (pay per GB, high success rate)
const resProxy = await Actor.createProxyConfiguration({
groups: ['RESIDENTIAL'],
countryCode: 'US',
});
// Google SERP proxy (specialized for Google)
const serpProxy = await Actor.createProxyConfiguration({
groups: ['GOOGLE_SERP'],
});
// Use with any crawler
const crawler = new CheerioCrawler({
proxyConfiguration: dcProxy,
// ...
});
import { Actor } from 'apify';
import { CheerioCrawler, createCheerioRouter } from 'crawlee';
const router = createCheerioRouter();
// Default route — listing pages
router.addDefaultHandler(async ({ request, $, enqueueLinks }) => {
// Extract links to detail pages
const detailLinks = $('a.product-link')
.map((_, el) => $(el).attr('href'))
.get();
await enqueueLinks({
urls: detailLinks,
label: 'DETAIL',
});
});
// Detail route — individual item pages
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']);
});
type Result<T> = { data: T; error: null } | { data: null; error: Error };
async function safeActorCall<T>(
client: ApifyClient,
actorId: string,
input: Record<string, unknown>,
): Promise<Result<T[]>> {
try {
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 };
}
}
// Usage
const result = await safeActorCall<{ url: string; title: string }>(
client, '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`);
}
| Pattern | Use Case | Benefit |
|---|---|---|
Actor.main() | Actor entry point | Auto init/exit + error reporting |
failedRequestHandler | Per-request failures | Log failures without stopping crawl |
| Safe wrapper | External calls | Prevents uncaught exceptions |
| Router | Multi-page scrapes | Clean separation of page types |
| Proxy rotation | Anti-bot sites | Higher success rate |
Apply patterns in apify-core-workflow-a for a complete web scraping workflow.
What SKILL.md sets; muted values are Claude Code's defaults
18 skills in this plugin
/apify-ci-integrationConfigure CI/CD pipelines for Apify Actor builds and deployments.
Use when automating Actor deployment via GitHub Actions,
running integration tests against Apify, or building CI/CD for scrapers.
Trigger: "apify CI", "apify GitHub Actions", "apify automated deploy",
"CI apify", "apify pipeline", "auto deploy actor".
/apify-common-errorsDiagnose and fix common Apify Actor and API errors.
Use when encountering run failures, API errors, proxy issues,
or Actor crashes on the Apify platform.
Trigger: "apify error", "fix apify", "actor failed",
"apify not working", "debug apify", "apify 429".
/apify-core-workflow-aBuild a complete web scraping Actor with Crawlee and deploy to Apify.
Use when implementing end-to-end scraping: input schema, crawler,
data extraction, dataset output, and platform deployment.
Trigger: "apify scrape website", "build apify actor",
"crawlee scraper", "apify main workflow".
/apify-core-workflow-bManage Apify datasets, key-value stores, and request queues programmatically.
Use when reading/writing datasets, exporting data, managing Actor storage,
or orchestrating multi-Actor pipelines.
Trigger: "apify dataset", "apify key-value store", "apify storage",
"export apify data", "apify pipeline", "apify request queue".
/apify-cost-tuningOptimize Apify platform costs through memory tuning, compute unit management, and proxy budgeting.
Use when analyzing Apify billing, reducing Actor run costs,
or implementing usage monitoring and budget alerts.
Trigger: "apify cost", "apify billing", "reduce apify costs",
"apify pricing", "apify expensive", "apify budget", "compute units".
/apify-debug-bundleCollect Apify debug evidence for support tickets and troubleshooting.
Use when encountering persistent issues, preparing support tickets,
or collecting diagnostic information about failed Actor runs.
Trigger: "apify debug", "apify support bundle", "collect apify logs",
"apify diagnostic", "apify run failed why".
npx claudepluginhub flight505/skill-forge --plugin apify-packOther plugins' skills for the same job