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

I am an experienced web developer with an eye for a solid UI/UX design. I have specialized in front-end development, responsive web design, 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 am a Full Stackish web developer with a strong passion for a beautiful front-end.

How to fetch your public photos from Instagram without the API

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)
}

Note that you need to wrap this into an async function.

Disclaimer: Obviously, this might break anytime if Instagram changes its source code and works only for the public profile.