How to retrieve Instagram username from User ID?

Solution 1:

One can use https://i.instagram.com/api/v1/users/USERID/info (replace USERID by the user id). It returns a JSON in which the field user.username is that user's username.

With curl and jq installed, this shell oneliner will directly return the username:

curl -s https://i.instagram.com/api/v1/users/USERID/info/ | jq -r .user.username

However, keep in mind that this isn't an official API endpoint.

Solution 2:

You can use this API (from official instagram site) to fetch username by user static id:

GET https://www.instagram.com/graphql/query/?query_hash=c9100bf9110dd6361671f113dd02e7d6&variables={"user_id": ${user_id}}

Replace ${user_id} with actual user id.

const username = result.data.user.reel.owner.username