Skip to content

Waiting for a read

Hold the request while a read runs, then ask again until it’s ready or failed.

A post WhyViral has already read comes back at once. A new read takes longer, so send wait to have the request hold the answer, for up to 20 seconds.

If the read is still running when the wait ends, you get a 202 with status: "pending". Ask for GET /reads/{id} with wait until the status is "ready" or "failed":

  • post fills in once the post is fetched.
  • read fills in once it’s ready.
  • A read that runs too long fails as TIMEOUT and is refunded.
202 responsejson
{
  "id": "j57d3k9x2v8q1m4n6b0c5w7h2r9t3y8a",
  "object": "read",
  "status": "pending",
  "url": "https://www.tiktok.com/@example.kitchen/video/7430000000000000001",
  "finished_at": null,
  "error": null,
  "post": null,
  "read": null
}

The answer carries every field of the read object; this one is trimmed.

In code

Node.jsjs
const base = "https://whyviral.ai/api/v1";
const headers = {
  Authorization: `Bearer ${process.env.WHYVIRAL_API_KEY}`,
  "Content-Type": "application/json",
};

async function call(path, init = {}) {
  const response = await fetch(base + path, { ...init, headers });
  const body = await response.json();
  if (!response.ok) throw new Error(`${body.error.code}: ${body.error.message}`);
  return body;
}

let read = await call("/reads", {
  method: "POST",
  body: JSON.stringify({ url, wait: 20 }),
});
while (read.status === "pending") {
  read = await call(`/reads/${read.id}?wait=20`);
}

Sending the same link again with the same key returns the same read, free, so a retry after a dropped connection never pays twice.

Type to search…

↑↓ navigate↵ selectEsc close