How to Get Instagram User ID from Username (and Vice Versa)
When you're building tools that interact with Instagram data — influencer discovery platforms, competitor trackers, lead-enrichment pipelines — you'll quickly run into the fact that many API endpoints require the numeric user ID rather than the human-readable username. For example, to fetch a user's posts, Reels, or tagged media, you need their internal Instagram user ID, not just their @username.
This guide shows you how to convert an Instagram username to its numeric user ID, and how to reverse the lookup (user ID back to username) using the Instagram Cheapest API. We'll cover both directions with Python and curl examples, explain why you need the user ID, and walk through a complete workflow that handles both lookups.
Why you need the Instagram user ID
Instagram internally identifies every account with a unique numeric ID — a long integer like 123456789. While you interact with accounts using their username (for example, @nike), several API operations require the user ID instead:
- Fetching user media: The
user_mediaendpoint needsuser_idto return a user's posts. - Fetching user Reels: The
user_reelsendpoint requiresuser_id. - Fetching tagged media: The
user_tag_mediaendpoint also requires the numeric ID. - Looking up user profiles by ID: The
userinfo_by_user_idendpoint retrieves profile details when you only have the ID.
The username is human-friendly but can change; the user ID is permanent and serves as the stable identifier for all backend operations. So your first step when working with a username is almost always to resolve it to the user ID.
Converting username to user ID
The Instagram Cheapest API provides the userinfo endpoint, which accepts a username and returns the full user profile — including the numeric user ID.
Endpoint details: userinfo
- Method:
GET - URL:
https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/user/{username} - Path parameter:
{username}— the Instagram username (without the@symbol) - Optional parameter:
fields— comma-separated field names to reduce response size - Required headers:
x-rapidapi-host: instagram-cheapest.p.rapidapi.com
x-rapidapi-key: YOUR_RAPIDAPI_KEY
Python example: username to user ID
import requests
import json
def get_user_id_from_username(username, api_key):
"""Fetch Instagram user profile and extract the numeric user ID."""
url = f"https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/user/{username}"
headers = {
"x-rapidapi-host": "instagram-cheapest.p.rapidapi.com",
"x-rapidapi-key": api_key
}
response = requests.get(url, headers=headers)
response.raise_for_status()
user_data = response.json()
# The exact field name depends on the response structure;
# inspect the JSON to find the user ID field (commonly 'id', 'user_id', or 'pk')
return user_data
# Usage
api_key = "YOUR_RAPIDAPI_KEY"
username = "nike"
user_profile = get_user_id_from_username(username, api_key)
print(json.dumps(user_profile, indent=2))
# Extract the user ID from the response
# Example: user_id = user_profile['id'] or user_profile['pk']
The response is raw JSON containing the user's profile: follower count, bio, profile picture URL, and crucially, the numeric user ID. Print the full JSON to see the structure, then extract the field that holds the ID. Common field names are id, pk, or user_id, depending on what Instagram returns.
curl example: quick username lookup
To test the lookup from the command line, use curl:
curl -X GET \
'https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/user/nike' \
-H 'x-rapidapi-host: instagram-cheapest.p.rapidapi.com' \
-H 'x-rapidapi-key: YOUR_RAPIDAPI_KEY'
This prints the full profile JSON. Pipe it to jq to extract just the user ID:
curl -X GET \
'https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/user/nike' \
-H 'x-rapidapi-host: instagram-cheapest.p.rapidapi.com' \
-H 'x-rapidapi-key: YOUR_RAPIDAPI_KEY' | jq '.id'
Adjust the jq selector (.id, .pk, or .user_id) to match the actual field name in the response.
Converting user ID back to username
Sometimes you have a numeric user ID — perhaps from a cached database or from another API response — and you need to resolve it back to the current username. The username_by_uid endpoint handles this reverse lookup.
Endpoint details: username_by_uid
- Method:
GET - URL:
https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/username_by_uid?uid={id} - Query parameter:
uid— the numeric Instagram user ID - Optional parameter:
fields— comma-separated field names - Required headers: same as above
Python example: user ID to username
def get_username_from_user_id(user_id, api_key):
"""Resolve a numeric Instagram user ID back to the username."""
url = "https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/username_by_uid"
headers = {
"x-rapidapi-host": "instagram-cheapest.p.rapidapi.com",
"x-rapidapi-key": api_key
}
params = {
"uid": user_id
}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
# Usage
user_id = "123456789"
username_data = get_username_from_user_id(user_id, api_key)
print(json.dumps(username_data, indent=2))
# Extract the username field from the response
# Example: username = username_data['username']
The response returns JSON with the username and possibly additional profile fields. Parse the response to extract the username field.
curl example: reverse lookup
curl -X GET \
'https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/username_by_uid?uid=123456789' \
-H 'x-rapidapi-host: instagram-cheapest.p.rapidapi.com' \
-H 'x-rapidapi-key: YOUR_RAPIDAPI_KEY'
Again, pipe to jq '.username' to extract just the username string.
A complete bidirectional workflow
Here's a Python script that demonstrates both directions: username → user ID, then user ID → username, and finally fetching the user's posts using the resolved ID:
import requests
import json
def get_user_profile(username, api_key):
"""Fetch user profile by username."""
url = f"https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/user/{username}"
headers = {
"x-rapidapi-host": "instagram-cheapest.p.rapidapi.com",
"x-rapidapi-key": api_key
}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def get_username_by_id(user_id, api_key):
"""Resolve user ID back to username."""
url = "https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/username_by_uid"
headers = {
"x-rapidapi-host": "instagram-cheapest.p.rapidapi.com",
"x-rapidapi-key": api_key
}
params = {"uid": user_id}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
def get_user_media(user_id, api_key):
"""Fetch user's media posts by numeric user ID."""
url = "https://instagram-cheapest.p.rapidapi.com/api/v1/instagram/user_media"
headers = {
"x-rapidapi-host": "instagram-cheapest.p.rapidapi.com",
"x-rapidapi-key": api_key
}
params = {"user_id": user_id}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
# Main workflow
api_key = "YOUR_RAPIDAPI_KEY"
username = "nike"
# Step 1: Username → User ID
print(f"Looking up user: {username}")
profile = get_user_profile(username, api_key)
user_id = profile.get('id') or profile.get('pk') # Adjust field name as needed
print(f"User ID: {user_id}")
# Step 2: User ID → Username (reverse check)
print(f"\nReverse lookup for user ID: {user_id}")
username_data = get_username_by_id(user_id, api_key)
resolved_username = username_data.get('username')
print(f"Resolved username: {resolved_username}")
# Step 3: Fetch user media using the user ID
print(f"\nFetching media for user ID: {user_id}")
media = get_user_media(user_id, api_key)
print(f"Media fetched: {json.dumps(media, indent=2)[:500]}...")
This script performs a round trip: it starts with a username, resolves it to a user ID, confirms the reverse lookup returns the same username, and then uses the user ID to fetch the user's posts. This pattern is the foundation for most Instagram data workflows.
When to use each endpoint
Here's a quick decision guide:
- You have a username and need the user ID: Use
userinfo→/api/v1/instagram/user/{username}. Extract the ID from the profile JSON. - You have a user ID and need the username: Use
username_by_uid→/api/v1/instagram/username_by_uid?uid={id}. - You want the full profile by username: Use
userinfo— it returns follower count, bio, profile picture, and more. - You want the full profile by user ID: Use
userinfo_by_user_id→/api/v1/instagram/user_by_user_id?user_id={id}.
The userinfo and userinfo_by_user_id endpoints return the complete profile, while username_by_uid is optimized for the lightweight reverse lookup when you only need the username string.
Using the fields parameter to save bandwidth
Profile JSON can be verbose. If you only need the user ID or username, use the fields parameter to request just that field:
# Request only the user ID field
params = {
"fields": "id" # or "pk", "username", etc., depending on the response structure
}
This shrinks the response payload from potentially tens of kilobytes down to a few hundred bytes, which keeps you inside the API's 10 GB/month bandwidth allowance and avoids overage charges. For bulk lookups across thousands of users, the bandwidth savings add up fast.
Pricing and cost control
Each API call (to userinfo, username_by_uid, or user_media) counts as one request. At scale, your cost is:
- Free Basic tier: 30 requests/month (great for testing)
- Pro tier ($59/mo): $0.13 per 1,000 requests
- Ultra tier ($119/mo): $0.11 per 1,000 requests
- Mega tier ($249/mo): $0.10 per 1,000 requests
For example, if you're resolving 50,000 usernames to user IDs per month, that's 50,000 requests — around $5 to $6.50 on the Mega or Pro tier. Use the fields parameter to keep bandwidth under the 10 GB allowance, and your total cost stays predictable.
Common use cases
Here are a few scenarios where username ↔ user ID conversion is essential:
- Influencer discovery: You scrape usernames from hashtag pages or competitor followers, then resolve each to a user ID to fetch their posts, Reels, and engagement metrics.
- Lead enrichment: You have a list of Instagram usernames from a CRM; resolve them to user IDs, fetch profiles, and enrich your database with follower counts and bios.
- Competitor tracking: Monitor a competitor's username, resolve it to the user ID, and periodically fetch their latest posts and Reels to track content strategy.
- Username change detection: Store user IDs in your database; periodically reverse-lookup the ID to detect if the username has changed.
Compliance and responsible use
This API returns public Instagram data only. You are responsible for using it in compliance with Instagram's terms of service and applicable privacy laws (GDPR, CCPA). Always confirm you have a legitimate basis for collecting and processing user data. The API is not affiliated with or endorsed by Meta or Instagram.
Conclusion
Converting between Instagram username and user ID is a foundational operation for any tool that works with Instagram data. The Instagram Cheapest API provides two simple endpoints — userinfo for username → user ID and username_by_uid for the reverse — that return real-time, uncached data at as low as $0.10 per 1,000 requests. Combined with endpoints like user_media and user_reels, you have everything you need to build scalable Instagram data pipelines on a budget.
Start with the free 30-request tier to test your integration, then scale up as your usage grows. With Python or curl and a valid RapidAPI key, you can resolve usernames to IDs (and back) in seconds.
Compliance note: this API returns public Instagram data only. You are responsible for complying with Instagram's terms and applicable privacy law (GDPR/CCPA). Not affiliated with or endorsed by Meta/Instagram.