Auralinna.blog - Tero Auralinna's web development blogAuralinna.blogTero Auralinna's blog about intriguing world of web development. Tweaking pixels since the '90s.

How to fetch your public photos from Instagram without the API

Published: 10/24/2019

Update 4.12.2021: Unfortunately, this doesn't work anymore due to changes on the Instagram side.

This is a short demo about how you can fetch your Instagram photos without the usage of Instagram API, which requires a user to authenticate.

Embedded content: https://codepen.io/teroauralinna/pen/YzzVxwV

This technique is based on the <script> tag found from the Instagram profile page. Script tag has the data, which we will parse by regular expression.

const instagramRegExp = new RegExp(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)

The following function will fetch the profile page. It parses the Instagram data to JSON and returns an array of objects holding an image page URL, image source URL, and caption.

const fetchInstagramPhotos = async (accountUrl) => {
  const response = await axios.get(accountUrl)
  const json = JSON.parse(response.data.match(instagramRegExp)[1])
  const edges = json.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges.splice(0, 8)
  const photos = edges.map(({ node }) => {
    return {
      url: `https://www.instagram.com/p/${node.shortcode}/`,
      thumbnailUrl: node.thumbnail_src,
      displayUrl: node.display_url,
      caption: node.edge_media_to_caption.edges[0].node.text
    }
  })
  return photos
}

Calling the function:

try {
  const photos = await fetchInstagramPhotos('https://www.instagram.com/sunsetwithbubbles/')
  // Do something with the photos
} catch (e) {
  console.error('Fetching Instagram photos failed', e)
}

You must wrap this into an async function.

Note that this might break anytime if Instagram changes its source code, and this works only for the public profile. Also, it's possible to fetch only 12 images with this technique.

Be the first commenter?

Latest CodePens

I am an experienced web developer with an eye for solid UI/UX design. I have specialized in front-end development, responsive web design, design systems, modern web frameworks, and content management systems. I also have experience in mobile apps development and back-end coding with PHP, Node.js, and Java. So I have a full stackish background, but I'm enjoying most building robust and beautiful front-ends with performance, accessibility, and testability in mind.

© Tero Auralinna

Auralinna.fiSunset with Bubbles: Travel and Photography Blog

The icon "ellipsis" is provided by loading.io