SocialCrawl

Serverless

SocialCrawl with Netlify Functions

Deploy Netlify functions that read a profile, search many platforms and page through posts

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

Prerequisites

  • Node.js 22 or later, the Netlify CLI and a Netlify site.
  • A SocialCrawl API key. Keys start with sc_.

계정을 확인하는 중입니다.

Set up

Set the key on the site, and in .env for netlify dev.

Terminal
netlify env:set SOCIALCRAWL_API_KEY sc_your_key_here
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. Each file under netlify/functions is one function, and config.path serves it at /api/profile rather than under /.netlify/functions.

netlify/lib/socialcrawl.mts
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": process.env.SOCIALCRAWL_API_KEY ?? "" },
  });
  return Response.json(await res.json(), { status: res.status });
}
netlify/functions/profile.mts
import { socialcrawl } from "../lib/socialcrawl.mts";

export default (request: Request) => {
  const handle = new URL(request.url).searchParams.get("handle") ?? "charlidamelio";
  return socialcrawl("/v1/tiktok/profile", { handle });
};

export const config = { path: "/api/profile" };

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.

netlify/functions/search.mts
import { socialcrawl } from "../lib/socialcrawl.mts";

// dry_run=1 prices the search and runs nothing.
export default (request: Request) => {
  const query = new URL(request.url).searchParams.get("query") ?? "oat milk";
  return socialcrawl("/v1/search/everywhere", { query, format: "compact", dry_run: "1" });
};

export const config = { path: "/api/search" };

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.

netlify/functions/posts.mts
import { socialcrawl } from "../lib/socialcrawl.mts";

// Send the last answer's pagination.next_cursor back as ?cursor=.
export default (request: Request) => {
  const { searchParams } = new URL(request.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);
};

export const config = { path: "/api/posts" };

A page of posts costs 1 credit.

Deploy

Deploy the site to production. The functions read the key you set with netlify env:set.

Terminal
netlify deploy --prod

Test it

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

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

Next steps

On this page

시작할 준비가 됐습니까?

무료로 소셜 데이터를 받기 시작하고, 프로젝트가 커지면 그대로 확장합니다. 카드 등록은 필요 없습니다.