SocialCrawl

Frameworks

SocialCrawl with Bun

Build a fast Bun server that reads a profile, searches many platforms and pages through posts

The app serves three routes under /api, and each answers with the API's JSON unchanged.

Prerequisites

  • Bun 1.2.3 or later, for the routes option of Bun.serve.
  • A SocialCrawl API key. Keys start with sc_.

Checking your account…

Set up

There is no package to install. Bun reads .env on its own.

Terminal
echo "SOCIALCRAWL_API_KEY=sc_your_key_here" >> .env

Keep the key on the server, out of your code and out of the browser.

Get a profile

The helper sends your key in the x-api-key header and answers with the API's JSON and status.

server.ts
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(path: string, params: Record<string, string>) {
  const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, {
    headers: { "x-api-key": Bun.env.SOCIALCRAWL_API_KEY ?? "" },
  });
  return Response.json(await res.json(), { status: res.status });
}

function profile(req: Request) {
  const handle = new URL(req.url).searchParams.get("handle") ?? "charlidamelio";
  return socialcrawl("/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.

server.ts
// dry_run=1 prices the search and runs nothing.
function search(req: Request) {
  const query = new URL(req.url).searchParams.get("query") ?? "oat milk";
  return socialcrawl("/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.

server.ts
// Send the last answer's pagination.next_cursor back as ?cursor=.
function posts(req: Request) {
  const { searchParams } = new URL(req.url);
  const params: Record<string, string> = { handle: searchParams.get("handle") ?? "charlidamelio" };
  const cursor = searchParams.get("cursor");
  if (cursor) params.cursor = cursor;
  return socialcrawl("/v1/tiktok/profile/videos", params);
}

Bun.serve({
  port: Bun.env.PORT ?? 3000,
  routes: { "/api/profile": profile, "/api/search": search, "/api/posts": posts },
});

A page of posts costs 1 credit.

Script usage

The same calls run as a script, with no server. Save this as script.ts and run bun run script.ts.

script.ts
const API = "https://www.socialcrawl.dev";

async function call(path: string, params: Record<string, string>) {
  const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, {
    headers: { "x-api-key": Bun.env.SOCIALCRAWL_API_KEY ?? "" },
  });
  const body = await res.json();
  if (!body.success) throw new Error(`${body.error.type} (${body.error.doc_url})`);
  return body;
}

const profile = await call("/v1/tiktok/profile", { handle: "charlidamelio" });
console.log("followers:", profile.data.author.followers);

const quote = await call("/v1/search/everywhere", { query: "oat milk", dry_run: "1" });
console.log("estimate:", quote.data.estimate.credits);

const params: Record<string, string> = { handle: "charlidamelio" };
for (const page of [1, 2]) {
  const posts = await call("/v1/tiktok/profile/videos", params);
  console.log(`page ${page}:`, posts.data.items.length, "posts");
  if (!posts.pagination.has_more) break;
  params.cursor = posts.pagination.next_cursor;
}

Test it

Start the app with bun run server.ts, then call the three routes. The answers hold data.author.followers, data.estimate and pagination.next_cursor.

Terminal
curl "http://localhost:3000/api/profile?handle=charlidamelio"
curl "http://localhost:3000/api/search?query=oat+milk"
curl "http://localhost:3000/api/posts?handle=charlidamelio&cursor="

Next steps

On this page

Ready to build?

Start getting social data for free and scale as your project grows. No credit card needed.