Tutorial · Node.js

Instagram and TikTok API in Node.js

Call HandleHaul from Node.js 18 or newer with the built-in fetch, or install the typed handlehaul package from npm, which also ships a CLI. Server side only: the key spends your credits.

Short answer. npm install handlehaul, set HANDLEHAUL_API_KEY, then await new HandleHaul().tiktok.profile("mila.studio"). Without packages, the fetch client below does the same with retries and cursor paging. Keep the key on the server, never in a browser or app bundle.

Facts checked 2 December 2026. Prices change: follow the links before you decide.

Set up

  • Node.js 18 or newer (for the global fetch). Export the key: export HANDLEHAUL_API_KEY=kd_live_..., or put it in your secrets manager and pass apiKey. A kd_test_ key returns free sample data.
  • The samples use ES modules with top-level await: save them as .mjs or set "type": "module" in package.json.
  • If a live key is used from a browser, the API adds meta.warning to every answer and the SDK logs it. Call the API from a route handler, server action, worker or script.

A client on fetch

client.get() makes one call with a 60-second timeout, waits out 429 using Retry-After and retries 502, 503 and 504 with a growing wait (all free). Other errors throw HandleHaulError with status, code and requestId.

client.paginate() is an async generator: for await over it, and break when you have enough, which stops further calls. The demo reads a TikTok profile first and pages with its sec_uid, which never costs a lookup credit.

handlehaul-client.mjsRun: node handlehaul-client.mjs

// A small HandleHaul API client for Node.js 18+ (built-in fetch): retries, cursor paging and clear errors.
//   export HANDLEHAUL_API_KEY=kd_live_...   (or a free kd_test_ key for sample data)
//   node handlehaul-client.mjs
const API = "https://api.handlehaul.com/v1";
const RETRY = new Set([429, 502, 503, 504]); // never charged; other 4xx answers will not change on a retry

export class HandleHaulError extends Error {
  constructor(status, code, message, requestId) {
    super(`${status} ${code}: ${message}`);
    Object.assign(this, { status, code, requestId });
  }
}

export class Client {
  constructor({ apiKey = process.env.HANDLEHAUL_API_KEY, maxTries = 5 } = {}) {
    if (!apiKey) throw new Error("Set HANDLEHAUL_API_KEY");
    this.apiKey = apiKey;
    this.maxTries = maxTries;
    this.credits = 0; // what this client spent, from meta.credits_charged
  }

  /** One call. Retries 429 (after Retry-After) and 502/503/504 (growing wait). */
  async get(path, params = {}) {
    const url = new URL(API + path);
    for (const [k, v] of Object.entries(params)) if (v != null) url.searchParams.set(k, String(v));
    for (let attempt = 0; attempt < this.maxTries; attempt++) {
      const res = await fetch(url, { headers: { "X-Api-Key": this.apiKey }, signal: AbortSignal.timeout(60_000) });
      if (RETRY.has(res.status) && attempt < this.maxTries - 1) {
        const wait = Number(res.headers.get("retry-after")) || 2 ** attempt + Math.random();
        await new Promise((r) => setTimeout(r, wait * 1000));
        continue;
      }
      const body = await res.json();
      if (!res.ok) throw new HandleHaulError(res.status, body.error?.code, body.error?.message, body.error?.request_id);
      this.credits += body.meta?.credits_charged ?? 0;
      return body;
    }
    throw new HandleHaulError(429, "rate_limited", "still limited after retries");
  }

  /** Every row of a list endpoint, one page (one call) at a time. Break out of the loop to stop paying. */
  async *paginate(path, params = {}, { maxPages = 10 } = {}) {
    const p = { ...params };
    for (let page = 0; page < maxPages; page++) {
      const body = await this.get(path, p);
      yield* body.data;
      if (!body.pagination.has_more) return;
      p.cursor = body.pagination.next_cursor;
    }
  }
}

// Demo when run directly.
if (import.meta.url === `file://${process.argv[1]}`) {
  const hh = new Client();
  const { data: profile } = await hh.get("/tiktok/users/mila.studio");
  console.log(profile.handle, profile.counts.followers, "followers");
  // sec_uid skips the handle lookup on every list call.
  for await (const post of hh.paginate(`/tiktok/users/${profile.sec_uid}/posts`, {}, { maxPages: 2 })) {
    console.log(post.created_at, post.counts.views, post.url);
  }
  try {
    await hh.get("/tiktok/users/private_demo/followers");
  } catch (e) {
    if (!(e instanceof HandleHaulError)) throw e;
    console.log("expected error:", e.code);
  }
  console.log("credits used:", hh.credits);
}
Tested 2026-12-02 against a local mock serving the documented API responses (retry on 429, cursor paging, a private-account error).

The official SDK and CLI

  • import { HandleHaul } from "handlehaul". Types for every response ship with the package. Methods follow the routes (hh.instagram.followers, hh.tiktok.postComments, hh.tiktok.transcript); list methods return one page, and .iterate() / .pages() walk the cursor with limit and maxPages.
  • It retries 429, 5xx, timeouts and network errors twice by default (maxRetries), waiting for Retry-After up to 30 seconds (maxRetryAfter). The same package has a CLI: npx handlehaul instagram followers natgeo --pages 3.
  • Below: several TikTok videos at once with at most 5 calls in flight, failed calls recorded instead of stopping the batch.

handlehaul-batch.mjsInstall: npm install handlehaulRun: node handlehaul-batch.mjs

// Many TikTok videos at once with the official SDK (npm install handlehaul), 5 calls in flight at most.
//   export HANDLEHAUL_API_KEY=kd_live_...
//   node handlehaul-batch.mjs
import { HandleHaul, HandleHaulError } from "handlehaul";

const hh = new HandleHaul({ maxRetries: 3 }); // retries 429 and 5xx using Retry-After
const VIDEOS = [
  "7300000000000000123",
  "https://www.tiktok.com/@mila.studio/video/7300004256287789000",
  "7300000000000000456",
];

// Run fn over items with at most `size` promises in flight.
async function mapLimit(items, size, fn) {
  const out = new Array(items.length);
  let next = 0;
  await Promise.all(Array.from({ length: size }, async () => {
    while (next < items.length) {
      const i = next++;
      out[i] = await fn(items[i]);
    }
  }));
  return out;
}

const rows = await mapLimit(VIDEOS, 5, async (video) => {
  try {
    const { data, meta } = await hh.tiktok.post(video);
    return { video, views: data.counts.views, credits: meta.credits_charged };
  } catch (e) {
    if (!(e instanceof HandleHaulError)) throw e;
    return { video, error: e.code, credits: 0 }; // failed calls are free
  }
});
for (const r of rows) console.log(r.video, r.views ?? r.error);
console.log("credits used:", rows.reduce((n, r) => n + r.credits, 0));
Tested 2026-12-02 against a local mock serving the documented API responses (retry on 429, cursor paging, a private-account error).

Every endpoint in Node.js

Each page below has a complete Node.js program for one route, plus the SDK version, the parameters, a sample response and what it costs. The link opens the Node.js tab.

Instagram

Instagram endpoint tutorials
TutorialRoute
ProfileGET /v1/instagram/users/{user}
Account detailsGET /v1/instagram/users/{user}/account-details
Similar accountsGET /v1/instagram/users/{user}/related
FollowersGET /v1/instagram/users/{user}/followers
FollowingGET /v1/instagram/users/{user}/following
PostsGET /v1/instagram/users/{user}/posts
ReelsGET /v1/instagram/users/{user}/reels
Tagged postsGET /v1/instagram/users/{user}/tagged
RepostsGET /v1/instagram/users/{user}/reposts
Post or reelGET /v1/instagram/posts/{post}
Post stats (batch)GET /v1/instagram/posts/stats
CommentsGET /v1/instagram/posts/{post}/comments
Comment repliesGET /v1/instagram/comments/{comment}/replies
Post likersGET /v1/instagram/posts/{post}/likers
Comment likersGET /v1/instagram/comments/{comment}/likers
StoriesGET /v1/instagram/users/{user}/stories
Story by idGET /v1/instagram/stories/{story}
HighlightsGET /v1/instagram/users/{user}/highlights
Highlight storiesGET /v1/instagram/highlights/{highlight}
Search hashtagsGET /v1/instagram/search/hashtags
HashtagGET /v1/instagram/hashtags/{tag}
Hashtag postsGET /v1/instagram/hashtags/{tag}/posts
Search placesGET /v1/instagram/search/places
PlaceGET /v1/instagram/places/{place}
Place postsGET /v1/instagram/places/{place}/posts
Search audioGET /v1/instagram/search/audio
Trending audioGET /v1/instagram/audio/trending
AudioGET /v1/instagram/audio/{audio}
Reels using audioGET /v1/instagram/audio/{audio}/reels
Search accountsGET /v1/instagram/search/users
URL resolverGET /v1/instagram/resolve

TikTok

TikTok endpoint tutorials
TutorialRoute
ProfileGET /v1/tiktok/users/{user}
Suggested accountsGET /v1/tiktok/users/{user}/suggested
FollowersGET /v1/tiktok/users/{user}/followers
FollowingGET /v1/tiktok/users/{user}/following
PostsGET /v1/tiktok/users/{user}/posts
RepostsGET /v1/tiktok/users/{user}/reposts
Liked videosGET /v1/tiktok/users/{user}/liked
PlaylistsGET /v1/tiktok/users/{user}/playlists
StoriesGET /v1/tiktok/users/{user}/stories
Video or photo postGET /v1/tiktok/posts/{post}
CommentsGET /v1/tiktok/posts/{post}/comments
Comment repliesGET /v1/tiktok/comments/{comment}/replies
TranscriptGET /v1/tiktok/posts/{post}/transcript
SoundGET /v1/tiktok/music/{music}
Posts using a soundGET /v1/tiktok/music/{music}/posts
HashtagGET /v1/tiktok/hashtags/{tag}
Hashtag postsGET /v1/tiktok/hashtags/{tag}/posts
SearchGET /v1/tiktok/search

Node.js: questions

Is there an npm package for the Instagram and TikTok API?
Yes: npm install handlehaul. It is the official JavaScript/TypeScript SDK and CLI, works on Node.js 18+, Bun, Deno and edge runtimes with fetch, and reads HANDLEHAUL_API_KEY by default.
Can I call the API from the browser or React Native?
No: the key spends your credits, and anyone can read a key shipped to a browser or app. Call the API from your server and send your own front end only what it needs.
How many requests can I run at once from Node.js?
Your account has a requests-per-second limit and a cap on calls in flight, shared by all its keys. A small pool (5 at a time) with Retry-After handling stays inside it; a 429 answer is free.

Build it in Node.js

One key for Instagram and TikTok. $1.40 to $0.80 per 1,000 calls; failed calls and cache hits are free.

Want to test first? Sandbox keys are free and unlimited, with sample data in the live schema.