Developer example
Import the latest posts and render a feed
Examples use bundle.social's public API shape: API key authentication, a post date, selected social account types, and platform-specific data.
1const headers = { "x-api-key": process.env.BUNDLE_SOCIAL_API_KEY, "Content-Type": "application/json" };23// Import the 12 latest grid posts from a connected Instagram account4const job = await fetch("https://api.bundle.social/api/v1/post-history-import", {5 method: "POST",6 headers,7 body: JSON.stringify({8 teamId: "team_123",9 socialAccountType: "INSTAGRAM",10 count: 12,11 onlyLatest: true,12 surface: "PROFILE_GRID",13 withAnalytics: true,14 }),15}).then(async (r) => {16 // 409 means an import is already running for this account17 if (!r.ok) throw new Error(`Import failed to start: ${r.status} ${(await r.json()).message}`);18 return r.json();19});2021// Poll the job until it finishes22let status = job.status;23while (status !== "COMPLETED" && status !== "FAILED") {24 await new Promise((resolve) => setTimeout(resolve, 5000));25 const res = await fetch(`https://api.bundle.social/api/v1/post-history-import/${job.id}`, { headers });26 if (!res.ok) throw new Error(`Status check failed: ${res.status}`);27 ({ status } = await res.json());28}29// Keep the previous cache if the import failed30if (status !== "COMPLETED") throw new Error("Import failed, keeping the cached feed");3132// Read the newest imported posts for the account and cache them for your feed.33// This returns every imported post, so earlier Story or non-grid imports show up too.34const { posts } = await fetch(35 "https://api.bundle.social/api/v1/post-history-import/posts?teamId=team_123&socialAccountType=INSTAGRAM&limit=12",36 { headers },37).then((r) => r.json());