Skip to content

Instantly share code, notes, and snippets.

@abir-taheer
Last active March 31, 2025 22:25
Show Gist options
  • Save abir-taheer/0d3f1313def5eec6b78399c0fb69e4b1 to your computer and use it in GitHub Desktop.
Save abir-taheer/0d3f1313def5eec6b78399c0fb69e4b1 to your computer and use it in GitHub Desktop.
"This is our community, this is our family, these are our friends." https://www.youtube.com/watch?v=gk7iWgCk14U&t=425s
if (window.location.origin !== "https://www.instagram.com") {
window.alert(
"Hey! You need to be on the instagram site before you run the code. I'm taking you there now but you're going to have to run the code into the console again.",
);
window.location.href = "https://www.instagram.com";
console.clear();
}
const fetchOptions = {
credentials: "include",
headers: {
"X-IG-App-ID": "936619743392459",
},
method: "GET",
};
let username;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
// This function handles all of the pagination logic
// Calls the API recursively until there are no more pages to load
const concatFriendshipsApiResponse = async (
list,
user_id,
count,
next_max_id = "",
) => {
let url = `https://www.instagram.com/api/v1/friendships/${user_id}/${list}/?count=${count}`;
if (next_max_id) {
url += `&max_id=${next_max_id}`;
}
const data = await fetch(url, fetchOptions).then((r) => r.json());
if (data.next_max_id) {
const timeToSleep = random(800, 1500);
console.log(
`Loaded ${data.users.length} ${list}. Sleeping ${timeToSleep}ms to avoid rate limiting`,
);
await sleep(timeToSleep);
return data.users.concat(
await concatFriendshipsApiResponse(
list,
user_id,
count,
data.next_max_id,
),
);
}
return data.users;
};
// helper methods to make the code a bit more readable
const getFollowers = (user_id, count = 50, next_max_id = "") => {
return concatFriendshipsApiResponse("followers", user_id, count, next_max_id);
};
const getFollowing = (user_id, count = 50, next_max_id = "") => {
return concatFriendshipsApiResponse("following", user_id, count, next_max_id);
};
const getUserId = async (username) => {
let user = username;
const lower = user.toLowerCase();
const url = `https://www.instagram.com/api/v1/web/search/topsearch/?context=blended&query=${lower}&include_reel=false`;
const data = await fetch(url, fetchOptions).then((r) => r.json());
const result = data.users?.find(
(result) => result.user.username.toLowerCase() === lower,
);
return result?.user?.pk || null;
};
const getUserFriendshipStats = async (username) => {
const user_id = await getUserId(username);
if (!user_id) {
throw new Error(`Could not find user with username ${username}`);
}
const followers = await getFollowers(user_id);
const following = await getFollowing(user_id);
const followersUsernames = followers.map((follower) =>
follower.username.toLowerCase(),
);
const followingUsernames = following.map((followed) =>
followed.username.toLowerCase(),
);
const followerSet = new Set(followersUsernames);
const followingSet = new Set(followingUsernames);
console.log(Array(28).fill("-").join(""));
console.log(
`Fetched`,
followerSet.size,
"followers and ",
followingSet.size,
" following.",
);
console.log(
`If this doesn't seem right then some of the output might be inaccurate`,
);
const PeopleIDontFollowBack = Array.from(followerSet).filter(
(follower) => !followingSet.has(follower),
);
const PeopleNotFollowingMeBack = Array.from(followingSet).filter(
(following) => !followerSet.has(following),
);
return {
PeopleIDontFollowBack,
PeopleNotFollowingMeBack,
};
};
// Make sure you don't delete the quotes
// Replace "example_username" below with your instagram username
//
// Change this:
username = "example_username";
//
//
//
getUserFriendshipStats(username).then(console.log);
@salthy26
Copy link

they suspended my insta account from doing this code just today so be careful

did u unfollow a lot of people at once? like 10 or more? and did u do it frequently? because with this, u do have to be careful with the amount u unfollow and how frequently u do it to avoid it.

no i didn't it would be less than 5 but i didn't do it frequently but i did do it three days ago i wanna say so but hopefully i get it back they still havent given me a response so idk what i can do just wanted to give a heads up

Copy link

ghost commented Apr 20, 2024

my account was suspended yesterday. I only followed one person.
It does not work anymore!

@deyshohan
Copy link

deyshohan commented Apr 23, 2024

eh i think the code is faulty at this point if its causing suspensions or false outputs. for me, it doesn't give the correct output; it misses 20+ ppl in the following output but the follower output is correct.

@shawndeezy22
Copy link

Image I keep getting this message now what should I do about it I also disable ad blockers as well so I don't understand the issue

@volt025
Copy link

volt025 commented May 12, 2024

did this stop working cuz ig changed their front end?

@cchoi928
Copy link

Hi, I don't think it's working again, the code stops loading followers halfway through and Instagram logs me out when I refresh to do it again, just letting you know

@fpookie
Copy link

fpookie commented May 27, 2024

can you make one of these for tiktok please!! :)

@spinkicks
Copy link

as of right now doesn't work

@camball
Copy link

camball commented May 31, 2024

Just worked for me—thank ya! Following/follower counts were 100% correct. Unfollowed ~30 people over the span of an hour or so after seeing the results of the script, so I don't suspect my account will get suspended as that's a fairly large time frame, but let's keep fingers crossed. This is the only solution I'd trust these days for getting these numbers, all the apps where you put in your credentials are a straight shot to getting suspended (happened to me in the past).

@miamiamiaaaaa
Copy link

not working... stays at loaded 20 followers ish

@miamiamiaaaaa
Copy link

plz help

@shawndeezy22
Copy link

it's still not working for me any updates anyone?

@pwixs
Copy link

pwixs commented Jun 12, 2024

it suspended my insta account ??

@shawndeezy22
Copy link

it suspended my insta account ??

did it fr, for how long?

@shawndeezy22
Copy link

@abir-taheer are u going to gibe us any updates on your code?

@pwixs
Copy link

pwixs commented Jun 24, 2024

it suspended my insta account ??

did it fr, for how long?

permenantly, but i appealed the suspension and got my account back really quickly

@arellale
Copy link

arellale commented Jul 5, 2024

I got a warning from Instagram saying my account is linked to automation or something. Be careful and use this sparingly or else they may suspend your account!

@cchoi928
Copy link

It keeps logging me out when I'm doing it and when I try to log back in after I click send verification code to phone the site just crashes

@shawndeezy22
Copy link

any update if its safe to use now

@Lazaro-Barrios
Copy link

Ive done it twice and everything has been fine for me

@cchoi928
Copy link

The code doesn't work again it says "could not find user with username "_" at getUs" and my user wasn't misspelled or at all

@Navyaverma1
Copy link

when i run the code it says that no user exists with my username

@Devtool-192
Copy link

It keeps saying couldn't find username whenever I try. It used to work but now does not

@neuroborus
Copy link

Everything is still working, thank you. If it doesn't work for someone, please ensure you have replaced the username with the correct one...

@Ramos0921
Copy link

Heads up if you are getting the username not found error, I believe it's because IG is no longer returning your user info in the query made on line 71.
What I did was logged out of IG and searched for myself on google by my IG handle. This should show your profile. Go into your profile with the inspect "Network" tab opened. Scroll down until you find a query that contains your user info. Copy your id and pk, seem to be the same. Then in the function getUserFriendshipStats set const user_id = your_pk_here and remove the if (!user_id) conditional.

This worked for me, hope it helps!

@xaesora
Copy link

xaesora commented Jan 21, 2025

hi, i'm getting this message:
Screenshot (355)
it might be because i used it about 5 minutes prior, and then proceeded to try to use it again because the follower/following count was not accurate, but not sure how to resolve this. thanks!

@Minifigures
Copy link

It used to work last week but now it's stuck at this only
image

@Minifigures
Copy link

It used to be working everyday for a year but ever since after last week it's stuck at this
image

@Yosh145
Copy link

Yosh145 commented Feb 20, 2025

why dont you parallelize requests?
const [followers, following] = await Promise.all([ getFollowers(user_id), getFollowing(user_id) ]);

instead of

const followers = await getFollowers(user_id); const following = await getFollowing(user_id);

@UnPurgable
Copy link

2/26/2025 works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment