How to post to Instagram using an API?


10 Jul, 2022

I like to automate things very much, so I was looking for a way to automate my Instagram account. I searched and looked for many ways and all of them seemed to be impossible. It is because Instagram doesn’t provide an API that can post images for you. After quite a few days of research, I came across a technique that would help us post to Instagram using an API – The Instagram-Private-API.

Instagram Private API

The Instagram Private API is kind of like a back door to Instagram where you can manipulate and control Instagram as to how you would do in the real mobile application. You can like, comment, share, publish stories, or even send a DM using this API. This API is not very famous because your account could be at risk if you use this API.

Here at, most of our social media interactions are automated. Thanks to Google’s Puppeteer, Heroku and Zapier. We have reduced the effort to publish to post our quotes and articles across various platforms. I’ve shared the code which can be used to login to Instagram and upload a photo for you through the API. There are also examples where you can post an image directly from a web URL.

The Code to post via API

You can use this code directly to post to Instagram from the API, just a couple of lines. Before using it make sure that you have the packages installed.

npm install instagram-private-api

Then you can use the following code to post your pictures. Make sure, the pictures are in proper resolution and aspect ratio. Instagram supports the aspect ratio of 4:5 and 1.91:1. If your images are not in this resolution, you might get an error.

const { IgApiClient } = require('instagram-private-api'); const { readFile } = require('fs'); const { promisify } = require('util'); const readFileAsync = promisify(readFile); const ig = new IgApiClient(); // Log in to your account const auth = await ig.account.login('username', 'password'); // Post the image const publishResult = await{ file: await readFileAsync('path-to-image.jpg'), caption: 'Hello World', });

If you have any questions or queries, please let me know in the comments section. Happy to help. You can try out the Free Online Image Quotes Generator developed by me to generate quotes online and download them instantly. For more coding related content, please visit my blog.

That's it for now, thanks for reading! You can find me at @samuellawrentz on X.

This helps me increase the session time of my site. Thank you!

Can you stay a bit longer?