13

I have a list of thousands of instagram user-ids. How do I get their Instagram usernames/handles?

peterh
  • 11,875
  • 18
  • 85
  • 108

9 Answers9

23

You can access it without needing API via internal /user/ endpoint instagram uses for AJAX requests:

https://i.instagram.com/api/v1/users/{user_id}/info/

where {user_id} is numeric user id like 6817966272.

Returned response example (see user['username'] key for username):

GET https://i.instagram.com/api/v1/users/6817966272/info/

{
  "user": {
    "pk": 6817966272,
    "username": "myriaamaa",
    "full_name": "\u2661",
    "is_private": false,
    "profile_pic_url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/66486d198fc02046d04d7bc11e51e54a/5D913015/t51.2885-19/s150x150/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net",
    "profile_pic_id": "2056076981860037983_6817966272",
    "is_verified": false,
    "has_anonymous_profile_picture": false,
    "media_count": 216,
    "follower_count": 4926,
    "following_count": 83,
    "following_tag_count": 0,
    "biography": "YOU. ARE. HOLY \ud83c\udf19",
    "external_url": "",
    "total_igtv_videos": 0,
    "total_ar_effects": 0,
    "usertags_count": 6,
    "is_favorite": false,
    "is_interest_account": true,
    "hd_profile_pic_versions": [
      {
        "width": 320,
        "height": 320,
        "url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/fafecdc76c82de85580c9c03d14b1aaa/5D9BD2E5/t51.2885-19/s320x320/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net"
      },
      {
        "width": 640,
        "height": 640,
        "url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/0ec5339e3958c9c41414e5378fa2443c/5D7DD28A/t51.2885-19/s640x640/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net"
      }
    ],
    "hd_profile_pic_url_info": {
      "url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/8b3859950f0bb8e1a4a8f65566992b78/5D9132EF/t51.2885-19/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net",
      "width": 774,
      "height": 774
    },
    "mutual_followers_count": 0,
    "has_highlight_reels": true,
    "can_be_reported_as_fraud": false,
    "is_business": false,
    "account_type": 1,
    "is_call_to_action_enabled": null,
    "include_direct_blacklist_status": true,
    "is_potential_business": true,
    "is_bestie": false,
    "has_unseen_besties_media": false,
    "show_account_transparency_details": false,
    "auto_expand_chaining": false,
    "highlight_reshare_disabled": false
  },
  "status": "ok"
}

EDIT 2019-10:

This endpoint only serves user-agents that identify themselves as instagram app.

I got it working with iphone's instagram user agent:

Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 Instagram 12.0.0.16.90 (iPhone9,4; iOS 10_3_3; en_US; en-US; scale=2.61; gamut=wide; 1080x1920)

For more check out this online user agent database: https://developers.whatismybrowser.com/useragents/explore/software_name/instagram/1

# top 5:
Mozilla/5.0 (Linux; Android 8.1.0; motorola one Build/OPKS28.63-18-3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/70.0.3538.80 Mobile Safari/537.36 Instagram 72.0.0.21.98 Android (27/8.1.0; 320dpi; 720x1362; motorola; motorola one; deen_sprout; qcom; pt_BR; 132081645)  
Mozilla/5.0 (Linux; Android 7.0; Lenovo K33b36 Build/NRD90N; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.109 Mobile Safari/537.36 Instagram 41.0.0.13.92 Android (24/7.0; 480dpi; 1080x1920; LENOVO/Lenovo; Lenovo K33b36; K33b36; qcom; pt_BR; 103516666)  
Mozilla/5.0 (Linux; Android 5.0.1; LG-H342 Build/LRX21Y; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.109 Mobile Safari/537.36 Instagram 40.0.0.14.95 Android (21/5.0.1; 240dpi; 480x786; LGE/lge; LG-H342; c50ds; c50ds; pt_BR; 102221277)  
Mozilla/5.0 (Linux; Android 6.0.1; SM-G610M Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.109 Mobile Safari/537.36 Instagram 41.0.0.13.92 Android (23/6.0.1; 480dpi; 1080x1920; samsung; SM-G610M; on7xelte; samsungexynos7870; pt_BR; 103516666)  
Mozilla/5.0 (Linux; Android 8.0.0; SM-A520F Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36 Instagram 46.0.0.15.96 Android (26/8.0.0; 480dpi; 1080x1920; samsung; SM-A520F; a5y17lte; samsungexynos7880; pt_BR; 109556226)

Edit 2020-05/2022-05: Currently this endpoint returns less information but username is still present:

{
    "user": {
        "username": "meram.ba",
        "pk": 6817966272,
        "profile_pic_url": "https://instagram.fbkk5-5.fna.fbcdn.net/v/t51.2885-19/s150x150/93795245_228684278392964_3931284463400517632_n.jpg?_nc_ht=instagram.fbkk5-5.fna.fbcdn.net&_nc_ohc=O6-_BTmMHUIAX9Qy8up&oh=8303e6fc8444b920c0c0edc603c917b5&oe=5EE91CF3"
    },
    "status": "ok"
}
Granitosaurus
  • 20,530
  • 5
  • 57
  • 82
  • 4
    This was working until recently. It is now returning `useragent mismatch` – Felipe Oct 17 '19 at 19:47
  • 1
    thanks for pointing this out @Felipe. I dug around and it turns out this api endpoint is only serving some specific user agents like iphone's instagram app etc, check out my edit for more information! – Granitosaurus Oct 18 '19 at 11:16
  • it was working with the iPhone user agent before, but now it is returning JSON with low-quality dp. – aadi1295 Nov 02 '19 at 23:13
  • I can confirm it's working as of May 2020, haven't found any other working solution for this. Thanks! – btuso May 20 '20 at 18:37
  • This doesn't seem to be working anymore... Any workaround? – wolnavi Jul 16 '20 at 10:35
  • @wolnavi are you sure you're setting User-Agent to iPhone as described in the edit? I've just tested it and it's working. – Granitosaurus Jul 17 '20 at 06:14
  • I am sorry @Granitosaurus , I thought it wasn't working because I was getting a "Page Not Found", but actually the user I was querying for might have their account deleted/banned... Testing it again with a valid account works... So sorry for the inconvenience, thanks – wolnavi Jul 18 '20 at 19:28
  • @Granitosaurus It works great. but I need it in the other way around, which means I have the username and need the user id. any solution/api for that which doesn't need logging in? – Soheil Nov 01 '20 at 15:12
  • 1
    @Soheil for other way around you can find user id in page source of user profile: make GET request to https://instragram/ and user id is referenced multiple times in the body under `"id"`, e.g. `"id": "1234567"`. – Granitosaurus Nov 03 '20 at 05:30
  • @Granitosaurus it used to be a solution. but now you have to log in first so that Instagram opens the page of the user – Soheil Nov 03 '20 at 10:02
  • 1
    @Soheil you don't; just open up a new question if you need more explanation. For example `curl "https://www.instagram.com/wraptile_/" | rg -o '"id":".+?"'` will find you my id just fine! – Granitosaurus Nov 06 '20 at 01:53
  • @Granitosaurus https://stackoverflow.com/questions/64647188/ then why it is not like that for me and some others?! – Soheil Nov 06 '20 at 06:15
9

If you don't have approved instagram application I suggest to use this php library: https://github.com/postaddictme/instagram-php-scraper

$instagram = Instagram::withCredentials('username', 'password', 'path/to/cache/');
$account = $instagram->getAccountById('193886659');
echo $account->getUsername();

Or direct access:

https://www.instagram.com/query/?q=ig_user(3){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}

Update: This url doesn't work anymore. You need to use POST. See repo to know how

raiym
  • 1,439
  • 1
  • 28
  • 54
7

Because of the Instagram limitation on its API (i mean the SandBox), It's not easy to use instagram official API for such cases so i coded a snippet in Python which converts Instagram user ID to username & vice versa.

It uses GraphQL API to get information without any limitation from Instagram.

[+] Code Updated due to Instagram API changes (30/5/2019)

import json
import requests
import re
import hashlib


def usernameToUserId(user):
    r1 = requests.get('https://www.instagram.com/web/search/topsearch/?query=' + user, headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'}).text

    if json.loads(r1)['users'][0]['user']['username'] == user:
        return json.loads(r1)['users'][0]['user']['pk']


def useridToUsername(id):
    if str(id).isnumeric():
        r1 = requests.get('https://instagram.com/instagram/', headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0', }).text
        rhx_gis = json.loads(re.compile('window._sharedData = ({.*?});', re.DOTALL).search(r1).group(1))['nonce']

        ppc = re.search(r'ProfilePageContainer.js/(.*?).js', r1).group(1)
        r2 = requests.get('https://www.instagram.com/static/bundles/es6/ProfilePageContainer.js/' + ppc + '.js').text
        query_hash = re.findall(r'{value:!0}\);const o=\"(.*?)\"', r2)[0]

        query_variable = '{"user_id":"' + str(id) + '","include_reel":true}'
        t = rhx_gis + ':' + query_variable
        x_instagram_gis = hashlib.md5(t.encode("utf-8")).hexdigest()

        header = {'X-Instagram-GIS': x_instagram_gis,
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
                  'X-Requested-With': 'XMLHttpRequest'}
        r3 = requests.get(
            'https://www.instagram.com/graphql/query/?query_hash=' + query_hash + '&variables=' + query_variable,
            headers=header).text

        username = json.loads(r3)['data']['user']['reel']['user']['username']
        return username


#print(useridToUsername("1234567890"))
#print(usernameToUserId("TheUserName"))

Github Link For more Updates: https://github.com/Snbig/InstaTrack

Snbig
  • 71
  • 1
  • 3
6

You have to use this Instagram API:

https://api.instagram.com/v1/users/{user-id}/?access_token=ACCESS-TOKEN

The response will have username, full name, bio, follower counts and other info.

But you have to get you app approved by Instagram before you can access API.

You can try https://www.picodash.com, it allows you to search for id and get user results, but it will be a manual process to search one-by-one and get the info

krisrak
  • 12,882
  • 3
  • 32
  • 46
4

Now that the Instagram API referenced in the approved answer is no longer available, you'll have to resort to scraping described in the other answer. Having said that, I ended up at this SO because I was trying to figure out what a user changed their username to.

Here's what I figured out: if you've got a user ID, you've probably been scraping or use something like 4K Stogram. If that's the case, you may very well have a media page's direct URL that looks something like: https://www.instagram.com/p/Bjh4rbdHcCU/ Easiest way to get the new username is just to go directly to that URL which will have been updated with the new username. If you're automating it, it's pretty easy to pull the new username from the page's <title> element.

I know the original poster's question was a bit different than this, but hopefully this is a solution for some of the people that end up on this page.

Matt K
  • 438
  • 3
  • 9
  • what about users with private pages? – M.H Mighani Nov 02 '19 at 08:39
  • There are no private pages. Just private users. Unless you’re seeing something different? – Matt K Nov 02 '19 at 22:34
  • I'm building an app that tracks users by their usernames, it tracks their profile pics and their bios and notifies me when they change and they are mostly private users. sometimes some of them change their username.I have their user id but I don't know how to find their new username... do you have any idea? I don't have any post url ... so I can't use the solution you suggested... – M.H Mighani Nov 03 '19 at 08:22
  • Nope, no idea. I’d suggest starting to store a post URL as that’s the only way I’ve seen it work. – Matt K Nov 03 '19 at 12:02
  • can't get posts for private users...for public pages it's possible but not for private pages – M.H Mighani Nov 03 '19 at 12:12
2

Grab screen name from a post by the user (e.g. https://www.instagram.com/p/Bqfhjk_AMTq/) in python:

import requests, re, json
from bs4 import BeautifulSoup
r = requests.get('https://www.instagram.com/p/Bqfhjk_AMTq/')
soup = BeautifulSoup(r.content, "lxml")
scripts = soup.find_all('script', type="text/javascript", text=re.compile('window._sharedData'))
stringified_json = scripts[0].get_text().replace('window._sharedData = ', '')[:-1]
print json.loads(stringified_json)['entry_data']['PostPage'][0]['graphql']['shortcode_media']['owner']['username']
lashgar
  • 5,184
  • 3
  • 37
  • 45
2

https://github.com/Snbig/InstaTrack works excellent for this, can process a .txt file with list of ID and get the users as a plaint text output also.

Camilo Caquimbo
  • 133
  • 1
  • 6
0

I made this. Put in the id variable whatever id you want to find out the name, in my case I am using 314216

id=314216 && curl --silent --header $'User-Agent: Instagram 12.0.0.16.90' https://i.instagram.com/api/v1/users/$id/info/ | jq --raw-output .user.username

Note: I made use of the curl and jq utilities here, install them if you don't have it

D.Snap
  • 1,704
  • 1
  • 22
  • 15
0

You may try Data365 Instagram API I am currently working on to get information by user_id from Instagram. To get the username, you should send requests including the profile_id (the same as the user_id), namely:

POST request to download profile data:

https://api.data365.co/v1.1/instagram/profile/{profile_id}/update?access_token=YOUR ACCESS TOKEN

GET request to receive profile data:

https://api.data365.co/v1.1/instagram/profile/{profile_id}?access_token=YOUR ACCESS TOKEN

You will receive a response not only a username but also a full name, biography, profile photo, business category, user gender and age, number of followers, followings, posts, and much more.

Here you can see the response example in JSON:

{
  "data": {
    "id": "314216",
    "username": "purr.in.ink",
    "full_name": "Luis Coelho",
    "profile_photo_url": "https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/s150x150/141493948_412460423353243_7908838244249710543_n.jpg?_nc_ht=scontent-frt3-1.cdninstagram.com&_nc_ohc=DOkixcVk5xcAX8LT62m&tp=1&oh=cc2ad6a4324e5a659553a19acecb3cc9&oe=60453164",
    "profile_photo_url_hd": "https://scontent-waw1-1.cdninstagram.com/v/t51.2885-19/s320x320/141493948_412460423353243_7908838244249710543_n.jpg?_nc_ht=scontent-waw1-1.cdninstagram.com&_nc_ohc=DOkixcVk5xcAX_1xSsV&tp=1&oh=b9c1f9ee57aa414addbd31413d26faa6&oe=6045B49C",
    "biography": "Life's less shitty\nwhen you see a kitty. \n▪️\nMy WebShop and more:",
    "external_url": "https://linktr.ee/Purrinink",
    "business_category": [
      "Restaurants"
    ],
    "age_approx": 42,
    "age_group": "35-49",
    "gender": "male",
    "langs": [
      "fr",
      "en"
    ],
    "is_private": true,
    "is_verified": true,
    "is_business_account": true,
    "followers_count": 3570057,
    "followings_count": 875,
    "posts_count": 4257,
    "highlight_reels_count": 5,
    "latest_location_id": "212988663"
  },
  "error": null,
  "status": "ok"
}

For more information, you can view https://data365.co/instagram