> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # Cloudflare Workers (/docs/quickstart/cloudflare-workers) Deploy a Worker that reads a profile, searches many platforms and pages through posts at the edge The app serves three routes under `/api`, and each answers with the API's JSON unchanged. ## Prerequisites - Node.js 22 or later and a Cloudflare account. - A SocialCrawl API key. Keys start with `sc_`. Get a free API key at https://www.socialcrawl.dev/dashboard/api. New accounts start with 100 welcome credits and no card is needed. ## Set up Install Wrangler. Put the key in `.dev.vars` for `wrangler dev`, and store it as a secret for the deployed Worker. ```bash title="Terminal" npm install wrangler echo "SOCIALCRAWL_API_KEY=sc_your_key_here" > .dev.vars npx wrangler secret put SOCIALCRAWL_API_KEY ``` Keep the key on the server, out of your code and out of the browser. ```jsonc title="wrangler.jsonc" { "name": "socialcrawl-quickstart", "main": "src/index.ts", "compatibility_date": "2026-11-01" } ``` ## Get a profile The helper sends your key in the `x-api-key` header and answers with the API's JSON and status. The key arrives on `env`, not on `process.env`. ```typescript title="src/index.ts" interface Env { SOCIALCRAWL_API_KEY: string; } const API = "https://www.socialcrawl.dev"; // Send one request with your key and answer with the API's JSON and status. // The JSON carries success, and error.type and error.doc_url when it is false. async function socialcrawl(env: Env, path: string, params: Record) { const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers: { "x-api-key": env.SOCIALCRAWL_API_KEY }, }); return Response.json(await res.json(), { status: res.status }); } function profile(url: URL, env: Env) { const handle = url.searchParams.get("handle") ?? "charlidamelio"; return socialcrawl(env, "/v1/tiktok/profile", { handle }); } ``` Every answer carries `success`. Branch on it, then on `error.type`, never on the message text. ## Search across platforms With `dry_run=1` the search is free and runs nothing. `data.estimate` holds the credits and seconds the real search would take. ```typescript title="src/index.ts" // dry_run=1 prices the search and runs nothing. function search(url: URL, env: Env) { const query = url.searchParams.get("query") ?? "oat milk"; return socialcrawl(env, "/v1/search/everywhere", { query, format: "compact", dry_run: "1" }); } ``` Drop `dry_run` to run the search for 20 credits. ## Get posts and the next page Each answer holds one page of posts in `data.items`. Send `pagination.next_cursor` back as `cursor`, and stop when `pagination.has_more` is false. ```typescript title="src/index.ts" // Send the last answer's pagination.next_cursor back as ?cursor=. function posts(url: URL, env: Env) { const params: Record = { handle: url.searchParams.get("handle") ?? "charlidamelio" }; const cursor = url.searchParams.get("cursor"); if (cursor) params.cursor = cursor; return socialcrawl(env, "/v1/tiktok/profile/videos", params); } export default { async fetch(request: Request, env: Env): Promise { const url = new URL(request.url); if (url.pathname === "/api/profile") return profile(url, env); if (url.pathname === "/api/search") return search(url, env); if (url.pathname === "/api/posts") return posts(url, env); return new Response("Not found", { status: 404 }); }, }; ``` A page of posts costs 1 credit. ## Deploy Deploy the Worker. It reads the secret you stored with `wrangler secret put`. ```bash title="Terminal" npx wrangler deploy ``` ## Test it Start the app with `npx wrangler dev`, then call the three routes. The answers hold `data.author.followers`, `data.estimate` and `pagination.next_cursor`. ```bash title="Terminal" curl "http://localhost:8787/api/profile?handle=charlidamelio" curl "http://localhost:8787/api/search?query=oat+milk" curl "http://localhost:8787/api/posts?handle=charlidamelio&cursor=" ``` ## Next steps - [Price a watch for free](/docs/engines/monitors.md): POST /v1/monitors?dry_run=1 quotes a scheduled watch of an account and creates nothing. - [Common jobs](/docs/common-jobs.md): The first call for each job, from profiles to comments and reviews. - [Search](/docs/search.md): Choose between one platform - [Errors](/docs/errors.md): Every error type, what it means, and which ones to retry.