> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # TypeScript for agents (/docs/agent-source-of-truth/typescript) The calls to copy when an agent uses SocialCrawl from TypeScript, with a simple and a complex example for each This page is for an agent that writes SocialCrawl calls in TypeScript. Copy an example, change the values, and run it. Each family below shows one simple and one complex call. ## Install Node.js 22.18 or later runs the examples as `.mts` files with `node socialcrawl.mts`. There is nothing to install. On an older Node, run them with `npx tsx socialcrawl.mts`. ## Authenticate Send your key in the `x-api-key` header on every call. Keep it in `SOCIALCRAWL_API_KEY`, out of your code. ```bash title="Terminal" export SOCIALCRAWL_API_KEY=sc_your_key_here ``` Check the key with the balance call. It is free. ```typescript title="socialcrawl.mts" const res = await fetch("https://www.socialcrawl.dev/v1/credits/balance", { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); console.log(await res.json()); ``` ## When to use what Pick the call by the job. Every row links its section. The price of each call is on [Endpoint pricing](/docs/endpoint-pricing.md), and the other calls are in the [API reference](/docs/api-reference.md). | Job | Call | Section | | --- | --- | --- | | Read one account: name, followers, bio | `GET /v1/tiktok/profile` | [Get a profile](#get-a-profile) | | Read an account's recent posts, page by page | `GET /v1/tiktok/profile/videos` | [Get posts one page at a time](#get-posts-one-page-at-a-time) | | Ask one question of many platforms at once | `GET /v1/search/everywhere` | [Search many platforms](#search-many-platforms) | | Price any call before you send it | `GET /v1/utility/estimate` | [Price a call before you send it](#price-a-call-before-you-send-it) | ## Get a profile ### Why use it Use it to read one account. You send a handle and get the name, the follower count and the bio. Other platforms answer in the same shape, so you change the platform in the path and nothing else. ### Preferred call `GET /v1/tiktok/profile`. Pass `handle`. Add `counts=web` for the counts the website shows. ### Simple example One call, no error handling. ```typescript title="socialcrawl.mts" const url = new URL("https://www.socialcrawl.dev/v1/tiktok/profile"); url.searchParams.set("handle", "charlidamelio"); const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); console.log(body.data); ``` ### Complex example Adds more params. A failed call raises an error that names its `error.type`. ```typescript title="socialcrawl.mts" // Send one request. Throw with error.type and error.doc_url when success is false. async function send(url: URL) { const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); if (!body.success) { const { type, message, doc_url } = body.error; throw new Error(`${type}: ${message} (${doc_url})`); } return body; } const url = new URL("https://www.socialcrawl.dev/v1/tiktok/profile"); url.searchParams.set("handle", "charlidamelio"); url.searchParams.set("counts", "web"); const answer = await send(url); console.log(answer.data.author.followers, answer.credits_used); ``` ### Parameters | Name | What it does | | --- | --- | | `handle` | TikTok username without the @ symbol. | | `user_id` | TikTok numeric user ID. | | `counts` | `app` (default) or `web`. | ## Get posts one page at a time ### Why use it Use it to read an account's recent videos. Each answer holds one page. To read the next page, send `pagination.next_cursor` back as `cursor`. Stop when `pagination.has_more` is false. ### Preferred call `GET /v1/tiktok/profile/videos`. Pass `handle`. Add `since` to stop at a date. ### Simple example One call, no error handling. ```typescript title="socialcrawl.mts" const url = new URL("https://www.socialcrawl.dev/v1/tiktok/profile/videos"); url.searchParams.set("since", "2026-09-01"); url.searchParams.set("handle", "charlidamelio"); const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); console.log(body.data.items.length, body.pagination.next_cursor); ``` ### Complex example Reads two pages and stops early when there is no next page. A failed call raises an error that names its `error.type`. ```typescript title="socialcrawl.mts" // Send one request. Throw with error.type and error.doc_url when success is false. async function send(url: URL) { const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); if (!body.success) { const { type, message, doc_url } = body.error; throw new Error(`${type}: ${message} (${doc_url})`); } return body; } const url = new URL("https://www.socialcrawl.dev/v1/tiktok/profile/videos"); url.searchParams.set("handle", "charlidamelio"); for (const page of [1, 2]) { const answer = await send(url); const { next_cursor, has_more } = answer.pagination; console.log(page, answer.data.items.length, next_cursor); if (!has_more) break; url.searchParams.set("cursor", next_cursor); } ``` ### Parameters | Name | What it does | | --- | --- | | `handle` | TikTok username without the @ symbol | | `user_id` | TikTok user id. | | `since` | Only videos published on or after this date: YYYY-MM-DD (midnight UTC) or an ISO 8601 timestamp. | | `cursor` | The `pagination.next_cursor` of the page before. Leave it out for the first page. | ## Search many platforms ### Why use it Use it to ask one question of many platforms in one call. Send it with `dry_run=1` first. A dry run creates nothing, costs nothing and returns the estimate. ### Preferred call `GET /v1/search/everywhere`. Pass `query`. Narrow it with `period` and `sources`. ### Simple example One call, no error handling. ```typescript title="socialcrawl.mts" const url = new URL("https://www.socialcrawl.dev/v1/search/everywhere"); url.searchParams.set("query", "kanye west"); url.searchParams.set("dry_run", "1"); const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); console.log(body.data); ``` ### Complex example Adds more params. A failed call raises an error that names its `error.type`. ```typescript title="socialcrawl.mts" // Send one request. Throw with error.type and error.doc_url when success is false. async function send(url: URL) { const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); if (!body.success) { const { type, message, doc_url } = body.error; throw new Error(`${type}: ${message} (${doc_url})`); } return body; } const url = new URL("https://www.socialcrawl.dev/v1/search/everywhere"); url.searchParams.set("query", "kanye west"); url.searchParams.set("period", "last_month"); url.searchParams.set("sources", "reddit,youtube"); url.searchParams.set("dry_run", "1"); const answer = await send(url); console.log(answer.data.estimate, answer.credits_used); ``` ### Parameters | Name | What it does | | --- | --- | | `query` | The phrase to search on every platform, 1 to 512 characters. | | `period` | A window by name: `Nd` for the last N days, `yesterday`, `last_week`, `last_month`, `this_year` and more. | | `sources` | Optional CSV allowlist of sources (mutually exclusive with exclude). | | `dry_run` | Send `1` to price the search without running it. Free. | ## Price a call before you send it ### Why use it Use it to learn what a call costs before you make it. You name the endpoint and its params. The answer is the price. The call is free. ### Preferred call `GET /v1/utility/estimate`. Pass `id` as `platform/resource`, and `params` as a query string. ### Simple example One call, no error handling. ```typescript title="socialcrawl.mts" const url = new URL("https://www.socialcrawl.dev/v1/utility/estimate"); url.searchParams.set("id", "tiktok/post/comments"); url.searchParams.set("params", "url=https://www.tiktok.com/@stoolpresidente/video/7623818255903329566&label=spam"); const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); console.log(body.data); ``` ### Complex example Adds more params. A failed call raises an error that names its `error.type`. ```typescript title="socialcrawl.mts" // Send one request. Throw with error.type and error.doc_url when success is false. async function send(url: URL) { const res = await fetch(url, { headers: { "x-api-key": process.env.SOCIALCRAWL_API_KEY! }, }); const body = await res.json(); if (!body.success) { const { type, message, doc_url } = body.error; throw new Error(`${type}: ${message} (${doc_url})`); } return body; } const url = new URL("https://www.socialcrawl.dev/v1/utility/estimate"); url.searchParams.set("id", "tiktok/profile/videos"); url.searchParams.set("params", "handle=charlidamelio"); url.searchParams.set("items", "100"); const answer = await send(url); console.log(answer.data, answer.credits_used); ``` ### Parameters | Name | What it does | | --- | --- | | `id` | The endpoint to price, as platform/resource (tiktok/post/comments) or its /v1 path, optionally with its query string. | | `params` | The endpoint's own params, as a URL-encoded query string (url=...&label=spam) or a JSON object. | | `items` | Rows you want. |