11

I've been using Instagram's undocumented API https://www.instagram.com/<user>/?__a=1 to get a public user feed on a website. Since a while now, this is not working anymore, probably because Facebook removed it. Is there an other way to get the data of an instagram account in a easy way?

9
  • 1
    Instagram blocked __a=1 endpoint , checkout stackoverflow.com/questions/49265339/… Apr 13, 2018 at 10:18
  • Same solution with Php I updated my answer yesterday. stackoverflow.com/questions/49458818/instagram-pb-full-res/…
    – xC0dex
    Apr 13, 2018 at 11:32
  • Too bad it's blocked completely... It's public available info for god's sake... But I already knew that was going to be the case, it was just a matter of time since Facebook acquired Instagram... And I know it's blocked, I'm just wondering if anyone knows a solid alternative solution. Apr 13, 2018 at 12:03
  • @OlivierVanBulck I am facing the same problem. Would you please give instagram example with pagination? Apr 24, 2018 at 8:55
  • 2
    It seems to work again indeed. Maybe they added it again because of complaints. Sep 18, 2019 at 12:41

5 Answers 5

17

I built a small server which does that transformation. You'll receive the instagram data as before with ?__a=1 (as JSON ) - have fun 😊

https://www.instapi.io/u/<username>

https://www.instapi.io/u/appwithus

EDIT 12/2020: Unfortunately the service is no longer available

14
  • Do the cursors work? I did it with a GET variable called max_id and gave that one the next cursor but that's not working here right now? Apr 16, 2018 at 21:53
  • Thats not possible, you can just provide your username, no additional parameters
    – chrisby
    Apr 17, 2018 at 7:54
  • 2
    Would you like to share your source code (e.g. github)?
    – wiesson
    May 1, 2018 at 9:01
  • 1
    @wiesson, he is getting data with the help of web scrapper. you can see my answer below to get that code. May 11, 2018 at 7:07
  • 6
    instapi.io seems to have gone out of existence 😢 Oct 23, 2018 at 17:47
8

Edit 15/03 NOT WORKING ANYMORE Seems like instagram changed again their API, now it gives a CORS error.

As of 2 february 2021, I have found a solution

Instead of using https://www.instagram.com/username/?__a=1 which it asks for a login.

Justing adding a /channel seems to make it work, like so:

https://www.instagram.com/username/channel/?__a=1

5
  • The original url without /channel gives the same result now I believe? Feb 3, 2021 at 8:25
  • It does, but without /channel it sends a login page instead of the JSON in a GET request
    – MattF
    Feb 3, 2021 at 16:46
  • How do you get the data? I've tried with resp.graphql.user.edge_owner_to_timeline_media.edgesand I get the following: TypeError: Cannot read property 'graphql' of undefined at index.js:8
    – cyruslk
    Feb 9, 2021 at 2:59
  • data getting in postman using instagram.com/username/channel/?__a=1 url but not getting in react.Js project while calling api using fetch().
    – Pragnesh
    Aug 10, 2021 at 11:59
  • only thing i'm getting with postman is login page
    – DarkestOne
    May 9, 2022 at 0:45
4

There is a JSON data in https://www.instagram.com/<user>/. You can use regexp to find what you need.

Sample

// This regexp gets widest possible dict around "profile_pic_url"
// but inside tag <script type="text/javascript">...</script>
let r = new RegExp('<script type="text\/javascript">' + 
                   '([^{]+?({.*profile_pic_url.*})[^}]+?)' +
                   '<\/script>');

let source = document.documentElement.outerHTML;
let jsonStr = source.match(r)[2];
let data = JSON.parse(jsonStr);
console.log('data', data);

let oldVariantOfData = data['entry_data']['ProfilePage'][0];
console.log('oldVariantOfData', oldVariantOfData);
3
  • Great option! Though it's not working perfectly for me. I need the info about the posts from that user. The json has the first 12 and a next cursor, but for what I know, there is no endpoint that I can use the next cursor on now. It seems the page is ignoring the information for pagination. Is there a way to get the rest of the posts in a new call? Apr 16, 2018 at 9:16
  • any idea how to get next page?
    – aadi1295
    Apr 16, 2018 at 16:01
  • In order to get 12 posts I use SlimerJS. First I got 12 posts, and read next cursor. Then I simulate scroll down action and capture respons with json, which url contains this cursor.I get new 12 posts from it and next cursor. etc. Apr 16, 2018 at 17:37
2

The same response is attached in the html response of the profile url, I perform this temporal solution (when I can't use the API) in python:

url_recent_media = 'https://www.instagram.com/%s/' % instagram_id
response = urllib2.urlopen(url_recent_media)

insta_html = response.read()
insta_html_split = insta_html.split('"ProfilePage":[')
if len(insta_html_split) > 1:
    insta_html_split_2 = insta_html_split[1].split(']},"gatekeepers"')
    if len(insta_html_split_2) > 1:
        json_dict = json.loads(insta_html_split_2[0])

I hope this help you.

1
  • It'd probably help, but too bad it's nothing more than a temporary solution. Whenever the code change, it won't work anymore... And Facebook will probably change it fast enough... Apr 13, 2018 at 12:05
1

you can try without using instagram API.

import json, urllib2

img_dicts = []
url = 'https://www.instagram.com/{}/'.format(instagram_username)
try:
    r = urllib2.urlopen(url, timeout=10.0)
    instagram_html = r.read()
    instagram_html_data = instagram_html.split('"ProfilePage":[')
    if len(instagram_html_data) > 1:
        instagram_html_final_data = instagram_html_data[1].split(']},"gatekeepers"')
        if len(instagram_html_final_data) > 1:
            json_dict = json.loads(instagram_html_final_data[0])
            media = json_dict['graphql']['user']['edge_owner_to_timeline_media']['edges']
            for obj in media:
                img_dicts.append({
                    'id': obj['node']['id'],
                    'caption': obj['node']['edge_media_to_caption']['edges'][0]['node']['text'],
                    'imgurl_standard': obj['node']['display_url'],
                    'imgurl_lower': obj['node']['thumbnail_resources'][4]['src'],
                    'imgurl_thumb': obj['node']['thumbnail_resources'][3]['src']
                })

img_dicts will give you images in different quality and caption of instagram post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.