DEV Community

Cover image for 💡 Quick tip: Fetch JSON data from a public Instagram profile without Graph API
Benjamin Grand
Benjamin Grand

Posted on • Edited on

💡 Quick tip: Fetch JSON data from a public Instagram profile without Graph API

I hate Instagram's Graph API for its complex configuration...

Thanks to a Google search, I discovered a magic query string to use with the official Instagram website:

https://www.instagram.com/{public_profile_name}/?__a=1
Enter fullscreen mode Exit fullscreen mode

Using the query string __a=1 allows you to fetch the JSON content of any public Instagram profile.

Below is one of the solutions with JavaScript:

async function getInstagramPictures (profileName) {
  const baseUrl = "https://www.instagram.com";
  const profileUrl = `${baseUrl}/${profileName}`;
  const jsonDataUrl = `${profileUrl}/?__a=1`;

  const response = await fetch(jsonDataUrl);
  const jsonData = await response.json();
  const pictures = jsonData.graphql.user.edge_owner_to_timeline_media.edges;

  if (response.ok) {
    return pictures;
  } else {
    throw new Error(pictures);
  }
}
Enter fullscreen mode Exit fullscreen mode
getInstagramPictures("nasa")
  .then(pictures => console.log("Pictures:", pictures))
  .catch(error => console.error("Error:", error));
Enter fullscreen mode Exit fullscreen mode

By looping pictures, you can display each image or its thumbnails:

  • picture.node.display_url
  • picture.node.thumbnail_resources
    • [0].src = 150x150
    • [1].src = 240x240
    • [2].src = 320x320
    • [3].src = 480x480
    • [4].src = 640x640

Sources:

Top comments (14)

Collapse
 
philyboysmith profile image
Phil Smith

The CORS and intermittent availability issues are frustrating - Instagram seems to throttle or block these unofficial endpoints unpredictably, especially from server IPs. If you're just fetching your own posts, you can use the official Instagram Basic Display API, though it's a bit fiddly due to refresh tokens expiring every 60 days and requiring manual reauthorization.
Here's a basic example once you have your access token:

async function getInstagramPosts(accessToken) {
  const fields = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp';
  const url = `https://graph.instagram.com/me/media?fields=${fields}&access_token=${accessToken}`;

  const response = await fetch(url);
  const data = await response.json();

  if (response.ok) {
    return data.data;
  } else {
    throw new Error(data.error.message);
  }
}

getInstagramPosts(YOUR_ACCESS_TOKEN)
  .then(posts => console.log("Posts:", posts))
  .catch(error => console.error("Error:", error));
Enter fullscreen mode Exit fullscreen mode

The tricky part is getting that initial token (requires Facebook App setup) and then refreshing it before it expires every 60 days. I actually built FeedFramer to handle this token refresh dance automatically because I got tired of my Instagram embeds breaking. But if you're comfortable managing the token lifecycle yourself, the official API is much more reliable than the ?__a=1 trick.

Collapse
 
ardhityawiedhairawan profile image
Ardhitya Wiedha Irawan

It's not working. Inst*gr*m has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Any solution ?

Collapse
 
bgrand_ch profile image
Benjamin Grand

Try on the request header:
Access-Control-Allow-Origin: *

developer.mozilla.org/en-US/docs/W...

Collapse
 
ardhityawiedhairawan profile image
Ardhitya Wiedha Irawan

It is working? 🤔

Collapse
 
bgrand_ch profile image
Benjamin Grand

❌ Unfortunately, this quick tip no longer works today...

If someone knows a good tutorial for using the Graph API, I am interested...

Collapse
 
iankduffy profile image
Ian Duffy

It does seem to be useable again strange.

Collapse
 
subashcs profile image
subashcs

This worked perfectly on a public Instagram profile. Applause !!

Collapse
 
ronakonly profile image
Ronak Patel

From my experience, this only works if you have recently logged into an Instagram account with the same IP address. But mostly does not work from a server/proxy.

Collapse
 
drakula2k profile image
Vlad

Nice article! I recently wrote an example of Instagram posts scraper with Scrapy using the same approach with ?__a=1 URLs webscraping.ai/blog/instagram-scra...

Collapse
 
griffinjohnston profile image
Griffin Johnston • Edited

Feel free to yell at me if this isn't allowed here, but this issue is exactly why I built behold.so - it let's you get user or hashtag posts as JSON with just a couple clicks. There are paid tiers, but the basic level is free. Much better than the madness of Meta's API red tape.

Collapse
 
hkrish0 profile image
Harikrishnan M

it is working for private account also

Collapse
 
mrpotocnik profile image
mrpotocnik

Requests from server/proxy seem to work intermittently and I haven't been able to figure out when/why.
Worked yesterday, doesn't today.

Collapse
 
bgrand_ch profile image
Benjamin Grand

✔️ Welcome back!