Convert Shortcode ↔ Media ID

📌 Try these examples:
INPUT
SHORTCODE
MEDIA ID (NUMBER)

Last updated

What is an Instagram Short Code Number?

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.

How Instagram Shortcodes Work

Instagram's Custom Base64 Alphabet:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_

Instagram uses a URL-safe base64 variant with '-' and '_' instead of '+' and '/'.

Key Facts

  • Instagram uses URL-safe Base64: A-Z, a-z, 0-9, -, _ (not + and /)
  • Each shortcode character represents 6 bits of the numeric ID
  • Shortcode and numeric ID are mathematically equivalent — same post, different representations
  • Always use BigInt in JavaScript when working with numeric IDs
  • The numeric ID also encodes the creation timestamp (right-shift by 23 bits + epoch)

Enter any Instagram shortcode or numeric ID to instantly convert between the two formats.

Convert Shortcodes in Code

JavaScript
// 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

Common Use Cases

  • API Integration: Convert shortcodes to media IDs for Instagram API calls
  • Data Analysis: Work with numeric IDs for database storage and analytics
  • Bot Development: Process Instagram posts using media IDs
  • URL Generation: Create Instagram post URLs from media IDs

Examples

Example 1: Shortcode ↔ Numeric ID Conversion

Instagram Base64 alphabet:
  A-Z = 0-25
  a-z = 26-51
  0-9 = 52-61
  -   = 62
  _   = 63

Shortcode to numeric ID:
  Shortcode: "B"
  Numeric ID: 1

  Shortcode: "BA"
  Numeric ID: 1 × 64 + 0 = 64

  Shortcode: "CxYz"
  C=2, x=49, Y=24, z=51
  = 2×64³ + 49×64² + 24×64 + 51
  = 524288 + 200704 + 1536 + 51
  = 726579

Numeric ID to shortcode (reverse):
  Numeric ID: 726579
  726579 ÷ 64 = 11352 remainder 51 → z
  11352 ÷ 64 = 177 remainder 24 → Y
  177 ÷ 64 = 2 remainder 49 → x
  2 ÷ 64 = 0 remainder 2 → C
  Read remainders in reverse: CxYz ✅

Example 2: JavaScript Conversion Functions

const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';

// Shortcode → Numeric ID
function shortcodeToNumber(shortcode) {
  let id = 0n;
  for (const char of shortcode) {
    const value = ALPHABET.indexOf(char);
    if (value === -1) throw new Error(`Invalid character: ${char}`);
    id = id * 64n + BigInt(value);
  }
  return String(id); // Return as string to preserve precision
}

// Numeric ID → Shortcode
function numberToShortcode(numericId) {
  let id = BigInt(numericId);
  if (id === 0n) return 'A';

  let shortcode = '';
  while (id > 0n) {
    shortcode = ALPHABET[Number(id % 64n)] + shortcode;
    id = id / 64n;
  }
  return shortcode;
}

// Examples:
console.log(shortcodeToNumber('CxYz123abcd'));
// → "2847392847392847392"

console.log(numberToShortcode('2847392847392847392'));
// → "CxYz123abcd"

// Verify round-trip:
const original = 'CxYz123abcd';
const numeric = shortcodeToNumber(original);
const restored = numberToShortcode(numeric);
console.log(original === restored); // → true

Example 3: Python Conversion Functions

ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'

def shortcode_to_number(shortcode: str) -> int:
    """Convert Instagram shortcode to numeric media ID."""
    media_id = 0
    for char in shortcode:
        media_id = media_id * 64 + ALPHABET.index(char)
    return media_id

def number_to_shortcode(numeric_id: int) -> str:
    """Convert numeric media ID to Instagram shortcode."""
    if numeric_id == 0:
        return 'A'
    shortcode = ''
    while numeric_id > 0:
        shortcode = ALPHABET[numeric_id % 64] + shortcode
        numeric_id //= 64
    return shortcode

# Examples:
print(shortcode_to_number('CxYz123abcd'))
# → 2847392847392847392

print(number_to_shortcode(2847392847392847392))
# → 'CxYz123abcd'

Frequently Asked Questions

An Instagram short code is a base64-encoded string that represents a numeric media ID. For example, shortcode 'CK1JqHqFVOy' converts to media ID 2501040375429168306. Instagram uses shortcodes in URLs to make them shorter and more readable.

Instagram shortcodes use a custom base64 alphabet (ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). Decode the shortcode using this alphabet to get the numeric media ID.

Instagram shortcodes appear in post URLs: instagram.com/p/SHORTCODE/. For example, in instagram.com/p/CK1JqHqFVOy/, the shortcode is 'CK1JqHqFVOy'.

Yes! This tool supports bidirectional conversion. Enter a numeric media ID to get the Instagram shortcode, or enter a shortcode to get the media ID.