Step-by-step guide on how to get insights from your Instagram Business using Python

Instagram. The multi-purpose social media, where you can post your favorite foods, snap your favorite pastimes, where you can see what your friends or families are up to, where you can get your daily news digest, where you can get your funny pet videos, where you can get your daily dose of memes, where you can research about some products, services or even “research” your crush if you want to. It’s basically the swiss army knife of social media.

Visit here for: Schedule Facebook Posts

In 2021, over 1 billion people use Instagram every month [1]. I open up my Instagram daily. so are my friends, so are my family, and I think, so are you.

With a huge amount of active users, added with the fact that some of them have high levels of disposable income [2], Instagram is, of course, still one of the hottest places for your business to sell or promote your products or services.

No doubt having an online presence on Instagram is important. Some may even argue that it is even more important than having an offline presence. This sentiment can only be strengthened by the pandemic condition out there in the world.

Now, you want to start a business. Or, maybe already have a business, then you want to take a chunk of the market from Instagram.

Visit here: Top Data Analytics Companies

How do you build a presence for your business on Instagram? First, you can do so by registering for your Instagram Business Account, and then, make content/post on Instagram periodically to build your community, grow your follower numbers, or create awareness.

Let’s say you’ve made a series of varied posts or contents, but then you’re curious about how well your content has been received by people. You wanted to see which demographic are interested in your products, which cities are reached by your posts, and so on. You can see the insights right from your Instagram profile. But honestly, the view itself is very limited.

Do you want to see more comprehensive insights data from your Instagram? Or maybe build your own data table or dashboard from the insights? Let’s dig deeper into Instagram Graph API with Python with me!

Instagram Business Account

Before going too deep into codes or Instagram Graph API, let’s take a step back first for people who are not familiar with Instagram Business Account.

What most people use on Instagram is Instagram Personal Account. By default, every time a new user made an account on Instagram, it’s a personal account. But if you’re going to use Instagram for business purposes, you should “switch” your personal account to a business account.

By converting to a business account, you can gain additional features to help your business grow, such as accessing insights to help you understand who is engaging with your content, reaching the unaware potential markets with ads, and adding more contact details to your profile.

You can read more on how to set up your business account by reading this Facebook Blueprint. It’s a short process, and you can do so within 10 minutes.

Facebook Developer Account

Once you’ve made an Instagram Business Account, let’s head over to Facebook for Developers Page to make a developer account for you.

After that, you need to make an app first to access insight data from Facebook. Your app type will determine which APIs are available to you.

Making Your First Facebook App

  1. Go to My Apps, then click Create App.
  2. Select Business for your app types as you will use Instagram Graph API later.
  3. Write your display name and write your contact email.
My Apps Page in Facebook for Developers | Image by Author
The final step before your app is created | Image by Author

Once your app is successfully created, you can go to Basic Settings to check your App ID and App SecretApp ID is a unique identifier for your app, so Facebook can identify who is making data request from the app. While App Secret will be used to decode encrypted messages from Facebook. These two are unique to your app and will be used later.

Basic Setting contains App ID & App Secret | Image by Author

Other Key Information

Before moving forward to explore Instagram Graph API, let’s step aside a bit to collect another key information that we will use to dig insight: Facebook Page ID.

You can easily get your Facebook Page ID by checking on your Facebook Business Page and looking at the bottom part of the ‘About’ tab.

Facebook Business Page — Page ID | Image by Author

Instagram Graph API

Now, let’s add Instagram Graph API to your App. You can easily select Instagram Graph API, then click Set Up. You can read more about Instagram Graph API here.

Selection of products available to your Facebook App | Image by Author

Graph API Explorer

Once you’ve added Instagram Graph API, you can go to the ‘Tools’ tab to use Graph API Explorer to define what kind of permissions that you need or want, also to generate an access token.

Graph API Explorer Main Page | Image by Author

There is a large list of permissions that we can select, and you can refer to them here. Obviously, when building an app or requesting data from Facebook, we only need a handful of data and not all of them.

There are permissions to access basic information only, access insights, publish comments, publish contents, etc. You can read in-depth in the link above.

As our goal is to harness the power of Instagram Insights, we’re going to ask for a specific list of permissions. What we’re going to do is to ask from Facebook/Instagram right now for these insights:

  1. Basic Insights from Every Instagram Posts (Likes & Comment)
  2. Deeper Insights from Every Instagram Posts (Impressions, Reach, etc.)
  3. Audience & Demographic Insights from your Instagram Account

If you’ve read the permission link above, you will know that to get those data, we have to ask for these permissions: instagram_basicinstagram_manage_insightspages_show_listpages_read_engagement, and pages_read_user_content.

Once you’ve selected all those permissions, click Generate Access Token, and a small window will pop up. Before confirming the subsequent processes, Facebook will also notify us of our Instagram Business Account ID. Copy this to your notepad as this will become handy later.

Instagram Business Account ID | Image by Author

Confirm the subsequent windows to link your app to Facebook, then you will finally obtain an access token. Please bear in mind that this token is only a short-lived access token, valid for one hour only. We’ll generate a long-lived access token that is valid for 60 days further down the line. Strap in, as we’re going to use Python now!

Getting Insights from Facebook Graph API


Open up your Jupyter NotebookGoogle Colab, or any other Python IDE, then we’ll start by importing the required libraries first.

Since we’re going to use GET Request to Facebook, we have to adhere to certain syntax depending on the data that we need to request. To make us easier using various GET syntax, let’s compile the details that we need in a dictionary called ‘params’.

You may be surprised at how many masked details are shown in the code, but actually, we’ve been collecting those details in previous processes! Let me explain:

  1. access_tokenshort-lived access token that we got directly before we start writing our code
  2. client_idApp ID from Settings
  3. client_secretApp Secret from Settings
  4. page_idFacebook Page ID from your business page’s ‘About’ tab
  5. instagram_account_id: shown during confirmation to generate token process

For ‘graph_version’, I’m using v12.0 because that’s the currently available version for me. If you use another version of the API, just update it accordingly.

Check Access Token

Before moving forward to request insights, let’s check our access token and app scope first. We can do so by writing this code.

Running those codes will result in this:

Access Token Check Result | Image by Author

We can confirm our app scope and also, more importantly, know what time exactly our token will expire. To convert that ‘expires_at’ number into proper timestamp format, let’s do a simple reformatting.

Now we know exactly how short-lived our token actually is 🙂

Short-Lived Access Token Expire Time | Image by Author

Change Short-Lived Access Token to Long-Lived Access Token

Since 1-hour expiry time may bother with our process of learning to request insights data from Instagram, let’s convert that short-lived token to a long-lived access token.

API Endpoint:{graph-api-version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={your-access-token}

Based on that API Endpoint from Facebook, let’s write some codes to get the long-lived access token.

Long-lived access token | Image by Author

Now that we get a long-lived access token, let’s check how long our newest token expiry time is by putting it back to our parameters dictionary (params[‘access_token’]), then re-run the Check Access Token code.

Long-lived access token expire time | Image by Author

We’ve successfully transformed our access token’s expiry time from October 2021 to December 2021! Now we’re able to perform data requests more flexibly since we have more time.

Instagram Posts — Basic Insights

Let’s start with the basic insights first, such as basic metadata from your Instagram Business posts, and their likes count and comment count.

API Endpoint:{graph-api-version}/{ig-user-id}/media?fields={fields}

By running those codes, we can get any post metadata and basic insights from any posts that started from the time you registered to business account. Bear in mind that if originally your account is a personal account, then you will only get insights from the posts that you posted after you changed to a business account.

Basic Insights Result | Image by Author

As you see, you will receive data in the form of a JSON dictionary. All those data are pretty important, so let’s just simply convert it to Pandas DataFrame.

Basic Insights — DataFrame Result | Image by Author

Instagram Posts — More Insights

We can delve deeper into each post insight by using a different API endpoint from above.

API Endpoint:{graph-api-version}/{ig-media-id}/insights?metric={metric}

This time, we’re going to loop over ‘media id’ that we got from previous codes, to request deeper insights metric.

More Insights — Result

Not all data above are important, we only want to get the ‘name’ and the ‘value’ so we have to parse it correctly, then convert it to Pandas DataFrame.

More Insights — DataFrame Result | | Image by Author

We’ve successfully compiled deeper insights for our posts. Not only engagement (likes comments), we can also see impressionsreach, and saved, which are increasingly important metrics in Instagram’s algorithm. We can even tidy up our data even further by combining this DataFrame of deeper insights with the DataFrame of basic insights.

Instagram Account — Audience Insight

We may have gotten insights from our individual posts, but we can also request insights from our Instagram Business Account, for example, Audience Insights.

API Endpoint:{graph-api-version}/{ig-media-id}/insights?metric={metric}

Audience Insights Result | Image by Author

We now have audience demographic insights with various levels: by countryby city, and by gender-age. With this data, we can see which cities are reached by our posts, which city likes our content the most, which demographic our contents appeal to.

We can make Pandas Dataframe with simple coding, for example, let’s create DataFrame of Audience by City and Audience by Gender-Age.

Audience by City — DataFrame Result | Image by Author
Audience by Gender-Age — DataFrame Result | Image by Author

Utilizing the above information, we can make better content creation or marketing strategies. The more we know about our users, the better we can target them with tailored campaigns or ads.


Now that you can extract several insights data from your Instagram Business Account, such as likes count, comment count, impressions, saved, and audience insights, you can analyze your Instagram account’s performance better.

This is just the beginning as you can make your dashboard or data visualization for data above, or you can delve even deeper by looking for more insights.

Original Source


A Data Science Padawan. Zen of Python Believer. Building Data Product & Team in DataSintesa. Get in touch in