A simple Node.js project to download Instagram media (images and videos) using Axios and Cheerio. This tool fetches metadata from Instagram posts/reels and downloads the media files locally.
Instagram-Downloader/
├─ index.js # Entry point: example usage of the downloader
├─ test.js # Main module handling Instagram media download
├─ LICENSE # BSD 3-Clause License
├─ README.md # Project readme and contact info
├─ package.json # Node.js project metadata & dependencies
- ✅ Download Instagram images and videos using the post URL
- ✅ Automatically fetch metadata: title, description, thumbnail, and media type
- ✅ Save files locally with proper file paths
- ✅ Simple and reusable functions for integration in other projects
- ✅ Supports Instagram reels and standard posts
- axios → For HTTP requests
- cheerio → For parsing HTML
- fs → For file system operations
- path → For handling file paths
Install dependencies with:
npm install axios cheerioconst instagram_download = require("./test");
(async () => {
const value = await instagram_download.downloadMedia(
"https://www.instagram.com/reel/DADNc8DimE8/?igsh=MXFwZzN2MmJwODV2cw==",
"./"
);
console.log(value);
})();Sample Output:
{
"file": "/Users/user/Downloads/DADNc8DimE8.mp4",
"type": "Video",
"thumbnail": "/Users/user/Downloads/DADNc8DimE8-thumb.jpg"
}-
Description: Appends
?__a=1to the Instagram URL to get JSON metadata. -
Parameters:
oriUrl(string): Original Instagram URL
-
Returns: New URL string
Example:
const newUrl = createNewUrl("https://www.instagram.com/p/abc123/");
console.log(newUrl); // https://www.instagram.com/p/abc123/?__a=1-
Description: Determines if the media is an image or video.
-
Parameters:
mediaData(object): Metadata object from Instagram
-
Returns:
"image"|"video"
-
Description: Fetches Instagram post metadata using Axios and Cheerio.
-
Parameters:
url(string): Instagram post URL
-
Returns: Object containing metadata
Metadata Fields:
| Field | Type | Description |
|---|---|---|
| description | string | Instagram post description |
| url | string | Direct media URL |
| thumbnail | string | Thumbnail image URL |
| title | string | Title of the post |
| is_video | boolean | True if media is video, false if image |
-
Description: Downloads media file from the given URL and saves it to disk.
-
Parameters:
url(string): Media file URLfilename(string): Name for the saved filesavePath(string): Directory to save the file
-
Returns: Promise resolving on successful download
-
Description: Main function to download Instagram media (image/video) with metadata.
-
Parameters:
url(string): Instagram post URLsavePath(string): Directory to save the media
-
Returns:
DownlodResultobject
DownlodResult Fields:
| Field | Type | Description |
|---|---|---|
| file | string | Absolute path to downloaded file |
| type | string | "Image" or "Video" |
| thumbnail | string | Path to thumbnail (only for video) |
Example:
const result = await downloadMedia("https://www.instagram.com/p/abc123/", "./media");
console.log(result);
/*
{
file: '/media/abc123.mp4',
type: 'Video',
thumbnail: '/media/abc123-thumb.jpg'
}
*/- Transform URL:
createNewUrl()adds?__a=1to get JSON from Instagram. - Fetch Metadata:
downloadMetaData()scrapes description, media URL, and thumbnail. - Determine Media Type:
getMediaType()checks if it's video or image. - Download Media:
download()streams the media content to local disk. - Return Paths:
downloadMedia()returns file paths and type for further use.
This project does not use a database. Media files are saved directly to the local filesystem.
File Example Structure on Disk:
./media/
├─ abc123.jpg # Image post
├─ xyz456.mp4 # Video post
├─ xyz456-thumb.jpg # Video thumbnail
This project is released under BSD 3-Clause License. See the LICENSE file for details.
- 🌐 Website: srza.ir
- 📱 Telegram: d_opa_mine | Sobhan_SRZA
- 📸 Instagram: mr.sinre
- 🎥 YouTube: @mr_sinre
- 🖥 Twitch: sobhan_srza
- 🐙 GitHub: Sobhan-SRZA
- 💬 Discord: Join PC Development | PC Club
- This downloader works on public Instagram posts only.
- Video posts download both the video and thumbnail automatically.
- All paths returned by
downloadMediaare absolute paths for easy integration.