Instagram how to get my user id from username?

Viewed 386458

I'm in the process of embedding my image feed in my website using JSON, the URL needs my user id so I can retrieve this feed.

So, where can I find/get my user id?

29 Answers

Working solution without access token as of October-14-2018:

Search for the username:

https://www.instagram.com/web/search/topsearch/?query=<username>

Example:

https://www.instagram.com/web/search/topsearch/?query=therock

This is a search query. Find the exact matched entry in the reply and get user ID from the entry.

Working Solution December 14, 2020

For simple usage like 3rd party tools that require an Instagram user ID (like embedding an image feed) I tend to use:

https://www.thekeygram.com/find-instagram-user-id/

because it makes it really easy to copy and paste the Instagram user ID that I am looking for. Unlike most tools I get the results fast, it's free and there are no ads. I recommend you watch the youtube video before using it so you can see how simple it is and get an idea of how it's used:

https://www.youtube.com/watch?v=9HvOroY-YBw

For more advanced usage I recommend:

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

(replace username with the requested username)

For example to find the user ID of the username "instagram" you would use:

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

This is the most advanced way which returns a JSON response and it's great if you are building an app that requires the raw data. You can save it in a database or build some type of front end UI to display it. Example: for a dashboard or on a website. Also, using the url is great because you can get additional attributes about users such as their total follower count and profile bio.

Since adding ?__a=1 to a profile URL is not working anymore to get a user ID from a username, we can do it with cURL and jq (the new API endpoint can be found in the network requests of Instagram web version, for example with Firefox Developer Tools):

curl -s 'https://i.instagram.com/api/v1/users/web_profile_info/?username=alanarblanchard' -H 'X-IG-App-ID: 936619743392459' | jq -r .data.user.id

If you are using Instagram in a web browser, you don't need to use the command above and can check the response of the HTTP request directly.

You may also be interested in finding the username from a user ID, in case someone changes frequently the username. I added an answer here: Instagram get username from userId

Working solution ~2018

I've found that, providing you have an access token, you can perform the following request in your browser:

https://api.instagram.com/v1/users/self?access_token=[VALUE]

In fact, access token contain the User ID (the first segment of the token):

<user-id>.1677aaa.aaa042540a2345d29d11110545e2499

You can get an access token by using this tool provided by Pixel Union.

Python solution with Instaloader external library (install it first with pip)

import instaloader

YOUR_USERNAME = "Your username here"
USERNAME_OF_INTEREST = "Username of interest here"

L = instaloader.Instaloader()
L.interactive_login(YOUR_USERNAME)
profile = instaloader.Profile.from_username(L.context, USERNAME_OF_INTEREST)
print(profile.userid)

With this kind of questions about constantly changing private APIs, I recommend to rely on actively developing libraries, not on the services or answers.

As of june 2022, you can to run or intercept a special HTTP request in order to successfully get the user data (and user ID). If you use Puppeteer, you can intercept the request that Instagram makes in the browser, and read its response. Example code:

const username = 'user.account';

const page = await browser.newPage();

const [foundResponse] = await Promise.all([
  page.waitForResponse((response) => {
    const request = response.request();
    return request.method() === 'GET' && new RegExp(`https:\\/\\/i\\.instagram\\.com\\/api\\/v1\\/users\\/web_profile_info\\/\\?username=${encodeURIComponent(username.toLowerCase())}`).test(request.url());
  }),
  page.goto(`https://instagram.com/${encodeURIComponent(username)}`),
]);

const json = JSON.parse(await foundResponse.text());
console.log(json.data.user);

See discussion here: https://github.com/mifi/SimpleInstaBot/issues/125#issuecomment-1145354294

See also working code here: https://github.com/mifi/instauto/blob/2de64d9a30dad16c89a8c45f792e10f137a8e6cb/src/index.js#L250

Related