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 passapiKey. Akd_test_key returns free sample data. - The samples use ES modules with top-level
await: save them as.mjsor set"type": "module"in package.json. - If a live key is used from a browser, the API adds
meta.warningto 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);
}
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 withlimitandmaxPages.- It retries
429,5xx, timeouts and network errors twice by default (maxRetries), waiting forRetry-Afterup 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));
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.
| Tutorial | Route |
|---|---|
| Profile | GET /v1/instagram/users/{user} |
| Account details | GET /v1/instagram/users/{user}/account-details |
| Similar accounts | GET /v1/instagram/users/{user}/related |
| Followers | GET /v1/instagram/users/{user}/followers |
| Following | GET /v1/instagram/users/{user}/following |
| Posts | GET /v1/instagram/users/{user}/posts |
| Reels | GET /v1/instagram/users/{user}/reels |
| Tagged posts | GET /v1/instagram/users/{user}/tagged |
| Reposts | GET /v1/instagram/users/{user}/reposts |
| Post or reel | GET /v1/instagram/posts/{post} |
| Post stats (batch) | GET /v1/instagram/posts/stats |
| Comments | GET /v1/instagram/posts/{post}/comments |
| Comment replies | GET /v1/instagram/comments/{comment}/replies |
| Post likers | GET /v1/instagram/posts/{post}/likers |
| Comment likers | GET /v1/instagram/comments/{comment}/likers |
| Stories | GET /v1/instagram/users/{user}/stories |
| Story by id | GET /v1/instagram/stories/{story} |
| Highlights | GET /v1/instagram/users/{user}/highlights |
| Highlight stories | GET /v1/instagram/highlights/{highlight} |
| Search hashtags | GET /v1/instagram/search/hashtags |
| Hashtag | GET /v1/instagram/hashtags/{tag} |
| Hashtag posts | GET /v1/instagram/hashtags/{tag}/posts |
| Search places | GET /v1/instagram/search/places |
| Place | GET /v1/instagram/places/{place} |
| Place posts | GET /v1/instagram/places/{place}/posts |
| Search audio | GET /v1/instagram/search/audio |
| Trending audio | GET /v1/instagram/audio/trending |
| Audio | GET /v1/instagram/audio/{audio} |
| Reels using audio | GET /v1/instagram/audio/{audio}/reels |
| Search accounts | GET /v1/instagram/search/users |
| URL resolver | GET /v1/instagram/resolve |
TikTok
| Tutorial | Route |
|---|---|
| Profile | GET /v1/tiktok/users/{user} |
| Suggested accounts | GET /v1/tiktok/users/{user}/suggested |
| Followers | GET /v1/tiktok/users/{user}/followers |
| Following | GET /v1/tiktok/users/{user}/following |
| Posts | GET /v1/tiktok/users/{user}/posts |
| Reposts | GET /v1/tiktok/users/{user}/reposts |
| Liked videos | GET /v1/tiktok/users/{user}/liked |
| Playlists | GET /v1/tiktok/users/{user}/playlists |
| Stories | GET /v1/tiktok/users/{user}/stories |
| Video or photo post | GET /v1/tiktok/posts/{post} |
| Comments | GET /v1/tiktok/posts/{post}/comments |
| Comment replies | GET /v1/tiktok/comments/{comment}/replies |
| Transcript | GET /v1/tiktok/posts/{post}/transcript |
| Sound | GET /v1/tiktok/music/{music} |
| Posts using a sound | GET /v1/tiktok/music/{music}/posts |
| Hashtag | GET /v1/tiktok/hashtags/{tag} |
| Hashtag posts | GET /v1/tiktok/hashtags/{tag}/posts |
| Search | GET /v1/tiktok/search |
Node.js: questions
Is there an npm package for the Instagram and TikTok API?
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?
How many requests can I run at once from Node.js?
Retry-After handling stays inside it; a 429 answer is free.Related
Instagram and TikTok API in Python
Two ways to call HandleHaul from Python: a 60-line client on requests that you own, or the official handlehaul package (sync and async).
Read more →Instagram and TikTok API in PHP
One class, PHP 8 and the curl extension: no Composer package needed.
Read more →Instagram and TikTok API in Go
A standard-library Go client with generic Page[T] and One[T] types: decode the fields you need into structs, page with a callback, and get a typed APIError for anything else.
Read more →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.