DEV Community

Cover image for Instagram Automation Bot for JavaScript: How to use InstAuto?
Michiel Mulders
Michiel Mulders

Posted on

Instagram Automation Bot for JavaScript: How to use InstAuto?

For many Instagram users, using an Instagram automation bot has proven to grow their account and audience.

An Instagram bot provides a simple way to find new accounts to follow, which are likely interested in your post's content.

However, we must use an Instagram bot correctly. There's no point in targeting people who like content about cars while posting content about flowers.

On top of that, Instagram implements stringent guidelines for bot automation. Although we don't know the exact limitations, a study by SocialPros.co showed the following average interaction statistics for users in 2020:

  • 13 follows per hour, or 100-150 follows per day
  • 300 to 400 likes per day (of followed accounts)
  • 2 to 5 comments per hour, or 20-30 comments per day

In this article, we'll learn how to set up the JavaScript Instagram automation bot InstAuto and how to automate within Instagram's terms of service without receiving a shadowban.

Why use an Instagram bot?

Instagram bots help us with repetitive tasks, such as liking, commenting, and (un)following. These are the core actions of the Instagram platform and consume a lot of time when done manually.

Therefore, this presents us with the opportunity to use an Instagram automation bot to automate the above tasks.

On top of that, an Instagram bot offers us much more flexibility.

Let's say we follow five new accounts, and we want to unfollow them after three days. It's quite a daunting task to manually verify if this person has followed you and then unfollow him.

For this reason, we use an Instagram bot as it allows for much more complex scenarios. Therefore, an Instagram automation bot often keeps track of likes, follows, and comments using a database. A database allows the bot to quickly lookup accounts that we followed three days ago, which we want to unfollow.

Let's take a look at how we can use InstAuto to configure our first Instagram automation bot.

Requirements

First, let's discuss the requirements for this tutorial.

  • Node.js version 8 or newer
  • New project setup using npm init with an index.js file in the root
  • Install required dependencies: npm install --save dotenv puppeteer instauto

Done? Let's continue!

Step 1: Creating Your First Instagram Automation Bot

It's time to modify the index.js file. We first want to load the required dependencies. We'll use dotenv to hide secrets, such as our Instagram username and password, where we use puppeteer for controlling a Chrome or Chromium browser instance to automate all aspects of our Instagram tasks.

Add the following code.

require('dotenv').config()
const puppeteer = require('puppeteer')
const Instauto = require('instauto')
Enter fullscreen mode Exit fullscreen mode

Next, let's prepare the options object to configure our Instagram bot. Here, we can configure the limits for our Instagram bot. To be safe, we only allow 20 new follows per hour and limit the total number of followers per day to 150. Also, we set a maximum number of likes per day equal to 50.

Note that you toggle a dry run on or off. For testing purposes, enable the dryRun property.

const options = {
  cookiesPath: './cookies.json',

  username: process.env.INSTA_USERNAME,
  password: process.env.INSTA_PASSWORD,

  // Global limit that prevents follow or unfollows (total) to exceed this number over a sliding window of one hour:
  maxFollowsPerHour: 20,
  // Global limit that prevents follow or unfollows (total) to exceed this number over a sliding window of one day:
  maxFollowsPerDay: 150,
  // (NOTE setting the above parameters too high will cause temp ban/throttle)

  maxLikesPerDay: 50,


  dontUnfollowUntilTimeElapsed: 3 * 24 * 60 * 60 * 1000,

  // Usernames that we should not touch, e.g. your friends and actual followings
  excludeUsers: [],

  // If true, will not do any actions (defaults to true)
  dryRun: true,
};
Enter fullscreen mode Exit fullscreen mode

Next, we need to add our Instagram username and password. As you can see, we import those secrets using process.env (dotenv via npm).

Therefore, let's create a .env file in the root of your project. Make sure to add both INSTA_USERNAME and INSTA_PASSWORD variables.

Here's an example of the .env file. Make sure to replace the values!

INSTA_USERNAME=myusername
INSTA_PASSWORD=mypassword
Enter fullscreen mode Exit fullscreen mode

All good? Let's add the bot execution code. This snippet follows the code for the options object.

Here, we launch an instance of Puppeteer to enable automatization. Next, we also create a database that keeps track of followed and unfollowed users, and liked photos.

Then, we pass the Puppeteer browser instance, database, and options object to the InstAuto dependency to create our bot.

It's possible to pass one or multiple users for which we want to follow their followers. At the moment, we've added only one Instagram username dedik_armawann.

This is the current setup for the bot:

  1. Follow all users (within the predefined limits) for user dedik_armawann
  2. Skip all private accounts
  3. Like 1-2 user images
  4. When the bot has completed following all accounts, it will sleep for 10 minutes and start the unfollow process for accounts that haven't followed us back. Remember that we've set the dontUnfollowUntilTimeElapsed to three days.
  5. Unfollow all users that didn't follow us back after 60 days (clean up phase)
(async () => {
  let browser;

  try {
    browser = await puppeteer.launch({ headless: false });

    // Create a database where state will be loaded/saved to
    const instautoDb = await Instauto.JSONDB({
      // Will store a list of all users that have been followed before, to prevent future re-following.
      followedDbPath: './followed.json',
      // Will store all unfollowed users here
      unfollowedDbPath: './unfollowed.json',
      // Will store all likes here
      likedPhotosDbPath: './liked-photos.json',
    });

    const instauto = await Instauto(instautoDb, browser, options);

    // List of usernames that we should follow the followers of, can be celebrities etc.
    const usersToFollowFollowersOf = ['dedik_armawann'];

    // Now go through each of these and follow a certain amount of their followers
    await instauto.followUsersFollowers({ usersToFollowFollowersOf, skipPrivate: true, enableLikeImages: true });

    await instauto.sleep(10 * 60 * 1000);

    // This is used to unfollow people - auto-followed AND manually followed -
    // who are not following us back, after some time has passed
    // (config parameter dontUnfollowUntilTimeElapsed)
    await instauto.unfollowNonMutualFollowers();

    await instauto.sleep(10 * 60 * 1000);

    // Unfollow auto-followed users (regardless of whether they are following us)
    // after a certain amount of days
    await instauto.unfollowOldFollowed({ ageInDays: 60 });

    console.log('Done running');

    await instauto.sleep(30000);
  } catch (err) {
    console.error(err);
  } finally {
    console.log('Closing browser');
    if (browser) await browser.close();
  }
})();
Enter fullscreen mode Exit fullscreen mode

Good, let's try our bot!

Step 3: Starting a Bot Dry Run

Now, let's start an Instagram bot dry run. We can execute the index.js file from our terminal.

node index.js
Enter fullscreen mode Exit fullscreen mode

If we've passed the correct Instagram username and password, the bot will open a new Chromium browser and log in to your Instagram account.

Have some patients while the bot is running. The bot will eventually look up the usernames we've passed to follow and start the follow and liking process.

We can always track the progress in the terminal as the bot prints everything it does. On top of that, you can also open the following files to track the progress:

  • followed.json
  • unfollowed.json
  • liked-photos.json

Here's an example of how the bot works:

Does your bot work? Yes! Next, let's customize our bot.

Step 4: Instagram Bot Customization

In this step, we want to discuss different parameters to tailor your bot. Let's discuss four essential parameters.

  1. followUserRatioMin: Don't follow users that have a follower to following ratio of less than the defined value.

  2. followUserRatioMax: Don't follow users that have a follower to following ratio higher than the defined value. Often, spam accounts have a very high ratio. To avoid those accounts, you can set this property.

  3. followUserMaxFollowers: Don't follow users who have more followers than the defined value. This property is useful to avoid popular accounts as they often don't follow back.

  4. followUserMaxFollowing: Similar property to not follow users who have more people following them than the defined value.

Of course, you can always exclude certain users from your bot using the excludeUsers array property.

Conclusion

In this article, we've learned how to set up an Instagram automation bot, operate a bot within the Instagram terms of service, and customize the InstAuto bot.

Tip: Don't forget to set the dryRun property to false when using the bot in production.

You can find more information about the bot on GitHub and more information about running the bot on a server using a tool such as pm2.

Top comments (3)

Collapse
 
sadaca73 profile image
Shawn

Hello Michiel,

First off, thanks for posting this. I’m a coding noob and was excited to have actually gotten this automation to work!

One thing I’m struggling with is the portion with the .env file. My file is named pass.env and I saved it in the root file as instructed. I copied and pasted the code supplied into it and filled on username and password.
I may have missed something, as I see you said to remember to name the variables (which I think the code you supplied actually did do).
The dry run got me to the login screen and didn’t successfully login. I’ve been running it with the username and password in the source code, but would love to complete the project as instructed.

Would you be able to give a noob like me some literal direction?

I did try to study more about the .env/process.env to gain some understanding, but it’s still gibberish to me at this point.

Really fun challenge for me...thanks again!

Shawn

Collapse
 
michielmulders profile image
Michiel Mulders

Hi Shawn, try saving the file as .env instead of pass.env. That should solve the problem! :)

Collapse
 
mmdzov profile image
MohammadAli

Hello Michelle

In some websites, I saw that the order of likes, followers and visits is registered and the order is done immediately, that is, an amount is paid and the order is done. There are both fake and real. What scenario do these websites use?