DEV Community

Cover image for Create an Instagram downloader API with Nodejs

Create an Instagram downloader API with Nodejs

nodejs developer
・2 min read

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 = "";

async function getPostLink(url) {

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

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

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

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

    return {link,caption};


  return res;


async function getCaption(url) {

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

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

    let caption= getCaptionFromHtml(;

    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("video_url"),"video_url") + 1000);

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

  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.

Discussion (2)

hosseinmobarakian profile image
h_mobarakian Author

Thank you.