type LivePost = {
type: "live_post";
platform: "instagram" | "tiktok";
external_id: string;
url: string;
like_count: number | null;
};
type LiveFeed = {
data: LivePost[];
pagination: { next_cursor: string | null; has_next: boolean };
meta: { fetched_at: string; cached: false; warnings: string[] };
};
async function fetchLivePosts(
apiKey: string,
handle: string,
maxPages = 3,
): Promise<LivePost[]> {
const posts: LivePost[] = [];
let cursor: string | null = null;
for (let pageNumber = 0; pageNumber < maxPages; pageNumber++) {
const url = new URL("https://api.creatoraudit.com/v2/live/tiktok/posts");
url.searchParams.set("handle", handle);
if (cursor !== null) url.searchParams.set("cursor", cursor);
const response = await fetch(url, {
headers: { Authorization: `Bearer ${apiKey}` },
cache: "no-store",
signal: AbortSignal.timeout(60_000),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
const body: LiveFeed = await response.json();
posts.push(...body.data);
if (!body.pagination.has_next) break;
cursor = body.pagination.next_cursor;
}
return posts;
}