SocialCrawl

Frameworks

SocialCrawl with Astro

Read a profile, search many platforms and page through posts from Astro server endpoints

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

Prerequisites

  • Node.js 22 or later.
  • Astro 7 with the Node adapter.
  • A SocialCrawl API key. Keys start with sc_.

Checking your account…

Set up

Add the Node adapter, put your key in .env, and declare it in astro.config.mjs as a server secret.

Terminal
npx astro add node
echo "SOCIALCRAWL_API_KEY=sc_your_key_here" >> .env
Warning

Never name the variable PUBLIC_SOCIALCRAWL_API_KEY. Astro copies every PUBLIC_ value into the browser bundle.

astro.config.mjs
import node from "@astrojs/node";
import { defineConfig, envField } from "astro/config";

export default defineConfig({
  adapter: node({ mode: "standalone" }),
  env: {
    schema: {
      SOCIALCRAWL_API_KEY: envField.string({ context: "server", access: "secret" }),
    },
  },
});

Get a profile

The helper sends your key in the x-api-key header and answers with the API's JSON and status. A page built ahead of time cannot hold a secret, so each route sets prerender = false and runs on the server.

src/lib/socialcrawl.ts
import { SOCIALCRAWL_API_KEY } from "astro:env/server";

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.
export async function socialcrawl(path: string, params: Record<string, string>) {
  const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, {
    headers: { "x-api-key": SOCIALCRAWL_API_KEY },
  });
  return Response.json(await res.json(), { status: res.status });
}
src/pages/api/profile.ts
import type { APIRoute } from "astro";

import { socialcrawl } from "../../lib/socialcrawl";

export const prerender = false;

export const GET: APIRoute = ({ url }) =>
  socialcrawl("/v1/tiktok/profile", { handle: url.searchParams.get("handle") ?? "charlidamelio" });

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.

src/pages/api/search.ts
import type { APIRoute } from "astro";

import { socialcrawl } from "../../lib/socialcrawl";

export const prerender = false;

// dry_run=1 prices the search and runs nothing.
export const GET: APIRoute = ({ url }) =>
  socialcrawl("/v1/search/everywhere", {
    query: url.searchParams.get("query") ?? "oat milk",
    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.

src/pages/api/posts.ts
import type { APIRoute } from "astro";

import { socialcrawl } from "../../lib/socialcrawl";

export const prerender = false;

// Send the last answer's pagination.next_cursor back as ?cursor=.
export const GET: APIRoute = ({ url }) => {
  const params: Record<string, string> = { handle: url.searchParams.get("handle") ?? "charlidamelio" };
  const cursor = url.searchParams.get("cursor");
  if (cursor) params.cursor = cursor;
  return socialcrawl("/v1/tiktok/profile/videos", params);
};

A page of posts costs 1 credit.

Test it

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

Terminal
curl "http://localhost:4321/api/profile?handle=charlidamelio"
curl "http://localhost:4321/api/search?query=oat+milk"
curl "http://localhost:4321/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.