SocialCrawl

Frameworks

SocialCrawl with Mastra

Give a Mastra agent tools that read a profile, search many platforms and page through posts

Prerequisites

  • Node.js 22 or later and a Mastra project.
  • A SocialCrawl API key, and a key for the model the agent uses.

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

Set up

Install Mastra and zod, then put both keys in .env.

Terminal
npm install @mastra/core zod
npm install -D mastra
echo "SOCIALCRAWL_API_KEY=sc_your_key_here" >> .env
echo "OPENAI_API_KEY=your_model_key" >> .env

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

Build the SocialCrawl tools

Each tool sends your key in the x-api-key header and throws with error.type and error.doc_url when success is false. The search tool keeps dry_run=1, so it is free.

src/mastra/tools/socialcrawl.ts
import { createTool } from "@mastra/core/tools";
import { z } from "zod";

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

// Send one request with your key. Throw with error.type and error.doc_url
// when success is false.
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 ?? "" },
  });
  const body = await res.json();
  if (!body.success) {
    throw new Error(`${body.error.type}: ${body.error.message} (${body.error.doc_url})`);
  }
  return body;
}

export const getProfile = createTool({
  id: "get-tiktok-profile",
  description:
    "Read a public TikTok profile. data.author holds the follower count; credits_used says what the call cost.",
  inputSchema: z.object({
    handle: z.string().describe("A TikTok handle without the @, such as charlidamelio"),
  }),
  execute: async ({ handle }) => socialcrawl("/v1/tiktok/profile", { handle }),
});

export const searchEverywhere = createTool({
  id: "price-search-everywhere",
  description:
    "Price a search across many platforms without running it. It is free: dry_run=1 returns data.estimate with the credits and seconds the real search would take.",
  inputSchema: z.object({ query: z.string().describe("What to search for, such as oat milk") }),
  execute: async ({ query }) =>
    socialcrawl("/v1/search/everywhere", { query, format: "compact", dry_run: "1" }),
});

export const getPosts = createTool({
  id: "get-tiktok-posts",
  description:
    "One page of an account's TikTok posts in data.items, and what it cost in credits_used. For the next page, pass pagination.next_cursor back as cursor; stop when pagination.has_more is false.",
  inputSchema: z.object({
    handle: z.string().describe("A TikTok handle without the @"),
    cursor: z.string().optional().describe("pagination.next_cursor from the last page"),
  }),
  execute: async ({ handle, cursor }) =>
    socialcrawl("/v1/tiktok/profile/videos", cursor ? { handle, cursor } : { handle }),
});

A real search costs 20 credits and a page of posts costs 1 credit. Send pagination.next_cursor back as cursor for the next page.

Create the agent

Give the agent the three tools. The model picks a tool by its description, so each one says what it returns and what it costs.

src/mastra/agents/social-agent.ts
import { Agent } from "@mastra/core/agent";

import { getPosts, getProfile, searchEverywhere } from "../tools/socialcrawl";

export const socialAgent = new Agent({
  id: "social-agent",
  name: "Social agent",
  instructions:
    "You answer questions about public social media accounts with the SocialCrawl tools. Price a search across platforms before you suggest running one. Fetch the next page of posts only when asked.",
  model: "openai/gpt-5-mini",
  tools: { getProfile, searchEverywhere, getPosts },
});

Register the agent

Register the agent on the Mastra instance.

src/mastra/index.ts
import { Mastra } from "@mastra/core/mastra";

import { socialAgent } from "./agents/social-agent";

export const mastra = new Mastra({ agents: { socialAgent } });

Test in Studio

Start Studio and open http://localhost:4111. Ask the agent for charlidamelio's follower count, then for two pages of posts, and read each tool call and its answer. To give an agent every endpoint without writing tools, connect the hosted MCP server instead.

Terminal
npx mastra dev

Next steps

On this page

시작할 준비가 됐습니까?

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