Upload a video and schedule it
This script takes a video file on your computer to a scheduled TikTok post in one run: it gets an upload link, uploads the file, schedules the post and follows it until TikTok's scheduler has it. The Quickstart walks through the same calls one at a time with curl.
There is no test mode. The post goes to TikTok on your real account. Cancel it before it goes out if you don't want it.
Run it
You need an API key with the posts:write and posts:read scopes, the id of an account that can post (from GET /accounts), and an MP4, MOV or WebM video.
export TIKTORNADO_API_KEY=<your key>
node upload-and-schedule.mjs j971x2c4ajrq0w8sd5e8fxz3a57v2k8m leg-day.mp4 2026-12-02T15:00:00Z "Leg day, no excuses #fitness"The time is ISO 8601 with a time zone, more than 10 minutes from now. It prints the post's id, the time it was saved for and its status:
Saved post k174wq9nrm6t2f0bd3xh8pz5a87v1c2e for 2026-12-02T15:00:00.000Z
Status: scheduledThe Python script runs the same way, python3 upload_and_schedule.py with the same arguments. It uses only the standard library, so there is nothing to install.
The code
// Upload a video from your computer, schedule it on one TikTok account, and follow its
// status. Node.js 22 or later, nothing to install.
//
// export TIKTORNADO_API_KEY=<your key>
// node upload-and-schedule.mjs <accountId> <video> <time> [caption]
//
// <time> is ISO 8601 with a time zone, more than 10 minutes ahead: 2026-12-02T15:00:00Z
import { randomUUID } from "node:crypto";
import { openAsBlob } from "node:fs";
import { basename, extname } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
const API = process.env.TIKTORNADO_API_URL ?? "https://api.tiktornado.com/api/v1";
const KEY = process.env.TIKTORNADO_API_KEY;
// api() tries a request at most this many times, waiting this long between tries
// unless the answer says how long (Retry-After).
const ATTEMPTS = 5;
const RETRY_WAIT_MS = 5000;
// How often, and how many times, to check the new post's status.
const STATUS_CHECKS = 30;
const STATUS_WAIT_MS = 10_000;
const VIDEO_TYPES = { ".mp4": "video/mp4", ".mov": "video/quicktime", ".webm": "video/webm" };
class ApiError extends Error {
constructor(status, error) {
super(`${status} ${error.reason}: ${error.message}`);
this.status = status;
this.reason = error.reason;
}
}
// One API call. On 429, 409, 5xx or a dropped connection it waits and sends the exact
// same request again, Idempotency-Key and body included, so a change is never made twice.
async function api(method, path, body, idempotencyKey) {
const headers = { Authorization: `Bearer ${KEY}` };
if (body !== undefined) headers["Content-Type"] = "application/json";
if (idempotencyKey) headers["Idempotency-Key"] = idempotencyKey;
const text = body === undefined ? undefined : JSON.stringify(body);
for (let attempt = 1; ; attempt++) {
let res;
try {
res = await fetch(API + path, { method, headers, body: text });
} catch (error) {
if (attempt === ATTEMPTS) throw error;
await sleep(RETRY_WAIT_MS);
continue;
}
const answer = await res.json().catch(() => null);
if (res.ok) return answer;
const error = answer?.error ?? { reason: "unknown_error", message: `HTTP ${res.status}` };
if (attempt === ATTEMPTS || !(res.status === 429 || res.status === 409 || res.status >= 500)) throw new ApiError(res.status, error);
await sleep(res.status === 429 ? Number(res.headers.get("retry-after") ?? 60) * 1000 : RETRY_WAIT_MS);
}
}
const [accountId, file, scheduledFor, caption = ""] = process.argv.slice(2);
const contentType = VIDEO_TYPES[extname(file ?? "").toLowerCase()];
if (!KEY || !accountId || !file || !scheduledFor || !contentType) {
console.error("Set TIKTORNADO_API_KEY, then: node upload-and-schedule.mjs <accountId> <video.mp4|.mov|.webm> <time> [caption]");
process.exit(1);
}
try {
// An upload link for this one file. It needs no Idempotency-Key: a repeat gives a new link.
const video = await openAsBlob(file);
const link = await api("POST", "/uploads", { accountId, filename: basename(file), contentType, size: video.size });
// The file goes to the link with only the headers the answer lists, never your key.
const upload = await fetch(link.uploadUrl, { method: link.method, headers: link.headers, body: video });
if (!upload.ok) throw new Error(`The upload failed with HTTP ${upload.status}. Ask for a new link and try again.`);
// The post, with a new Idempotency-Key: a retry of this request can't post twice.
const post = await api("POST", "/posts", { accountId, kind: "video", caption, mediaRefs: [link.mediaRef], scheduledFor }, randomUUID());
console.log(`Saved post ${post.id} for ${post.scheduledFor}`);
// scheduling until TikTok's scheduler has the post, then scheduled.
let current = await api("GET", `/posts/${post.id}`);
for (let check = 1; current.status === "scheduling" && check <= STATUS_CHECKS; check++) {
await sleep(STATUS_WAIT_MS);
current = await api("GET", `/posts/${post.id}`);
}
console.log(`Status: ${current.status}`);
if (current.status === "failed") console.log(`Why: ${current.error.reason}: ${current.error.message}`);
} catch (error) {
console.error(error.message);
process.exit(1);
}How it works
- The upload link.
POST /uploadstakes the file's name, type and size in bytes, and answers anuploadUrland amediaRef. It needs noIdempotency-Key: asking again simply gives a new link. - The upload. The file goes to
uploadUrlwith an HTTPPUTand only the headers the answer lists. Your API key never goes there. The link expires, so upload soon after you get it, and post soon after you upload. - The post.
POST /postswith themediaRefand a newIdempotency-Key. On a429,409,5xxor a dropped connection,api()waits and sends the same request again with the same key, so the post is never made twice. See Safe retries. - The status. The answer's status is
schedulinguntil TikTok's scheduler has the post. The script reads the post withGET /posts/{postId}every 10 seconds until it isn't. Once it isscheduledit goes out at its time:publishing, thenpublished. See Post statuses.
What can go wrong
The script prints the refusal's status, reason and message, and exits with an error.
| Reason | What to do |
|---|---|
schedule_too_soon | Pick a time more than 10 minutes from now |
daily_limit_reached | The account already has 15 videos in some 24 hours around that time. Pick another time |
past_account_end | The account ends before that time. Pick an earlier one |
file_type_not_supported, file_too_large | Use an MP4, MOV or WebM video up to 4 GB |
link_unreachable, file_damaged | The uploaded file couldn't be read. Check the upload finished, or export the video again and upload it again |
upload_expired | Upload the file again, then schedule it soon after |
account_not_ready, invalid_state | The account can't take posts right now. Check its status with GET /accounts |
api_scope_missing | The key needs posts:write and posts:read |
slow_down | api() waits the Retry-After seconds and tries again. See Rate limits |
Every reason is on Errors.