toktokdata.live

Last updated

SDKs and samples

Official client libraries for TypeScript and JavaScript, Python and Go wrap the REST API with authentication, retries, pagination and typed errors. Samples in eight languages show common tasks end to end.

Install

The SDKs, the MCP server, the samples and the OpenAPI file live in one open repository, MKK-Platform/toktokdata-sdks. Each SDK follows semantic versioning; the API path /v1 only grows in compatible ways.

npm install @toktokdata.live/sdk

First request

Create a key under Settings → API keys (see Authentication) and put it in the TOKTOKDATA_API_KEY environment variable. This reads the hourly board for Vietnam:

import { TokTokData } from "@toktokdata.live/sdk"; const client = new TokTokData({ apiKey: process.env.TOKTOKDATA_API_KEY }); const board = await client.rankings.official("hourly", { region: "VN", limit: 10 });console.log(board.data);

Responses are the API’s JSON unchanged: reads return data and provenance, as described in the reference.

What the SDKs handle

FeatureBehaviour
AuthenticationSends Authorization: Bearer <key>. The key comes from the constructor or from TOKTOKDATA_API_KEY; without one the client fails at construction.
RetriesRetries network errors, 429 and retryable 5xx answers up to twice, waiting for Retry-After when it is sent. Never retries 402, other 4xx or daily_quota_exceeded.
Fresh dataWaits on 202 “still collecting” answers and asks again until the data is ready, for up to 3 minutes.
PaginationIterators that follow nextCursor page by page.
ErrorsOne error type with status, code, message, requestId and retryable.
Rate limitsThe X-RateLimit-* headers of the last response, to slow down before a 429.
WebhooksA helper that checks the x-toktokdata-signature of an alert delivery.

Resources

Methods are grouped by resource, with the same names in every language: camelCase in TypeScript, snake_case in Python, PascalCase in Go. Path parameters are positional; query parameters and bodies go in one options object (a params struct in Go).

NamespaceCoversScope
rankingsOfficial boards, regions, board history and daily bars.rank:read
creatorsSearch, profile, analysis, changes, followers, following, top gifters, insights and videos.creator:read · gifter:read · content:read
agenciesAgency directory and one agency.creator:read
insightsBest time to go LIVE and hourly LIVE presence per region.creator:read
videosOne video and its comments.content:read
trendsRegional trends.trend:read
giftsGift catalog.live:read
giftersTop gifters by window, segment and region.gifter:read
liveLIVE sessions, timelines, comparisons, creator performance, refresh, playback and chat.live:read · live:stream
alertsAlert rules, test deliveries, fires, deliveries and stats.webhook:manage
exportsCreate, poll and download exports.export
accountEntitlements and usage of the workspace.keys:manage

Errors and retries

Every failed call raises one error type: TokTokDataError in TypeScript and Python, *APIError in Go. It carries the error envelope and the HTTP status. Retries already happened by the time you see it, so a retryable error here means the API kept failing.

import { TokTokDataError } from "@toktokdata.live/sdk"; try {  await client.creators.get("room.one");} catch (err) {  if (err instanceof TokTokDataError) {    console.error(err.status, err.code, err.requestId, err.retryable);  }  throw err;}

Waiting for fresh data

Creator profiles, followers, following, video comments and LIVE playback may answer 202 while fresh data is collected. The SDK methods for these routes sleep for the advised retryAfterSeconds and ask again until the data arrives, for up to 3 minutes, then raise a timeout error. A 202 costs no more than a cached read. Pass wait: false (Python wait=False) to get the 202 body back and poll yourself.

Pagination

Cursor lists (followers, following, LIVE sessions, LIVE performance, board history and video comments) accept cursor and limit and return data.nextCursor. Each SDK has an iterator that follows the cursor and yields whole pages until nextCursor is null; see Pagination.

Webhook signatures

verifyWebhook (Python verify_webhook, Go VerifyWebhook) takes the raw body, the headers and the channel’s whsec_… secret, checks the HMAC in constant time, rejects timestamps older than five minutes and returns the parsed payload. The format is described under Webhooks.

Samples

The examples/ folder of the repository has runnable samples. Each one reads the key from TOKTOKDATA_API_KEY.

FolderLanguageUses
examples/curlShellcurl
examples/typescriptTypeScript@toktokdata.live/sdk
examples/pythonPythontoktokdata
examples/goGotoktokdata-sdks/go
examples/javaJavaPlain HTTP, standard library
examples/csharpC#Plain HTTP, standard library
examples/phpPHPPlain HTTP, standard library
examples/rubyRubyPlain HTTP, standard library

The samples cover these tasks:

  • Leaderboard: print the top creators of an official board.
  • Creator report: read a profile and wait while fresh data is collected (202).
  • CSV export: create an export, wait until it is ready and download it.
  • Webhook server: receive alert deliveries and verify their signature.

Other languages

SDKs for Java, C#, PHP and Ruby are planned. Until then, the samples for those languages call the API over plain HTTP, and openapi/openapi.json in the repository describes every public route if you prefer to generate a client.

Was this page helpful?