Last active
November 14, 2025 17:07
-
-
Save sclark39/9daf13eea9c0b381667b61e3d2e7bc11 to your computer and use it in GitHub Desktop.
Convert from Instagram Shortcode to Id and Back using big-integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var bigint = require( 'big-integer' ) | |
| var lower = 'abcdefghijklmnopqrstuvwxyz'; | |
| var upper = lower.toUpperCase(); | |
| var numbers = '0123456789' | |
| var ig_alphabet = upper + lower + numbers + '-_' | |
| var bigint_alphabet = numbers + lower | |
| function toShortcode( longid ) | |
| { | |
| var o = bigint( longid ).toString( 64 ) | |
| return o.replace(/<(\d+)>|(\w)/g, (m,m1,m2) => | |
| { | |
| return ig_alphabet.charAt( ( m1 ) | |
| ? parseInt( m1 ) | |
| : bigint_alphabet.indexOf( m2 ) ) | |
| }); | |
| } | |
| function fromShortcode( shortcode ) | |
| { | |
| var o = shortcode.replace( /\S/g, m => | |
| { | |
| var c = ig_alphabet.indexOf( m ) | |
| var b = bigint_alphabet.charAt( c ) | |
| return ( b != "" ) ? b : `<${c}>` | |
| } ) | |
| return bigint( o, 64 ).toString( 10 ) | |
| } | |
| toShortcode( '908540701891980503' ) // s.b. 'ybyPRoQWzX' | |
| fromShortcode( 'ybyPRoQWzX' ) // s.b. '908540701891980503' |
In Python if anyone is interested!
>>> id_to_shortcode = lambda instagram_id: base64.b64encode(instagram_id.to_bytes(9, 'big'), b'-_').decode().replace('A', ' ').lstrip().replace(' ', 'A') >>> def shortcode_to_id(shortcode): ... code = ('A' * (12-len(shortcode)))+shortcode ... return int.from_bytes(base64.b64decode(code.encode(), b'-_'), 'big')
Thanks for sharing the code for the python functions. It was very helpful to me. I organized them for better understanding:
import base64
def shortcode_to_id(shortcode: str) -> int:
code = ('A' * (12-len(shortcode)))+shortcode
return int.from_bytes(base64.b64decode(code.encode(), b'-_'), 'big')
def id_to_shortcode(pk: int) -> str:
bytes_str = base64.b64encode(pk.to_bytes(9, 'big'), b'-_')
return bytes_str.decode().replace('A', ' ').lstrip().replace(' ', 'A')
print(id_to_shortcode(3549837394260045367))
print(shortcode_to_id("DFDjk1fRK43"))hii anyone found how to get asset id from shortcodes ?
it's not possible.
Here is a simpler way to do the decode part using built-in BigInt and Base64URL handling. This is typescript but you can easily remove the types if you want.
function instagramShortCodeToId(shortCode: string) {
const paddedShortCode = shortCode.padStart(12, 'A');
//const bytes = Buffer.from(paddedShortCode, 'base64url'); // if you want to use Node
const bytes = Uint8Array.fromBase64(paddedShortCode, { alphabet: 'base64url' });
let id = 0n; // this is a BigInt
for (let i = 0; i < bytes.length; i++) {
// shift by one byte and add next byte
id = (id << 8n) + BigInt(bytes[i]);
}
return id;
}and a bonus of extracting the timestamp in milliseconds
function instagramIdToTimestampMs(id: bigint) {
const instagramEpochMs = 1314220021721n;
const offsetTimestamp = id >> (64n - 41n);
const timestampMs = offsetTimestamp + instagramEpochMs;
return Number(timestampMs);
}
function instagramShortCodeToTimestampMs(shortCode: string) {
return instagramIdToTimestampMs(instagramShortCodeToId(shortCode));
}implementing the encode should be pretty straightforward using the same approach.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone comes across this... this short code converter only works to generate the ig_id, but the facebook graph api ids are different and this doesn't work for thate
Examples shown above:
17889455560051444
17883949432067035
These are both ids from the ig graph api