DEV Community

Cover image for Create an Instagram downloader API with Nodejs
Hossein Mobarakian
Hossein Mobarakian

Posted on

13 1

Create an Instagram downloader API with Nodejs

Hi, I'm Hossein, In this article, I'll show you how to create your Nodejs Instagram photo and image downloader.
To start our project we need to install dependencies:
npm init
npm install node-html-parser axios

After installation dependencies have done, Create an index.js file and put the codes below into it:

const axios = require('axios');
const { parse } = require("node-html-parser");

const instagramPostLink = "https://www.instagram.com/p/CD5um-vHA33/";

async function getPostLink(url) {

  url = url + 'embed' + '/captioned';


  let res = axios.get(url).then(async (response) => {
    const root = parse(response.data);

    let link = "";
    if (response.data.search("video_url") != -1)
      link = getVideoLinkFromHtml(response.data);
    else
      link = root.querySelector('img.EmbeddedMediaImage').getAttribute("src");





    while (link.search("&") != -1) {
      link = link.replace("&", "&");
    }
    let caption =await getCaptionFromHtml(response.data);

    return {link,caption};

  });

  return res;

}

async function getCaption(url) {


  url = url + 'embed' + '/captioned';

  let res = axios.get(url).then((response) => {

    let caption= getCaptionFromHtml(response.data);

    return caption;

  });

  return res;

}

async function getCaptionFromHtml(html) {


  const root = parse(html);

  let caption=root.querySelector('.Caption')?.text;
  if(caption == undefined)
      caption="No caption";

  caption=caption.replace("view all comments","");
  return caption;

}

function getVideoLinkFromHtml(html) {
  let crop = "{\"" + html.substring(html.search("video_url"), html.search("video_url") + 1000);

  crop = crop.substring(0, crop.search(",")) + "}";

  return JSON.parse(crop).video_url;
}

(async ()=>{
    await getPostLink(instagramPostLink);
})();

Enter fullscreen mode Exit fullscreen mode

Some trouble with download the Instagram app is it needs a login, So we are use the embed Instagram posts to download that.
And Instagram can't block this way, Because so many sites use that.

Do you like to create animated emojis for your website? Click this link.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (18)

Collapse
 
zyennuu profile image
zyennuu

I follow this method and made this amazing Instagram reels, Stories, Photo, profile, downloader all in one with next.js. Check this out StoriesSaver.org

Collapse
 
sahil99 profile image
Sahil

can i get source code?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
iabhishekrajpoot profile image
iabhishekrajpoot • Edited

we cant able too get video urls for some links like that one: instagram.com/reel/CTxDDvOJx72/?ut...

only thumbnail image was returned.
And some of the websites like instavideosave.net can able do to so.

Collapse
 
sujayroy1576812 profile image
jhon cena

useful.

Collapse
 
downloader2x profile image
MR adama • Edited

Instagram used to be a straight forward app. Post a photo, and carry on. Now it's a feed of stories, nearly redundant to Reels, with ads every second Story you scroll through. Instagram stories downloader

Collapse
 
rinku_singh_6de5c7b85593c profile image
RINKU SINGH

we cant able too get video urls for some links like that one: instagram.com/reel/CTxDDvOJx72/?ut...

only thumbnail image was returned.
And some of the websites like fastreelshub.com/ able do to so.

Collapse
 
anony9653 profile image
coder9653 • Edited

this API only provides to download photos only , you can't download reels and videos using this API but some website allows to download the reels and videos. and i was looking to increase reels views as well.
Any tips?

Collapse
 
zonemvn profile image
zonemvn • Edited

Hey guys, do you struggle while downloading any content from Instagram. Here is the solution try Instagram Reels video Download Tool which can you to download Instagram Reels within a second

Collapse
 
saverreel profile image
Reel Saver

This API only provides photos; video and reels cannot be retrieved. There are various websites that can collect all of the reels/videos/images/Stories instagram Mp3 Downloader

Collapse
 
hackerrsites profile image
hackerrsites • Edited

This api Only returns images and Video & reels can't be fetched.
There are some website that are able to get all the Reels/Videos/Images together

Check Link : reelsdownloader.io

Collapse
 
hosseinmobarakian profile image
Hossein Mobarakian

Do this way for each , don't copy and paist

Collapse
 
melikashojaee profile image
melika_shojaee

thanks for this code
but this code just return the link of first page for album posts.so for get all of them what shoud we do?

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay