> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # Mastra quickstart (/docs/quickstart/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. Get a free API key at https://www.socialcrawl.dev/dashboard/api. New accounts start with 100 welcome credits and no card is needed. ## Set up Install Mastra and zod, then put both keys in `.env`. ```bash title="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. ```typescript title="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) { 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. ```typescript title="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. ```typescript title="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](/docs/skills-and-mcp.md) instead. ```bash title="Terminal" npx mastra dev ``` ## Next steps - [Price a watch for free](/docs/engines/monitors.md): POST /v1/monitors?dry_run=1 quotes a scheduled watch of an account and creates nothing. - [Common jobs](/docs/common-jobs.md): The first call for each job, from profiles to comments and reviews. - [Search](/docs/search.md): Choose between one platform - [Errors](/docs/errors.md): Every error type, what it means, and which ones to retry.