Use SocialCrawl with Anthropic
Give an Anthropic agent social media data, with the price shown before it spends.
The Anthropic Messages API gives your agent SocialCrawl tools through its tool use loop, and SocialCrawl already serves some tool definitions in Anthropic's own format. No SocialCrawl package is published, so you write the loop once and it covers every tool.
Setup
Install the Anthropic client and requests, then set both keys. Create your SocialCrawl key in the dashboard under API Keys; it looks like sc_your_key_here.
pip install anthropic requests
export SOCIALCRAWL_API_KEY=sc_your_key_here
export ANTHROPIC_API_KEY=your_anthropic_keyConnect
Pass a list of tools with name, description and input_schema. When stop_reason is tool_use, run each requested tool against the REST API and send the result back as a tool_result block in a user message.
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) -> 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) -> 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}
import json
from anthropic import Anthropic
client = Anthropic()
MODEL = "claude-sonnet-5-5"
SYSTEM = "Call estimate_cost and report the hold before any call_endpoint. If a call returns INSUFFICIENT_CREDITS, tell the user and stop."
TOOLS = [
{
"name": "estimate_cost",
"description": "Quote the credits a SocialCrawl call will hold. Free. Call it before call_endpoint and show the user the answer.",
"input_schema": {"type": "object",
"properties": {
"endpoint": {"type": "string", "description": "platform/resource, for example tiktok/profile"},
"params": {"type": "object", "description": "The endpoint's query parameters, for example {\"handle\": \"nasa\"}"},
},
"required": ["endpoint", "params"]},
},
{
"name": "call_endpoint",
"description": "Run one SocialCrawl call to read public social media data. Paid, so quote it first. A 402 means out of credits: tell the user and stop.",
"input_schema": {"type": "object",
"properties": {
"platform": {"type": "string", "description": "For example tiktok"},
"resource": {"type": "string", "description": "For example profile or profile/videos"},
"params": {"type": "object", "description": "The endpoint's query parameters"},
},
"required": ["platform", "resource", "params"]},
},
]
FUNCS = {"estimate_cost": estimate_cost, "call_endpoint": call_endpoint}
def run(prompt: str, tools=TOOLS, funcs=FUNCS, max_steps: int = 5) -> str:
messages = [{"role": "user", "content": prompt}]
for _ in range(max_steps):
resp = client.messages.create(
model=MODEL, max_tokens=1024, system=SYSTEM, messages=messages, tools=tools
)
if resp.stop_reason != "tool_use":
return "".join(b.text for b in resp.content if b.type == "text")
messages.append({"role": "assistant", "content": resp.content})
results = [
{
"type": "tool_result",
"tool_use_id": b.id,
"content": json.dumps(funcs[b.name](**b.input)),
}
for b in resp.content
if b.type == "tool_use"
]
messages.append({"role": "user", "content": results})
return "Stopped at the step limit."Read a profile and its posts
Quote the call first, then let the model run it. The estimate is free and its hold is the most the call can take.
quote = estimate_cost("tiktok/profile", {"handle": "nasa"})
print("hold:", quote["hold"], "valid:", quote["valid"])
print(run("Read the TikTok profile for nasa and its latest videos, then summarise them."))Keep spend in check
Call estimate_cost before any paid call and show the answer to the person who owns the key. Keep max_steps small, and send max_credits on list endpoints so a walk stops 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.
Use our tool files
SocialCrawl serves ready-made tool definitions at /agent-tools/<name>.json, with no key needed to fetch them. Each file is a single tool in the format above, so it goes straight into tools. This one finds creators who post about a topic; its description tells the model to run a free dry_run first and to relay next_step to the user.
creators = requests.get(f"{BASE}/agent-tools/socialcrawl_find_creators.json", timeout=30).json()
def flat(args: dict) -> dict:
out = {}
for key, value in args.items():
if isinstance(value, list):
out[key] = ",".join(map(str, value))
elif isinstance(value, dict):
out[key] = json.dumps(value)
elif isinstance(value, bool):
out[key] = str(value).lower()
else:
out[key] = value
return out
def find_creators(**args) -> dict:
r = requests.get(f"{BASE}/v1/prism/creators", headers=HEADERS, params=flat(args), timeout=60)
return r.json()
print(run(
"Find home espresso creators on TikTok. Dry run first and show me the estimate.",
tools=[creators],
funcs={creators["name"]: find_creators},
))Creator search explains the request fields and how a larger run continues in the background.
