Convert Shortcode ↔ Media ID
📌 Try these examples:
INPUT
SHORTCODE
MEDIA ID (NUMBER)
Convert Instagram shortcodes to numeric media IDs and vice versa
Instagram uses shortcodes in post URLs to make them shorter and more user-friendly. Each shortcode is a base64-encoded representation of a numeric media ID. For example, shortcode 'CK1JqHqFVOy' represents media ID 2501040375429168306.
Instagram's Custom Base64 Alphabet:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
Instagram uses a URL-safe base64 variant with '-' and '_' instead of '+' and '/'.
// JavaScript
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
function shortcodeToMediaId(shortcode) {
let mediaId = 0n;
for (const char of shortcode) {
mediaId = mediaId * 64n + BigInt(ALPHABET.indexOf(char));
}
return mediaId.toString();
}
function mediaIdToShortcode(mediaId) {
let id = BigInt(mediaId);
let shortcode = '';
while (id > 0n) {
shortcode = ALPHABET[Number(id % 64n)] + shortcode;
id = id / 64n;
}
return shortcode;
}
// Example
console.log(shortcodeToMediaId('CK1JqHqFVOy')); // 2501040375429168306
console.log(mediaIdToShortcode('2501040375429168306')); // CK1JqHqFVOy