SocialCrawl

Serverless

SocialCrawl with AWS Lambda

Deploy a Lambda function that reads a profile, searches many platforms and pages through posts

One handler answers three routes, /api/profile, /api/search and /api/posts, with the API's JSON unchanged.

Prerequisites

  • An AWS account, the AWS CLI, and an execution role for the function.
  • The Node.js 22 Lambda runtime.
  • A SocialCrawl API key. Keys start with sc_.

Checking your account…

Set up

The function reads the key from its environment. For production, keep it in AWS Secrets Manager instead.

Terminal
export SOCIALCRAWL_API_KEY=sc_your_key_here
export LAMBDA_ROLE_ARN=arn:aws:iam::123456789012:role/socialcrawl-lambda
Warning

Do not give the function a public Function URL. Anyone who finds an open URL can spend your credits. Call it with aws lambda invoke, or put it behind an authorizer.

Get a profile

The helper sends your key in the x-api-key header and answers with the API's JSON and status. handler reads the route from event.rawPath and the query from event.queryStringParameters.

index.mjs
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, params) {
  const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, {
    headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY },
  });
  return {
    statusCode: res.status,
    headers: { "content-type": "application/json" },
    body: await res.text(),
  };
}

function profile(query) {
  return socialcrawl("/v1/tiktok/profile", { handle: query.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.

index.mjs
// dry_run=1 prices the search and runs nothing.
function search(query) {
  return socialcrawl("/v1/search/everywhere", {
    query: query.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.

index.mjs
// Send the last answer's pagination.next_cursor back as cursor.
function posts(query) {
  const params = { handle: query.handle ?? "charlidamelio" };
  if (query.cursor) params.cursor = query.cursor;
  return socialcrawl("/v1/tiktok/profile/videos", params);
}

const routes = { "/api/profile": profile, "/api/search": search, "/api/posts": posts };

// An API Gateway (v2) or Function URL event: the path and the query.
export const handler = async (event) => {
  const route = routes[event.rawPath];
  if (!route) return { statusCode: 404, body: "Not found" };
  return route(event.queryStringParameters ?? {});
};

A page of posts costs 1 credit.

Deploy

Zip index.mjs and create the function with the key in its environment. After a change, run aws lambda update-function-code with the same zip.

Terminal
zip function.zip index.mjs
aws lambda create-function --function-name socialcrawl --runtime nodejs22.x --handler index.handler --role "$LAMBDA_ROLE_ARN" --zip-file fileb://function.zip --environment "Variables={SOCIALCRAWL_API_KEY=$SOCIALCRAWL_API_KEY}"

Test it

Invoke the function once per route. Each file holds the answer, and its body holds the API's JSON with data.author.followers, data.estimate or pagination.next_cursor.

Terminal
aws lambda invoke --function-name socialcrawl --cli-binary-format raw-in-base64-out --payload '{"rawPath":"/api/profile","queryStringParameters":{"handle":"charlidamelio"}}' profile.json
aws lambda invoke --function-name socialcrawl --cli-binary-format raw-in-base64-out --payload '{"rawPath":"/api/search","queryStringParameters":{"query":"oat milk"}}' search.json
aws lambda invoke --function-name socialcrawl --cli-binary-format raw-in-base64-out --payload '{"rawPath":"/api/posts","queryStringParameters":{"handle":"charlidamelio","cursor":""}}' posts.json

Next steps

On this page

Ready to build?

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