LangChain
Wrap the SocialCrawl API in a LangChain tool so your agent can fetch live social media data.
LangChain
SocialCrawl does not ship a LangChain package, and it does not need one: the API is one authenticated GET request, so you wrap it in a tool() in about 30 lines. Once bound to your model, your agent can pull live data from 42 platforms through a single tool.
How do I add social media data to a LangChain agent?
Define a tool with a zod schema that takes a platform, a resource, and the endpoint's query params, then call the SocialCrawl REST API inside execute with your key in the x-api-key header. Bind the tool to a chat model and the agent decides when to call it.
// socialcrawl-langchain.ts
// Run with: SOCIALCRAWL_API_KEY=sc_... ANTHROPIC_API_KEY=... npx tsx socialcrawl-langchain.ts
import { tool } from "@langchain/core/tools";
import { ChatAnthropic } from "@langchain/anthropic";
import { HumanMessage, ToolMessage } from "@langchain/core/messages";
import { z } from "zod";
const API_KEY = process.env.SOCIALCRAWL_API_KEY;
if (!API_KEY) throw new Error("Set SOCIALCRAWL_API_KEY");
const socialcrawl = tool(
async ({ platform, resource, params }) => {
const url = new URL(`https://www.socialcrawl.dev/v1/${platform}/${resource}`);
for (const [key, value] of Object.entries(params ?? {})) {
url.searchParams.set(key, String(value));
}
const res = await fetch(url, { headers: { "x-api-key": API_KEY } });
const json = await res.json();
// Return a string so the model can read it back as a ToolMessage.
return JSON.stringify(json);
},
{
name: "socialcrawl",
description:
"Fetch live social media, commerce, and review data from SocialCrawl. " +
"Covers TikTok, Instagram, YouTube, LinkedIn, Reddit, Amazon and more. " +
"Set `platform` (e.g. 'tiktok'), `resource` (e.g. 'profile'), and `params` " +
"(the endpoint's query fields, e.g. { handle: 'charlidamelio' }).",
schema: z.object({
platform: z.string().describe("Platform slug, e.g. 'tiktok' or 'youtube'"),
resource: z.string().describe("Endpoint resource, e.g. 'profile' or 'search'"),
params: z
.record(z.string(), z.union([z.string(), z.number()]))
.describe("Query parameters for the endpoint"),
}),
},
);
const model = new ChatAnthropic({ model: "claude-sonnet-4-5" }).bindTools([
socialcrawl,
]);
// One round trip: ask, let the model call the tool, feed the result back.
const messages = [
new HumanMessage("How many followers does @charlidamelio have on TikTok?"),
];
const first = await model.invoke(messages);
messages.push(first);
for (const call of first.tool_calls ?? []) {
const output = await socialcrawl.invoke(call.args as never);
messages.push(new ToolMessage({ content: output, tool_call_id: call.id! }));
}
const final = await model.invoke(messages);
console.log(final.text);Install the packages first (check the LangChain docs for the current versions):
npm install langchain @langchain/core @langchain/anthropic zodWhy one tool instead of one per endpoint?
Every SocialCrawl endpoint shares the same shape: a GET at /v1/{platform}/{resource} with query parameters and one x-api-key header, returning the same JSON envelope. A single tool that takes platform, resource, and params therefore reaches all 325 endpoints without you writing a new tool each time. Point the model at the platform directory or the machine-readable llms.txt so it knows which platform and resource to pass.
How do I keep the agent from spending too many credits?
The tool spends credits only when it makes a live request, billed exactly as the REST API bills: standard endpoints are 1 credit, transcripts and other premium endpoints are 10 credits, and the universal search/everywhere call is a flat 20 credits. Empty results and upstream errors are auto-refunded, and cache hits cost 0 credits. Read credits_remaining from the envelope after each call to track spend, and see Credits and Endpoint pricing for the full table.
Where to go next
- Get a key at socialcrawl.dev (100 free credits) and read Authentication.
- Prefer the Vercel AI SDK? The same pattern in that framework is on the Vercel AI SDK page.
- Using an MCP client instead? Claude Code and Skills & MCP skip the DIY tool entirely.
- Explore the platform directory or start with the Quickstart.
