'Given a user_id, how do I find the username?
I have a list of thousands of instagram user-ids. How do I get their Instagram usernames/handles?
Solution 1:[1]
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"
}
Solution 2:[2]
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
Solution 3:[3]
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
Solution 4:[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.
Solution 5:[5]
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']
Solution 6:[6]
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.
Solution 7:[7]
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
Solution 8:[8]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Matt K |
| Solution 5 | |
| Solution 6 | Camilo Caquimbo |
| Solution 7 | D.Snap |
| Solution 8 | Michael Kris |
