1. Tutorials
  2. How to scrape TikTok live status
  3. JavaScript

How to scrape TikTok live status with JavaScript

A JavaScript script that fetches TikTok live status and saves them to a file.

Get an API key; the examples use api.socialscrape.dev.

Set up

You need Node 18 or newer. It has fetch built in, so there is nothing to install.

1export SOCIALSCRAPE_API_KEY=ss_live_...

The script

Save as scrape.mjs and run node scrape.mjs. It calls GET /v1/tiktok/profile/live.

1import { writeFileSync } from "node:fs";23const BASE = "https://api.socialscrape.dev/v1/tiktok/profile/live";4const KEY = process.env.SOCIALSCRAPE_API_KEY;5const PARAMS = {6  handle: "khaby.lame",7};89// Follow a path through nested objects; undefined if any step is missing or null.10const pick = (obj, ...path) => path.reduce((o, k) => (o == null ? undefined : o[k]), obj);11const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));1213// One request. Failures cost nothing; retry the ones the API marks retryable.14async function get(params) {15  for (let attempt = 0; attempt < 3; attempt++) {16    const url = new URL(BASE);17    url.search = new URLSearchParams(params).toString();18    const body = await (await fetch(url, { headers: { "x-api-key": KEY } })).json();19    if (body.ok) return body;20    const { code, message, retryable } = body.error;21    if (!retryable || attempt === 2) throw new Error(`${code}: ${message}`);22    await sleep(2 ** attempt * 1000);23  }24}2526const body = await get(PARAMS);27const data = body.data;2829writeFileSync("out.json", JSON.stringify(data, null, 2));30console.log("url:", String(pick(data, "url")).slice(0, 200));31console.log("author.handle:", String(pick(data, "author", "handle")).slice(0, 200));32console.log(`saved out.json, credits left: ${body.credits_remaining}`);

How it works

  1. get() sends one request and retries the failures the API marks retryable. A failed request costs nothing.
  2. The result is one object in data.
  3. pick() reads nested fields and gives undefined instead of crashing when the platform returned null.
  4. The whole object goes to out.json and a few fields are printed.

What you get back

These fields come from a recorded sample. The full field list is on the endpoint page.

SampleFields in the script
1{2  "url": "https://www.tiktok.com/@khaby.lame/live",3  "author": {4    "handle": "khaby.lame"5  }6}

The short version

Without paging, saving or retries, the request is this.

1const url = new URL("https://api.socialscrape.dev/v1/tiktok/profile/live");2url.search = new URLSearchParams({3  handle: "khaby.lame",4}).toString();5const res = await fetch(url, {6  headers: { "x-api-key": process.env.SOCIALSCRAPE_API_KEY },7});8const body = await res.json();910if (body.ok) {11  console.log(body.data);12} else {13  console.error(body.error.code, body.error.message);14}

With the SDK

The JavaScript client does the retries for you: see the SDKs.

1import { SocialScrape } from "socialscrape";23const client = new SocialScrape(); // reads SOCIALSCRAPE_API_KEY45const res = await client.tiktok.profileLive({ handle: "khaby.lame" });6console.log(res.data);7console.log(res.credits_charged, res.credits_remaining);

What it costs

1 credit. A failed request costs nothing. This endpoint is always fetched live, so there is no cached repeat.

Next