Error fetching Instagram posts due to incorrect API usage

1 replies
Last updated: May 29, 2023
hey, in my StudioNavbar.tsx I want to fetch posts from instagram, it previously worked when I only fetch and inport new posts, but now I'm trying to update the media URL of existing ones at the same time. here's my function:const fetchInstagramPosts = async () => {
const existingPosts = await client.fetch(

      *[ _type == "instagramPost" ] {
try {
const url = `,media_url,timestamp&access_token=${INSTAGRAM_KEY} `; const res = await fetch(url);
const data = await res.json();
if (!data || ! {
"Error: Instagram API returned an unexpected response",

// Create a transaction for creating or updating the documents
const transaction = client.transaction();

for (const post of {
const existingPost = existingPosts.find(
(p: any) => p.instagramId ===

if (existingPost) {
// Update the existing post's mediaUrl
mediaUrl: post.media_url,
timestamp: post.timestamp,
} else {
// Create a new document
const newPost = {
_type: "instagramPost",
mediaUrl: post.media_url,
timestamp: post.timestamp,
articleLink: " ", // set a default value };


// Commit the transaction
await transaction.commit();
} catch (error) {
console.error("Error fetching Instagram posts", error);

But I receive this error:
Error fetching Instagram posts TypeError: transaction.patch(...).set is not a function

Can someone help me?
May 29, 2023, 6:59 PM
Hi Max. That doesn’t appear to be the right API for
. Could you please try changing this:

  mediaUrl: post.media_url,
  timestamp: post.timestamp,
to this:

  mediaUrl: post.media_url,
  timestamp: post.timestamp,
Which, if we remove the abstraction, is effectively this:

        mediaUrl: post.media_url,
        timestamp: post.timestamp,
  .commit() // I'm assuming this happens later in your code and works
May 29, 2023, 7:41 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?