Search code examples
instagraminstagram-api

Instagram API - get ALL users for specific hashtag or location


I've started looking into the Instagram API and I want to pull out ALL users that have used a specific hashtag or have been at a given location.

As the documentation says I can get the RECENT users for a hashtag and location using the following methods:

https://api.instagram.com/v1/tags/{tag}/media/recent?access_token={token}

https://api.instagram.com/v1/media/search?lat={latitude}&lng={longitude}&access_token={token}

I am getting the recent (up to 7 minutes old) data with these. My question is if it is possible to retrieve ALL users, not just the recent? Also, how can I get media objects for a name of a location, for example "London"? It seems like I have to get the location ID of the location first, but the documentation doesn't make it clear how to get the ID.


Solution

  • Getting users by hashtag or location

    To my knowledge, there isn't a tidy endpoint for getting user data by hashtag or location. I think the only way to get all users from the Tags, Locations, or Geographies recent media endpoints is to slaughter the API and scape the user object from each item in the data array from each paginated response on those endpoints. There's a pagination attribute on the same level as data in each response that helps you do this.

    "pagination": { "next_max_tag_id": "1009570282366298753", "deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead", "next_max_id": "1009570282366298753", "next_min_id": "1009570411710268632", "min_tag_id": "1009570411710268632", "next_url": "https://api.instagram.com/v1/tags/{tag}/media/recent?access_token={access_token}&max_tag_id=1009570282366298753" },

    pagination.next_url is going to get you another page of responses later than the current response, which will have a new pagination.next_url and so on.

    You can pass an additional parameter in your GET URL ?count=33, this specifies the number of objects you get in the data array, the max is 33. So you can get 33 results per request instead of the default 20 per request.

    Getting Location IDs

    The Location Media Recent Endpoint only works on location ID, but location ID can be found in the location property of an object in the data array from any recent media endpoint. That property will only be populated if that image was geo-located and a location was specified, which are different to the Instagram API.

    It'd probably be easier just to use the location search endpoint and search through the results until you find the location your looking for.

    https://api.instagram.com/v1/locations/search?lat={latitude}&lng={longitude}&distance={radius_in_meters}&access_token={access_token}