Use SocialCrawl with LangGraph
Give a LangGraph agent social media data, with the price shown before it spends.
LangGraph runs the same LangChain tools as any other LangChain agent, so two small functions give a LangGraph agent SocialCrawl data. SocialCrawl publishes no LangGraph or LangChain package, and neither needs one.
Setup
Install LangChain, LangGraph, a model integration and requests, then set your keys. Create the SocialCrawl key in the dashboard under API Keys; it looks like sc_your_key_here.
pip install langchain langgraph langchain-anthropic requests
export SOCIALCRAWL_API_KEY=sc_your_key_here
export ANTHROPIC_API_KEY=your_anthropic_keyConnect
LangChain's create_agent builds a compiled LangGraph graph with a model node and a tool node, so it is the quickest start. Wrap each function with tool, and the docstring becomes the tool description.
import os
import requests
BASE = "https://www.socialcrawl.dev"
HEADERS = {"x-api-key": os.environ["SOCIALCRAWL_API_KEY"]}
def estimate_cost(endpoint: str, params: dict[str, str | int]) -> dict:
"""Quote the credits a call will hold. Free. endpoint looks like tiktok/profile."""
r = requests.get(
f"{BASE}/v1/utility/estimate",
headers=HEADERS,
params={"id": endpoint, **params},
timeout=30,
)
body = r.json()
return body.get("data", body)
def call_endpoint(platform: str, resource: str, params: dict[str, str | int]) -> dict:
"""Run one SocialCrawl call, for example tiktok and profile with a handle."""
r = requests.get(
f"{BASE}/v1/{platform}/{resource}", headers=HEADERS, params=params, timeout=60
)
body = r.json()
if body.get("success"):
return body["data"]
# Give the model the error type, not the whole envelope.
err = body.get("error", {})
return {"error": err.get("type"), "message": err.get("message"), "status": r.status_code}
from langchain.agents import create_agent
from langchain.tools import tool
agent = create_agent(
model="anthropic:claude-sonnet-5",
tools=[tool(estimate_cost), tool(call_endpoint)],
system_prompt=(
"Call estimate_cost and report the hold before any call_endpoint. "
"If a call returns INSUFFICIENT_CREDITS, tell the user and stop."
),
)Read a profile and its posts
Quote the call yourself, then run the agent. recursion_limit is the step limit, and the run stops with an error when the graph reaches it.
quote = estimate_cost("tiktok/profile", {"handle": "nasa"})
print("hold:", quote["hold"], "valid:", quote["valid"])
result = agent.invoke(
{"messages": [{"role": "user", "content": "Read the TikTok profile for nasa and its latest videos, then summarise them."}]},
{"recursion_limit": 10},
)
print(result["messages"][-1].content)Keep spend in check
Call estimate_cost before any paid call and show the answer to the person who owns the key. recursion_limit caps the graph, and max_credits stops a list walk where you set it. A 402 means the account is out of credits. Tell the user it is an offer to top up and stop, because a retry cannot succeed. See Errors and Credits.
Add a standing watch as a graph step
A Monitor repeats one call on a schedule. This graph quotes it for free with dry_run, and a second node creates it only when approved is true. Run it once with False to read the quote, then again with True after the user agrees. Monitors explains the body and what each run costs.
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
BODY = {
"name": "NASA followers",
"recipe": "tiktok/profile",
"params": {"handle": "nasa"},
"cadence": "daily",
"track": {"metrics": ["author.followers"]},
}
class WatchState(TypedDict, total=False):
approved: bool
quote: dict
monitor_id: str
def quote_watch(state: WatchState) -> dict:
r = requests.post(f"{BASE}/v1/monitors", params={"dry_run": 1}, headers=HEADERS, json=BODY, timeout=30)
return {"quote": r.json().get("data", {}).get("estimate", {})}
def create_watch(state: WatchState) -> dict:
r = requests.post(f"{BASE}/v1/monitors", headers=HEADERS, json=BODY, timeout=30)
body = r.json()
monitor = body.get("monitor") or body.get("data", {}).get("monitor", {})
return {"monitor_id": monitor.get("id", "")}
graph = StateGraph(WatchState)
graph.add_node("quote", quote_watch)
graph.add_node("create", create_watch)
graph.add_edge(START, "quote")
graph.add_conditional_edges("quote", lambda s: "create" if s.get("approved") else END, ["create", END])
graph.add_edge("create", END)
watch = graph.compile()
print(watch.invoke({"approved": False})["quote"])