Last updated
What is an Instagram Shortcode — Examples
Instagram shortcodes are the alphanumeric identifiers in Instagram post URLs. They are Base64-encoded versions of the underlying numeric media ID. Here are examples showing how they work.
Anatomy of an Instagram URL
// Instagram post URL structure
// https://www.instagram.com/p/SHORTCODE/
// Example:
// https://www.instagram.com/p/CxYz1234abc/
// ^^^^^^^^^^^
// This is the shortcode
// The shortcode encodes the numeric media ID
// CxYz1234abc → some large numeric ID
Decoding a Shortcode to Media ID
// JavaScript — decode Instagram shortcode to numeric ID
function shortcodeToId(shortcode) {
// Instagram uses URL-safe Base64 alphabet
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
let id = 0n;
for (const char of shortcode) {
id = id * 64n + BigInt(ALPHABET.indexOf(char));
}
return id.toString();
}
// Example
console.log(shortcodeToId("CxYz1234abc"));
# Python — decode Instagram shortcode to numeric ID
def shortcode_to_id(shortcode):
"""Decode an Instagram shortcode to its numeric media ID."""
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
id_val = 0
for char in shortcode:
id_val = id_val * 64 + ALPHABET.index(char)
return id_val
# Example
print(shortcode_to_id("CxYz1234abc"))
Encoding a Media ID to Shortcode
# Python — encode a numeric media ID to shortcode
def id_to_shortcode(media_id):
"""Encode a numeric media ID to an Instagram shortcode."""
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
shortcode = ""
id_val = int(media_id)
while id_val > 0:
shortcode = ALPHABET[id_val % 64] + shortcode
id_val //= 64
return shortcode
# Round-trip test
media_id = 1234567890123456789
shortcode = id_to_shortcode(media_id)
decoded = shortcode_to_id(shortcode)
print(f"Media ID: {media_id}")
print(f"Shortcode: {shortcode}")
print(f"Decoded back: {decoded}")
print(f"Match: {str(media_id) == decoded}")
Extracting Shortcode from URL
// JavaScript — extract shortcode from any Instagram URL
function extractShortcode(url) {
const patterns = [
/instagram\.com\/p\/([A-Za-z0-9_-]+)/,
/instagram\.com\/reel\/([A-Za-z0-9_-]+)/,
/instagram\.com\/tv\/([A-Za-z0-9_-]+)/,
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match) return match[1];
}
return null;
}
// Examples
const urls = [
"https://www.instagram.com/p/CxYz1234abc/",
"https://www.instagram.com/reel/CxYz1234abc/?igshid=abc",
"https://www.instagram.com/p/CxYz1234abc/?utm_source=ig_web",
];
urls.forEach(url => {
console.log(extractShortcode(url));
});
Timestamp from Instagram Media ID
# Python — extract creation timestamp from Instagram media ID
# Instagram media IDs encode a timestamp similar to Twitter Snowflake IDs
def get_instagram_post_date(shortcode):
"""
Extract the approximate creation date from an Instagram shortcode.
Instagram media IDs encode a timestamp in their upper bits.
"""
media_id = shortcode_to_id(shortcode)
# Instagram uses a similar approach to Twitter Snowflake
# The timestamp is in the upper bits of the media ID
# Instagram's epoch is approximately January 1, 2011
INSTAGRAM_EPOCH_MS = 1293840000000 # approximate
# Extract timestamp (upper bits)
timestamp_ms = (media_id >> 23) + INSTAGRAM_EPOCH_MS
from datetime import datetime, timezone
dt = datetime.fromtimestamp(timestamp_ms / 1000, tz=timezone.utc)
return dt.strftime("%Y-%m-%d %H:%M:%S UTC")
# Note: This is an approximation — exact epoch may vary
Using Shortcodes in the Instagram API
# Python — use shortcode with Instagram Graph API
import requests
def get_post_by_shortcode(shortcode, access_token):
"""
Look up an Instagram post by its shortcode using the Graph API.
First convert shortcode to media ID, then query the API.
"""
media_id = shortcode_to_id(shortcode)
url = f"https://graph.instagram.com/{media_id}"
params = {
"fields": "id,media_type,timestamp,permalink",
"access_token": access_token
}
response = requests.get(url, params=params)
return response.json()
# Example
# post = get_post_by_shortcode("CxYz1234abc", "YOUR_ACCESS_TOKEN")
Shortcode Alphabet Reference
- Instagram uses URL-safe Base64 encoding
- Alphabet: A-Z (26) + a-z (26) + 0-9 (10) + - (1) + _ (1) = 64 characters
- Each character encodes 6 bits
- Older posts have shorter shortcodes (smaller numeric IDs)
- Newer posts have longer shortcodes (larger numeric IDs)
- The shortcode is stable — it never changes for a given post
Instagram shortcodes are a compact, URL-safe representation of the underlying numeric media ID. Understanding the encoding allows you to convert between shortcodes and IDs, extract timestamps, and build tools that work with Instagram content programmatically.