52

Update 19 April

After a few days using cookie ig_pr two days ago is block. Looks like the only way to get the data now is use sessionid with a specific value

Original

I was using instagram ?__a=1 url to read all the post of instagram's users.

A few hours ago there was a change in the response and now doesn't allow me to use max_id to paginate.

Before I usually sent a request to

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

and using the graphql.edge_owner_to_timeline_media.page_info.end_cursor in the response I called the same page with a new max_id

https://www.instagram.com/{{username}}/?__a=1&max_id={{end_cursor}}

Now the end_cursor changes in each call & max_id is not working.

Please help :)

5
  • Did you happen to find a solution for this?
    – Hese
    Apr 13, 2018 at 17:07
  • instagram.com{{username}}/?__a=1 doesn't work for me at all and just gives access denied.
    – Dani
    Apr 19, 2018 at 20:49
  • @dprogramz ?__a=1 is not working anymore, please read the solutions where you can extract _sharedData in the normal webpage Apr 20, 2018 at 7:32
  • now this url's not working Dec 8, 2018 at 10:15
  • 1
    "?__a=1" seems to be working just fine.
    – Doug S
    Sep 13, 2019 at 12:23

16 Answers 16

40

The query_hash does not change, at least in the past few days. It indicate what TYPE of query it is.

Below listed 4 query types I knew, hope these help.

Load more media under https://www.instagram.com/someone/?__a=1

https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={"id":"93024","first":12,"after":"XXXXXXXX"}

(Instagram blocked the above access since 2018-04-12. You have to remove the __a=1 and extract the JSON inside a block. Look for "window._sharedData" in the HTML)

Load more media under https://www.instagram.com/explore/tags/iphone/?__a=1

https://www.instagram.com/graphql/query/?query_hash=298b92c8d7cad703f7565aa892ede943&variables={"tag_name":"iphone","first":12,"after":"XXXXXXXX"}

Load more media under https://www.instagram.com/explore/locations/703629436462521/?__a=1

https://www.instagram.com/graphql/query/?query_hash=ac38b90f0f3981c42092016a37c59bf7&variables={"id":"703629436462521","first":12,"after":"XXXXXXXX"}

Load more comments for https://www.instagram.com/p/Bf-I2P6grhd/

https://www.instagram.com/graphql/query/?query_hash=33ba35852cb50da46f5b5e889df7d159&variables={"shortcode":"Bf-I2P6grhd","first":20,"after":"XXXXXXXX"}

where XXXXXXXX is the end_cursor from the original request

19
  • do you know the query_hash for load more location for explore/locations api?
    – Noushad
    Apr 10, 2018 at 11:24
  • 1
    the above query_hash gives { message: 'forbidden', status: 'fail' } for my request https://www.instagram.com/graphql/query/?query_hash=298b92c8d7cad703f7565aa892ede943&variables=%7B%22tag_name%22%3A%22nasa%22%2C%22first%22%3A50%2C%22after%22%3A%22AQB0kona4PHJNMYAdLC4F4vPROUjIH47lawQ2Kx_sbq6KgE8p9hTkJu-V2LlMlzbS5gCZ4Uks3APKeBZ5SCnf9L_ovpvxzJPmYaG0o8EVF5Jgg%22%7D
    – Noushad
    Apr 10, 2018 at 11:26
  • 1
    as of 12 April 2018, only load more apis are failing without Cookie header set
    – Noushad
    Apr 12, 2018 at 13:46
  • 1
    @ArbazAbid: what I did is I logged in to my Instagram and loaded some page and in the developer console I checked the networks tab to see the network calls. You can see in every request they are sending a header called Cookie with a variable. So I used that value to make the api calls by setting the same header value pair in the request header. I used only Cookies header and nothing else
    – Noushad
    Apr 17, 2018 at 8:33
  • 1
    @ArbazAbid you can see my code in the answer below. It seems Instagram has different API authentication processes in different parts of the world. I am not passing any csrftoken or other headers except for the simple Cookie header Maybe this link will help you
    – Noushad
    Apr 17, 2018 at 9:12
32

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

13
  • 1
    Worked for me also. But when I am using instagram.com/pawanlakhera77/?__a=1&access_token={mytoken} I am still getting the login page. Do I need to have instagram_graph_user_profile and instagram_graph_user_media for instagram basic display api ? Can't I test this without these permission? Feb 14, 2021 at 20:51
  • 4
    Is this solution still working for you? I receive also on the /username/channel endpoint a CORS error
    – dohomi
    Mar 15, 2021 at 1:18
  • 2
    You as of 2-3 days back, CORS issue is being triggered. I am able to do that query from a node environment on my local system, but when I do using a lamda function, I think it is trying to redirect me to login page and thus invalid JSON response.
    – karx
    Mar 18, 2021 at 18:17
  • 2
    I wish they still had this active :( Have you guys found any workarounds?
    – dominic
    Apr 21, 2021 at 4:25
  • 1
    instagram.com/username/channel/?__a=1 also not working and ask for login details Sep 7, 2021 at 12:06
25

i fixed it by adding &__d=dis. for example like this https://www.instagram.com/p/Ch656GRoyuO/?__a=1&__d=dis

4
15

I just came by the same issue.

Looks like they just changed their private api by removing the max_id. Their website seems to have replaced the old api with the graphql api.

https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={"id":"111","first":12,"after":"xxx"}

  • id: user ID,
  • first: amount of nodes to get,
  • after: the 'end_cursor' you can get from data['user']['edge_owner_to_timeline_media']['page_info']['end_cursor']

use either query_hash or query_id

  • query_hash: 472f257a40c653c64c666ce877d59d2b
  • query_id: 17888483320059182

I have no idea how long that query_id/query_hash will work, it's up to Instagram. They will eventually change it.

Updated 4/8/2018 - Before FB didn't check any cookies, but looks like they added quick validation. Try adding ig_pr=2 to the request cookie, when sending your API. This quick fix works for me. Who knows when FB will change it again.

As long as FB doesn't enforce login for these basic APIs, there always will be an easy workaround.

15
  • I just found the same solution! It's working right now, but looks like we will have to use their API Mar 14, 2018 at 10:43
  • @pablo , did you find any solution? I realy need it ;( Mar 15, 2018 at 12:25
  • 1
    @SalehMosleh That api endpoint is Instagram's private api, created to be used only by Instagram app/web not for public. So probably only Instagram know what query_hash and query_id represents. I first thought it was hashed value of the variables, but it is permanent(independent of variables). Think of it like hard coded auth_token. Mar 16, 2018 at 7:11
  • 1
    @ArbazAbid I was afraid this would happen. Ever since the Cambridge Analytica fiasco, Facebook has been changing their APIs without notices. We can't really blame them since this API is not meant to be used by end users anyways. Since one of my project depends on this API, I will eventually take a look into it. Apr 8, 2018 at 1:09
  • 2
    @Pablo I have updated quick fix above. When sending the request, add ig_pr=2 to the request cookie. I have a feeling this is gonna be a game of cat and mouse. Apr 9, 2018 at 1:01
12

Translated some of the folks' code to PHP:

<?php
function getPublicInfo($username) {
    $url     = sprintf("https://www.instagram.com/$username");
    $content = file_get_contents($url);
    $content = explode("window._sharedData = ", $content)[1];
    $content = explode(";</script>", $content)[0];
    $data    = json_decode($content, true);
    return $data['entry_data']['ProfilePage'][0];
}

Not sure for how long it's gonna work. For my small project it does the job for now. The result is very similar (if not equal) to the one at the URL: instagram.com/{user}/?__a=1

4
  • thank you for sharing this, solved my problem for now
    – Pezhvak
    Feb 19, 2019 at 1:02
  • can we get the mp4 or jpg url of media (picture or video ) using php curl ? Mar 14, 2019 at 16:31
  • I don't know how long is gonna work, but so far the "window._sharedData" method works, while the usual "file_get_contents" seems to trigger some kind of black-listing of the request's IP (which, btw, we don't know how long it stays as black-listed or if it's white-listed any time). Jun 12, 2020 at 9:56
  • It seems that solution is not working now Mar 12, 2021 at 9:14
8

The main problem with using graph/query is that I only have the username, to extract the userId & the last post like we use to do with ?__a=1 we have to get the instagram's user page & extract _sharedData

Javascript

let url = "https://www.instagram.com/"+username;
$.ajax({
    type: 'GET',
    url: url,
    error: function () {
        //..
    },
    success: function (data) {
        data = JSON.parse(data.split("window._sharedData = ")[1].split(";</script>")[0]).entry_data.ProfilePage[0].graphql;
        console.log(data);
    }
})

After get all this data we can call graph/query (not in client side)

2
  • 1
    Your data var will not work. You need to escape the closing bracket in the script tag data = JSON.parse(data.split("window._sharedData = ")[1].split(";<\/script>")[0]).entry_data.ProfilePage[0].graphql;
    – jpisty
    Jan 2, 2019 at 18:52
  • @Pablo how i can get first page of my own feeds? I want get window.__additionalDataLoaded('feed' data! Could you help ? Thanks Jul 7, 2019 at 0:35
6

As of 21 May 2021, using a /channel will make it work, but only if using a browser User-Agent header with your request, for example with a curl:

curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36" https://www.instagram.com/{username}/channel/?__a=1

1
5

For pagination you can now use ?__a=1&page=2

2
  • 1
    Thank you. Just curious, but how did you figure this out?
    – Moondra
    Jul 17, 2019 at 6:19
  • 2
    @Moondra i was just looking into network requests in dev tools. Jul 19, 2019 at 11:02
3

This answer is not directly helping the question but posting because someone might benefit from the answer. As of the current date 12 April 2018, the load more APIs will not work without a Cookie header set. Below are some codes for fetching Instagram public APIS

    let url = "https://www.instagram.com/explore/";
    if (payload.type == 'location') {
        url = url + "locations/" + payload.location_id + "/" + payload.location_name + "/?__a=1";
    } else if (payload.type == 'hashtag') {
        url = url + "tags/" + payload.hashtag + "/?__a=1";
    } else { //profile
        url = "https://www.instagram.com/" + payload.user_name + "/?__a=1";
    }

    request(url, function (error, response, body) {
        body = JSON.parse(body);
        //below are params which are required for load more pagination payload
        paginationData = {
            has_next_page: body.data.user.edge_owner_to_timeline_media.page_info.has_next_page,
            end_cursor: body.data.user.edge_owner_to_timeline_media.page_info.end_cursor
        };

        //user.edge_owner_to_timeline_media for profile posts,
        //hashtag.edge_hashtag_to_media for hashtag posts
        //location.edge_location_to_media for location posts
    });

and for load more items, I am using:

    let url = "https://www.instagram.com/graphql/query/";
    if (payload.type == 'location') {
        let variables = encodeURIComponent('{"id":"' + payload.pagination.id + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
        url = url + "?query_hash=ac38b90f0f3981c42092016a37c59bf7&query_id=17865274345132052&variables=" + variables;
    } else if (payload.type == 'hashtag') {
        let variables = encodeURIComponent('{"tag_name":"' + payload.pagination.tag_name + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
        url = url + "?query_hash=298b92c8d7cad703f7565aa892ede943&query_id=17875800862117404&variables=" + variables;
    } else { //profile
        let variables = encodeURIComponent('{"id":"' + payload.pagination.owner_id + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
        url = url + "?query_hash=472f257a40c653c64c666ce877d59d2b&query_id=17888483320059182&variables=" + variables;
    }

    let options = {
        url: url,
        headers: {
            Cookie: "Cookie value which i copied from my logged in instagram browser window"
        }
    };

    request(options, function (error, response, body) { });

It seems query_id is no longer required and query_hash is sufficient now. I'm not sure though but it seems working without them too for me.

3
  • How can I get query_hash and query_id parameter ? Apr 12, 2018 at 18:00
  • query_hash and query_id are fixed for each type of request. I think query_id is not longer required. query_hash is enough. I just kept it for someones reference. You can use the above values
    – Noushad
    Apr 13, 2018 at 18:39
  • 1
    Thanks, your solution worked for me so far, seems there is no rate limits as I have daily 2k+ traffic on my site and this solution is still working fine.
    – aadi1295
    Apr 17, 2018 at 20:33
2

Now it returns this response and doesn't work:

for (;;);{"__ar":1,"error":1357004,"errorSummary":"Sorry, something went wrong","errorDescription":"Please try closing and re-opening your browser window.","payload":null,"hsrp":{"hblp":{"consistency":{"rev":1006107013}}},"lid":"7136695846284496053"}

URL: https://www.instagram.com/instagram/?__a=1

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

1
2

As of 19th May 2023

The solution that seems to be working fine for me (without using an external website) is to send a request here:

https://www.instagram.com/{username}/feed/?__a=1&__d=dis

There is no need to be authenticated (and have a session id) to make this request.

The problem I'm still encountering is that I can't get all posts, and I can only get the first 12.

I tried adding max_id={{end_cursor}} to the URL but it didn't work.

2
  • Its working in a browser where I have instagram already logged in but not in postman/incognito window. What else does it want? Aug 19 at 1:04
  • somewhere in another post, I was people recommending putting the max_id in between the parameters like this ?__a=1&max_id={{end_cursor}}&__d=dis this was the only way for me to work
    – lestra
    Oct 4 at 13:44
1

As of the current date 12 April 2018, 4:00PM (GMT+1), API queries work without any cookie. I have no idea what they're doing...

Just try this link in private navigation.

3
  • 2
    I'm getting 403 error for this without login. I'm from India and I think they have deployed their changed code to a new server and gradually migrating the traffic.
    – Noushad
    Apr 13, 2018 at 18:44
  • 1
    @FarazAzhar x-requested-with, x-instagram-gis, cookie and user-agent are mandatory in headers. Apr 18, 2018 at 19:14
  • where do you get the query_hash from ? Jun 12, 2018 at 13:28
1

I faced a similar problem in that I was unable to parse the JSON file using "?__a=1" and ended up with JSONDecodeError: Expecting value. Searched in many places and finally found a catch, using Header solved the probelm. Try using this, it worked for me

link = 'http://instagram.com/instagram/?__a=1'

headers = {'User-Agent': 'Mozilla'}
r = requests.get(link, headers=headers)
data = r.json()
1

100% working as of now It can be circumvented using the session ID.

1
  • 1
    it doesn't seem to work
    – DarkestOne
    May 9, 2022 at 0:20
0

actually the position and tag changed if you look clearly we dont require any channel or anything url change at all the data is present under video versions attribute with many video quality actually enter image description here

but sometimes ?_a=1 working normally i.e you can see the short code in the start

1
  • using ?__a=1 is resulting in 'has been blocked by CORS policy'
    – DarkestOne
    May 8, 2022 at 16:58
0

Currently https://www.instagram.com/username/channel/?__a=1 seems to be working. However, after a few minutes of trying this URL, it may still request for login. In this case, if you convert the word channel to reels, the problem will be fixed.

For example: https://www.instagram.com/username/reels/?__a=1

3
  • Worked for me to some extent. What other endpoint variations are available? For example, feed/ worked too
    – Radical Ed
    Aug 3, 2022 at 5:53
  • where did you find these endpoint variations ? Aug 3, 2022 at 15:26
  • I looked my instagram profile in web browser. Then, I clicked the all pages in profile pages like, reels, channels etc. Finally, I tried this words and it worked. Also, can you try tagged, saved, followers and follower words? Maybe, It works. Aug 4, 2022 at 6:17

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.