16

How to get ALL Instagram POSTs by hashtag with the API (not only the posts of my own account)

I am trying to get all the instagram pictures tagged with a precise hashtag but I only receive my own developer account posts tagged with the hashtag.

I am currently working in local development environment, maybe that's the problem?

Furthermore, what is Sandbox mode, and what should I do to go "Real" mode ?

In the platform policy, it's written "You cannot use the API Platform to crawl or store users' media without their express consent." .

Does it mean that what I am trying to do is simply not possible ?

Thanks for your help

KROMI Developers
  • 291
  • 1
  • 3
  • 9

3 Answers3

28

You can get the posts in raw JSON by simply accessing this URL: https://www.instagram.com/explore/tags/summer/?__a=1

Then just use javascript/jquery to fetch the data an loop through the posts. You get 12 posts, and a variable to see if there are more pages.

Example of getting latest 6 posts:

$.get('https://www.instagram.com/explore/tags/summer/?__a=1', function (data, status) {
    for(var i = 0; i < 6; i++) {
        var $this = data.graphql.hashtag.edge_hashtag_to_media.edges[i].node;
        $('#container').append('<img src="'+  $this.thumbnail_resources[2].src +'">');
    }
});
Legarndary
  • 957
  • 2
  • 16
  • 37
  • Wow! Then what is their API for? Our app that want to grab hashtag content has been rejected (without reason) for three times now in the app review process.. How reliable is this url? – Jos Jan 03 '19 at 09:53
  • @JosFabre i have been using this method for a couple of years. I think it was made possible when facebook bought instagram, But in the time i have been using this method the URL never changed, but the response have changed one time. – Legarndary Jan 03 '19 at 13:58
  • I can get the post image and everything but how can I access to the profile? I can see the `owner` property but can't find any way to access to the profile with it. I appreciate your answer! – Phillip YS Mar 06 '19 at 10:20
  • @PhillipYS what do want to access? – Legarndary Mar 11 '19 at 14:45
  • @Legarndary I got the answer from the question but do you know is there any way to get top media only? I can see there's `is_top_media_only` in the object but I can't way to make the property true – Phillip YS Mar 15 '19 at 07:26
  • @PhillipYS so for the late answer. If you still want to get the top media you can, The result of the above url always give you the 9 more popular posts. you can find them under the object: `edge_hashtag_to_top_posts` – Legarndary Mar 21 '19 at 11:43
  • @Legarndary Hi. Sorry for butting in but I also needed this so thank you for posting it. Would you happen to know if it is possible to search multiple hashtags at once? Or implement an "and" and "or" search? – iamnobody Mar 23 '19 at 02:51
  • @Legarndary I also notice has_next_page and end_cursor attributes. Would you know how to access the succeeding pages? – iamnobody Mar 23 '19 at 03:00
  • 3
    @iamnobody the URL endpoint doesn't have multiple hashtag support. So you have to do an ajax call for each hashtag. You can use `has_next_page` to make an if statement to determine if you should make another ajax call to the same hashtag. Then you add another parameter to your URL and put the `end_cursor`as value. Like this: `https://www.instagram.com/explore/tags/{{ hashtag }}/?__a=1&max_id={{ end_cursor }}` Hopes it helps, otherwise, let me know :) – Legarndary Mar 26 '19 at 10:34
  • @Legarndary it works! Thank you so much! I was on the verge of giving up on FB/IG developer access so this really made my day. Thanks! – iamnobody Mar 26 '19 at 14:30
  • @iamnobody I am glad I could help :) – Legarndary Mar 27 '19 at 07:28
  • @Legarndary Awesome! Is there a way to also get the link for each image? – Fred K Nov 04 '19 at 08:48
  • 1
    @FredK do you mean a link to the post? if so, you have to use the shortcode which is returned by the object: https://instagram.com/p/[ShortCodeHere]. Alternative i have made a little jQuery plugin that uses this method, You can find it here: https://github.com/kasperlegarth/instastory.js – Legarndary Nov 04 '19 at 11:13
  • Thanks @Legarndary You know if is there an URL to get posts by hashtag AND by user/account? – Fred K Nov 06 '19 at 13:44
  • @FredK, nope. You have to make a different request for each source. A request to get the hashtag posts and then another for a user's posts. – Legarndary Nov 06 '19 at 15:48
  • Hello, @Legarndary This was really a life saver Thanks for posting, I think the response have been changed since you last posted do you know how can I get the posts of next pages? – Lalit Vavdara Aug 06 '21 at 08:14
  • Hi, @LalitVavdara sadly this method is no longer available due to CORS policy. You can no longer make a request from one domain to another like this. – Legarndary Aug 12 '21 at 12:03
4

When you register for API client, you will be sandbox mode (development/test mode), in this mode you will get only your and your sandbox user's data in API response.

Once you complete app, you have to submit for review to instagram, if approved then you can set app to Live mode, and then you will see all posts in API response.

P.S. Note that you have have public_content permission in oauth scope to get all posts

krisrak
  • 12,882
  • 3
  • 32
  • 46
-1

You can get Instagram public posts by one or several hashtags with Data365 API I am currently working on. In a request, you can specify the number of posts you want to receive. You can get up to 20,000 posts in one request. It is possible to define the period (from-to) for receiving posts and specify if you need comments and how much. You also can point out other parameters to customize requests based on your personal needs.

To retrieve posts by hashtag you can use the following queries.

POST request to create a task for downloading posts, on the example of bitcoins hashtag: https://api.data365.co/v1.1/instagram/tag/bitcoins/update?max_posts_count=10000&access_token=TOKEN

GET request to obtain a list of posts of 100 items each through pagination: https://api.data365.co/v1.1/instagram/tag/bitcoins/posts?max_page_size=100&access_token=TOKEN

Besides, you can set up posts auto-monitoring by hashtag. You need to indicate the auto_update_interval parameter in the request if you want all data to be updated automatically according to some defined time interval. With this function, you can receive posts by a given hashtag regularly.

More detailed info in API documentation here: https://api.data365.co/v1.1/instagram/docs#tag/Instagram-hashtag-search

I hope you find my comment helpful.