← blog blog / instagram-api.md

How to post to Instagram using an API?

Did you know that you can post to instagram using an API? It can be done via the Instagram private API. You can automate your instagram with few lines of code.

How to post to Instagram using an API?

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 offshorewritings.com, 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 ig.publish.photo({
  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.

Thanks for reading!

I write about frontend craft, React, TypeScript, and the web. Found this useful? Let me know.

@samuellawrentz →

$ echo "enjoyed this post?" · subscribe via rss ↗

$ git log --oneline --grep="node"

More articles

cd ../blog →
  1. a8356c1 Building Reusable Bun Modules - Why Bun Makes It Stupidly Easy

    Feb 21, 2026 5 min read tag: buntag: typescript

    Building Reusable Bun Modules - Why Bun Makes It Stupidly Easy
  2. a2e5f33 Dynamic OG images for your Gatsby blog

    Jul 27, 2022 5 min read tag: gatsbytag: node

    Dynamic OG images for your Gatsby blog
  3. cea819a A Sandbox That Boots in 60ms - MicroVMs vs Containers for Untrusted Code

    Jul 06, 2026 3 min read tag: infrastructuretag: sandbox

    A Sandbox That Boots in 60ms - MicroVMs vs Containers for Untrusted Code
  4. c40222e Rendering Millions of Points in the Browser Without Killing the Tab

    Jul 06, 2026 3 min read tag: frontendtag: performance

    Rendering Millions of Points in the Browser Without Killing the Tab

$ giscus --load ./comments

00:00

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

Can you stay a bit longer?