Back to Blog
Instagram

Instagram Shortcode to Media ID (and Back): Convert Post URLs in Code

October 18, 2026
7 min read
S
By SociaVault Team
Instagramshortcodemedia IDInstagram APIBigInt

Instagram Shortcode to Media ID (and Back): Convert Post URLs in Code

Every Instagram post goes by two names. There's the short one in the link, like the Dd1gQBsCWE8 in instagram.com/reel/Dd1gQBsCWE8/. And there's a long number, 3996242083173720380, which is what Instagram's own systems use.

Your marketing team pastes links into a sheet. Your database keys on IDs. At some point somebody needs to match one against the other, and the first instinct is to call an API for every row.

You don't have to. The two names are the same number written two different ways, and you can convert between them with a few lines of code and no network call at all.

The shortcode is the ID, written in base 64

Instagram writes the media ID in base 64 using this alphabet:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_

A is 0, B is 1, and so on through the lowercase letters and digits, up to _ at 63. Read a shortcode as a base-64 number and you get the media ID. Write the ID out in base 64 and you get the shortcode back.

We didn't want to trust the theory, so we tested it on real posts, including a reel NASA published this month. All four converted exactly, in both directions:

ShortcodeMedia ID
DH3tWudxIKB3600545900919030401
DEiyb48AeB93540614075954356349
DF5s0duxDts3565077699422862188
Dd1gQBsCWE83996242083173720380

The code

JavaScript needs BigInt for this, and not as a nice-to-have. We'll get to why in a moment.

const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

function shortcodeToId(shortcode) {
  let id = 0n;
  for (const ch of shortcode) {
    const v = ALPHABET.indexOf(ch);
    if (v === -1) throw new Error(`not a shortcode character: ${ch}`);
    id = id * 64n + BigInt(v);
  }
  return id.toString(); // keep it a string
}

function idToShortcode(mediaId) {
  // also accepts IDs like "3996242083173720380_528817151"
  let n = BigInt(String(mediaId).split("_")[0]);
  let code = "";
  while (n > 0n) {
    code = ALPHABET[Number(n % 64n)] + code;
    n /= 64n;
  }
  return code;
}

// Pull the shortcode out of a post or reel link
function shortcodeFromUrl(url) {
  const m = String(url).match(/instagram\.com\/(?:p|reels?|tv)\/([A-Za-z0-9_-]+)/);
  return m ? m[1] : null;
}

shortcodeToId("Dd1gQBsCWE8");         // "3996242083173720380"
idToShortcode("3996242083173720380"); // "Dd1gQBsCWE8"

Python's integers have no size limit, so the Python version has nothing to trip over:

import re

ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

def shortcode_to_id(shortcode):
    n = 0
    for ch in shortcode:
        n = n * 64 + ALPHABET.index(ch)
    return str(n)

def id_to_shortcode(media_id):
    n, code = int(str(media_id).split("_")[0]), ""
    while n > 0:
        n, r = divmod(n, 64)
        code = ALPHABET[r] + code
    return code

def shortcode_from_url(url):
    m = re.search(r"instagram\.com/(?:p|reels?|tv)/([A-Za-z0-9_-]+)", url)
    return m.group(1) if m else None

Why JavaScript gets this wrong without BigInt

An Instagram media ID is around four quintillion. JavaScript's ordinary numbers can only represent whole numbers exactly up to 9,007,199,254,740,991. Above that, they round to the nearest value they can store, and they don't tell you.

So Number("3996242083173720380") gives you 3996242083173720600. No error, no warning. Just a different number, pointing at no post at all. Do the shortcode maths with ordinary numbers and you get the same drift, which is why the code above sticks to BigInt from start to finish.

It can happen before your code even runs. If an ID ever reaches you as a bare number in JSON rather than a string, JSON.parse rounds it on the way in. That's why we take media IDs from string fields, or derive them from the shortcode, and never trust a numeric ID field that has been through a JavaScript parser. A tell-tale sign: an ID that ends in a suspicious 00 where its string twin doesn't.

What's the number after the underscore?

Instagram responses often carry the ID as a string like 3996242083173720380_528817151. The first half is the media ID. The second half is the account that posted it.

We checked rather than assumed. 528817151 is NASA's Instagram account ID, and the post-info response for that reel named nasa as the owner. So one field gets you both the post and its author, with no extra lookup. Split on the underscore and keep both halves as strings. If you're tracking accounts by ID, which you should be, finding user IDs on Instagram, TikTok and X covers the account side.

Where each value sits in our responses

EndpointShortcodeID string
/instagram/postsitems[].codeitems[].id
/instagram/reelsitems[].media.codeitems[].media.id
/instagram/post-infoxdt_shortcode_media.shortcodexdt_shortcode_media.id

On post-info, the media object sits a level or two down inside the response's data, so read the path defensively.

/instagram/post-info takes the post's url, and it doesn't care whether that's a /p/ link or a /reel/ link. We sent NASA's reel both ways and got the identical post back, same shortcode, same ID. The shortcode itself doesn't change with the path, so /p/Dd1gQBsCWE8/ and /reel/Dd1gQBsCWE8/ are the same post. Normalise to one form before you store links and your duplicate count drops.

If you need the post's stats rather than just its ID, our guides to the Instagram posts endpoint and the reels endpoint cover the fields.

A quick sanity check for your pipeline

Because the conversion is pure arithmetic, it doubles as a free integrity check. Run every stored shortcode through shortcodeToId and compare the result with the ID you stored next to it. Any row that doesn't match has a corrupted ID, almost always from passing through a numeric type somewhere. On a large table this catches problems that would otherwise surface months later as "missing" posts.

What the conversion won't tell you

  • Whether the post still exists. It's maths, not a lookup. A deleted post converts just as happily as a live one.
  • Who posted it, from the shortcode alone. The owner only comes with the underscore form of the ID, or from a post-info call.
  • Anything about other kinds of ID. Comment IDs, story IDs and account IDs aren't shortcodes. Don't run them through this.
  • That Instagram won't change it. The scheme held on every post we checked, from January 2025 to this month, but it isn't documented. Keep the original shortcode alongside the ID, just in case.

Frequently Asked Questions

What is an Instagram shortcode?

It's the short code in a post or reel URL, like Dd1gQBsCWE8 in instagram.com/reel/Dd1gQBsCWE8/. It's the post's numeric media ID written in base 64.

How do I convert an Instagram shortcode to a media ID?

Treat each character as a digit in base 64, using the alphabet A–Z, a–z, 0–9, - and _, and build up the number. In JavaScript use BigInt; in Python ordinary integers work. The code above does it in a few lines.

How do I get the post URL from a media ID?

Convert the ID back to a shortcode and put it in a URL: https://www.instagram.com/p/ followed by the shortcode. If your ID has an underscore, use only the part before it.

Why does my converted media ID end in 00?

It passed through an ordinary JavaScript number, which can't hold integers this large exactly, so it was rounded. Use BigInt for the maths and keep IDs as strings.

Is the shortcode the same for posts and reels?

Yes. The same post works at /p/ and at /reel/, and the shortcode doesn't change. We fetched a reel through both paths and got the same post.

What does the number after the underscore in an Instagram ID mean?

It's the ID of the account that posted it. In 3996242083173720380_528817151, the first part is the media ID and 528817151 is the owner's account ID.


Working from a pile of Instagram links? Sign up for 50 free credits and pull full post data for any shortcode you convert.

Found this helpful?

Share it with others who might benefit

Ready to Try SociaVault?

Start extracting social media data with our powerful API. No credit card required.