InstagramAnswer

Instagram API get user ID and profile info by username

Our profile route returns an Instagram user ID, its Graph API ID and followers from a username for 1 credit. Meta finds only Business and Creator accounts.

4 min read

Short answer

  • An Instagram account has two numeric IDs: Instagram's own user ID and the 17841 ID that Meta's Graph API uses.
  • Meta's Business Discovery returns another account's Graph API ID only for Business or Creator accounts, called from your own Business account through Facebook Login.
  • Our profile route returns both IDs, the name, followers and following from a username or profile URL for 1 credit, with 100 free credits a month.
On this page

Send a username to our /v1/instagram/user/info route and you get the account's user ID, its Graph API ID, name and follower counts for 1 credit. Meta's own API looks up another account only if it is a Business or Creator account, and only from your own Business or Creator account.

Two IDs for one account

An Instagram account has two numeric IDs. id is Instagram's own user ID, the one our other Instagram routes take as user_id: 787132 for natgeo. fbid is the ID Meta's Graph API uses: 17841400573960012 for natgeo. For bluebottle, our fbid is 17841401441775531, the same ID Meta's documentation shows for that account.

What Meta's API returns

Meta's Business Discovery reads another account by username and returns its Graph API id with fields such as follower and media counts. You call it from your own Instagram Business or Creator account through Facebook Login, with permissions such as instagram_basic. It only returns Business or Creator accounts, and leaves out age-gated ones.

User ID and profile with ToolzerHub

We make ToolzerHub. Our Instagram API reads public Instagram accounts with one API key. /v1/instagram/user/info takes username_or_url, a username or a full profile URL, and costs 1 credit.

bash
curl "https://api.toolzerhub.com/v1/instagram/user/info?username_or_url=natgeo" \
  -H "x-api-key: $TOOLZERHUB_API_KEY"

The fields used here, from natgeo's response:

json
{
  "data": {
    "id": "787132",
    "username": "natgeo",
    "full_name": "National Geographic",
    "fbid": "17841400573960012",
    "is_private": false,
    "is_verified": true,
    "edge_followed_by": {
      "count": 268239859
    },
    "edge_follow": {
      "count": 195
    }
  }
}

edge_followed_by.count is followers and edge_follow.count is following. /v1/instagram/user/id returns the same id without fbid or counts, also for 1 credit.

Look up many accounts at once

The script takes usernames, @handles or profile URLs and writes both IDs, the name, followers, following and the verified and private flags to a CSV.

python
import csv
import os
import sys
import time

import requests

API = "https://api.toolzerhub.com/v1/instagram/user/info"
HEADERS = {"x-api-key": os.environ.get("TOOLZERHUB_API_KEY", "")}
FIELDS = ["input", "username", "user_id", "graph_id", "full_name", "followers", "following",
          "verified", "private"]


def cell(value):
    # A leading = + - or @ would run as a formula when the CSV opens in a spreadsheet.
    return "'" + value if isinstance(value, str) and value[:1] in ("=", "+", "-", "@") else value


def lookup(name, tries=4):
    # 429 and 503 are worth a retry; the body or the Retry-After header says how long to wait.
    for attempt in range(tries):
        r = requests.get(API, params={"username_or_url": name.lstrip("@")}, headers=HEADERS, timeout=90)
        if r.status_code in (429, 503) and attempt < tries - 1:
            wait = int(((r.json().get("error") or {}).get("details") or {}).get("retryAfter") or r.headers.get("Retry-After") or 5 * (attempt + 1))
            time.sleep(min(wait, 60))
            continue
        if r.status_code in (400, 404):  # not a valid username, or no such account
            return None
        r.raise_for_status()
        return r.json()["data"]


def main(names, out="instagram_user_ids.csv"):
    found = 0
    with open(out, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS)
        writer.writeheader()
        for name in [n.strip() for n in names.split(",") if n.strip()]:
            p = lookup(name) or {}
            found += bool(p)
            writer.writerow({
                "input": cell(name), "username": p.get("username"), "user_id": p.get("id"),
                "graph_id": p.get("fbid"), "full_name": cell(p.get("full_name")),
                "followers": (p.get("edge_followed_by") or {}).get("count"),
                "following": (p.get("edge_follow") or {}).get("count"),
                "verified": p.get("is_verified"), "private": p.get("is_private"),
            })
            print(name, "->", p.get("id") or "not found")
    print(f"found {found} user IDs, saved to {out}")


if __name__ == "__main__":
    if not HEADERS["x-api-key"] or len(sys.argv) < 2:
        sys.exit("usage: TOOLZERHUB_API_KEY=... python instagram_user_ids.py natgeo,@nasa,https://www.instagram.com/nike/")
    main(*sys.argv[1:])
bash
export TOOLZERHUB_API_KEY=your_key
python instagram_user_ids.py "natgeo,@nasa,https://www.instagram.com/nike/,bluebottle,spacex,bbcnews,nytimes,mosseri"
text
natgeo -> 787132
@nasa -> 528817151
https://www.instagram.com/nike/ -> 13460080
bluebottle -> 354032059
spacex -> 20311520
bbcnews -> 16278726
nytimes -> 1624593034
mosseri -> 95561
found 8 user IDs, saved to instagram_user_ids.csv

What the lookups returned

Eight inputs returned eight accounts in 12 seconds; the CSV also holds names, follower and following counts, which move between calls.

Username User ID Graph API ID
natgeo 787132 17841400573960012
nasa 528817151 17841401474538262
nike 13460080 17841400602400210
bluebottle 354032059 17841401441775531
spacex 20311520 17841400705600317
bbcnews 16278726 17841400681780254
nytimes 1624593034 17841400511045384
mosseri 95561 17841400946830001

All eight Graph API IDs start with 17841, while the Instagram IDs run from 5 digits (mosseri) to 10 (nytimes).

What it costs

Each lookup is 1 credit, so the 100 free credits a month cover 100 accounts. $19 buys 10,000 credits that never expire, and plans for higher volume cover larger lists. Set your key and your first account's IDs are one request away.

To list the account's posts or stories next, see Instagram API get user posts and Instagram API get stories; for the same job on Threads, see finding a Threads user ID.

We are not affiliated with Meta or Instagram.

Get this data from your own code

The Instagram API returns it as JSON with one x-api-key header. 100 free credits monthly.