Search API Connector Documentation

Print

Import Instagram Data to Google Sheets

premium

In this guide, we’ll walk through how to pull Instagram Insights data data directly into Google Sheets, using the API Connector add-on for Sheets.

To use the Instagram Insights API, you must have an Instagram Business account that is connected to a Facebook Page, and you must be an admin of that Facebook page. If you need help, this guide provides information on converting your personal Instagram account to a business account.

Contents

Before You Begin

Click here to install the API Connector add-on from the Google Marketplace.

Part 1: Connect to the Instagram Insights API

If you haven’t connected to the Instagram Insights API before, you’ll first need to initiate the connection.

  1. Open up Google Sheets and click Extensions > API Connector > Manage Connections.
  2. In the list of available connections, find Instagram Insights and click Connect.
    instagram-img2
  3. You will be directed to Facebook and asked to choose which Instagram accounts and Facebook Pages you’d like to use with API Connector. If you are an admin of multiple accounts, make sure to choose the Instagram account and Facebook page that you’ve previously linked together. Click Done.
    instagram-img3
  4. You’ll then be returned to your Google Sheet, and can verify that your Instagram Insights connection is active in the Connections screen.

Part 2: Create Your Instagram API Request URL

For our first request, we’ll get some basic information about your Instagram account.

  • API root: https://graph.facebook.com
  • Endpoint: /me/accounts

Putting it all together, we get the full API Request URL.

https://graph.facebook.com/me/accounts

Part 3: Pull Instagram API Data into Sheets

We can now import our first set of data.

  1. Back in the Create Request interface, enter the Request URL we just created above.
    instagram-img1
  2. Under OAuth, choose Instagram Insights from the dropdown menu. You should see a badge saying “Connected” if you’ve already connected as described in Part 1.
    instagram-connected
  3. You don’t need any headers so just leave that section blank.
  4. Create a new tab and click Set current to use that tab as your data destination.
  5. Name your request and click Run. A moment later you’ll see some information about your Instagram account populate your sheet. Pay special attention to the value located in the data.id field as you’ll need it in the next request.
    instagram-data-id

  6. Now let’s use that ID to get your Instagram Business account ID, because we’ll need that for all requests to the /insights endpoint, which is what we’re here for. In a new request, create an API URL like this, substituting in your own data ID.
    https://graph.facebook.com/YOUR_DATA_ID?fields=instagram_business_account
  7. Just as you did before, choose Instagram Insights from the Authentication menu (you’ll need to do this for all requests to the Instagram API) and click Run.
  8. You should now see your Instagram Business account ID printed into the sheet. Keep this handy, we’ll use this ID to get all our Instagram stats!
    instagram-business-id

Part 4: Example API URLs

The Instagram API provides a lot of different endpoints and functionality. I’ve tried to give a lot of examples and summarize the most useful endpoints below, but for more complete information, please check the full Instagram Insights documentation here.

In addition, you can use API Connector’s API Library to easily pull up most of these sample URLs (just search for “Instagram” while in the extension).

Account/User Metrics (metrics about the account)

  • Key metrics about an account (id, total followers, following, etc)
    https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID?fields=id,ig_id,followers_count,follows_count,media_count,name,profile_picture_url,username
    instagram-img10
  • Engagement metrics by day for a date range (the “since” and “until” parameters contain start and end dates as UNIX timestamps, read more about that in the section on handling dates. )
    https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=reach,impressions,profile_views,website_clicks,phone_call_clicks,text_message_clicks,get_directions_clicks,email_contacts&period=day&since=1645574400&until=1646438400
    By default, the request above will put each metric into separate rows. To convert them into column headers, switch to the grid report style and use the JMESPath technique described here, like this: { reach:data[?name=='reach']|[0], impressions:data[?name=='impressions']|[0], profile_views:data[?name=='profile_views']|[0], website_clicks:data[?name==' website_clicks']|[0], phone_call_clicks:data[?name=='phone_call_clicks']|[0], text_message_clicks:data[?name=='text_message_clicks']|[0], get_directions_clicks:data[?name=='get_directions_clicks']|[0], email_contacts:data[?name=='email_contacts']|[0] }

  • Count of new followers (query structure is just like that of engagement metrics, but follower count metrics can only be queried for the last 30 days)
    https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=follower_count&period=day
  • A breakdown of your followers by age and gender
    https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=audience_gender_age&period=lifetime

Media Metrics (metrics for content)

  • List posts with comments, likes, and other data. If you need more than 100 records, please check the section on pagination.
https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID/media?fields=caption,comments_count,id,ig_id,is_comment_enabled,like_count,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username,video_title&limit=100
Just leave off the fields you don’t need, and click Edit Fields to set your field order.
  • List posts including engagement metrics. Nearly the same as above, but listed separately as adding the insights.metric expansion will limit the response to data from only after the Instagram account was converted to a Business account.
https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID/media?fields=caption,comments_count,id,ig_id,is_comment_enabled,like_count,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username,video_title,insights.metric(reach,impressions,engagement)&limit=100
  • List posts that tagged you.
https://graph.facebook.com/v13.0/YOUR_BUSINESS_ACCOUNT_ID/tags?fields=id,username

Part 5: Handle Dates

The Instagram Insights API requires dates to be entered as UNIX timestamps. In Sheets, you can convert a regular date in cell A1 to a UNIX timestamp with the formula =(A1-DATE(1970,1,1))*86400.

Here is one way to make your requests run off a dynamically updating date range:

  • Create a tab called Inputs
  • In cells A1 and A2, add dynamic date formulas, e.g. =today()-7 and =today()-1
  • In cells B1 and B2, convert those dates to UNIX with =(A1-DATE(1970,1,1))*86400 and =(A2-DATE(1970,1,1))*86400
  • Now back in the API Connector sidebar, reference those timestamp cells in your request URL like this: https://graph.facebook.com/v12.0/111111111111/insights?metric=reach,impressions,profile_views&period=day&since=+++Inputs!B1+++&until=+++Inputs!B2+++ 

Now every time the date changes, your URL will update automatically.

Part 6: Handle Pagination

  1. By default Facebook’s graph API, which includes Instagram, limits the number of results in a single response (usually to 25 records) as described here. To get more records, you can add the ‘limit’ parameter (e.g. &limit=100) to the end of your URL.
  2. If you still have more records after that, the URL for the next page of results is printed into the paging.next field.
    instagram-img12
  3. In API Connector you can loop through these pages automatically using pagination handling, like this:
    • API URL: enter your request URL, including limit=100
    • Pagination type: next page URL
    • Field name: paging.next
    • Run until: choose when to stop fetching data
      instagram-pagination-nextpageurl

Part 7: API Documentation & Resources

There are two very useful endpoints for Instagram analytics: /{ig-media-id} (metrics on a media object) and /{ig-user-id} (metrics on an Instagram Business or Creator Account). Each endpoint has different available metrics, as described here:

Available Media Metrics:

Available User/Account Metrics:

Unofficial APIs

The article above discusses Instagram’s official API, which has quite a few limits and isn’t the easiest to work with. Therefore you may also want to check out some of the unofficial APIs listed here: https://rapidapi.com/search/instagram. They have their own limits, and in the long run may be less reliable than using the official API, but they simplify the connection process and provide some data points that Instagram doesn’t provide itself.

Previous Import Hunter Data to Google Sheets
Next Import Intercom Data to Google Sheets

60 thoughts on “Import Instagram Data to Google Sheets”

  1. Hey Ana,

    how can i see a day by day breakdown of the last 90 -120 days? i tried to use the “data_preset=last_90d” from FB ads account request, but it didn’t work.

    thank you so much for this post/content.
    – Leo.

    Reply
    • Hi Leo, thank you for the comment. To get data broken down by day you need to use the period=day query parameter. Then, to specify a time range, you need to use the since and until parameters, as a UNIX timestamp as described above. Unlike Facebook’s Insights API, the Instagram Insights API doesn’t support the date_preset parameter, so you need to explicitly specify the date.

      One more wrinkle: in my tests, it seems like since/until can only fetch 30 days at a time, though the documentation doesn’t state that clearly. So, in conclusion, you need to make a few different fetches to retrieve your data. If you plug in your dates and the UNIX conversion formula into a sheet called Inputs, then the following should work (just change your date inputs each time and append data into the same sheet):
      https://graph.facebook.com/v8.0/11111111111111111111/insights?metric=impressions,reach,profile_views,follower_count&period=day&since=+++Inputs!C2+++&until=+++Inputs!C3+++

      Reply
      • Hi Ana,

        Love your post. I have the same use case as Leo’s and I kept getting this error message, even though the date range is under 30 days:

        {“error”:{“message”:”(#100) (follower_count) metric only supports querying data for the last 30 days excluding the current day”,”type”:”OAuthException”,”code”:100,”fbtrace_id”:”A_xShfApDTNImI9MgflzEa4″}}

        Thanks so much!
        – Firas

      • Unfortunately Instagram’s follower_count metric is only available for the last 30 days specifically, it’s not just a 30 day range. So back-dating the query doesn’t work since they don’t provide any historical data past the last 30 days. Sorry for the inconvenience but I hope that clarifies.

  2. It works, but only for a lifetime period.

    I tried to add ‘period’ or ‘since/until’ to the queries, but it shows incompatible error. It looks like there’s no way to get monthly or daily engagement because of Instagram API limitations.

    But thank you anyway for answers!

    Reply
    • Bummer, I just investigated this further and confirmed your findings. The Instagram API doesn’t provide any way to slice up the media insights by time, it’s all lifetime totals only. The /{ig-user-id}/insights endpoint does provide time slicing capabilities, but you can’t get the engagement metric there.
      Sorry for the inconvenience, I’ll update the post with some of these findings.

      Reply
  3. Hello!
    Thank you for this great resource. I’m having quite a bit of trouble with something quite simple, however. I simply want to use the API to find the total number of followers an account (in this case my own) has.

    I feel as though I am missing something very obvious, help would be very appreciated.

    Thank you in advance.

    Reply
    • Hey there, you’re not missing something obvious, they’ve just hidden it deeply in the documentation :p. You can use this request URL to get total followers:
      https://graph.facebook.com/v9.0/YOUR_BUSINESS_ACCOUNT_ID?fields=followers_count
      I’ll update the article to make that more clear.

      Reply
  4. Hi!
    I’m really new to this, so I don’t really understand: is there a way to create automated scheduled reports? I’m trying to create a graph with monthly data, but I don’t know if I should simply do it by the end of the month or I should collect data daily.

    Thanks,
    Trinidad

    Reply
    • Hi Trinidad, sure, you can check this article on scheduling to see how you’d set up your request to refresh every month. So you could run a request like https://graph.facebook.com/v10.0/1111111111111111?fields=id,ig_id,followers_count,follows_count,media_count,name,profile_picture_url,username and then save all the values at the end of each month and re-run the query for new values. You can save the data manually or automate that monthly saving process with my other (totally free) addon Archive Data.

      Also, if you’re asking about getting historical data, that’s harder as the API only provides historical data for certain metrics. So feel free to provide more detail on what exactly you’re looking for.

      Reply
  5. Hi

    I’m having trouble getting all of the post ID of an Instagram account.

    ・I used the below API URL Path, and managed to get 25 id’s but how can I get more then 25 id’s? Is their a number limitation?

    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/media

    The output sheet has 4 columns as below.

    data.id, paging.cursors.before, paging.cursors.after, paging.next

    In the “paging.next” column there is a URL and the below message is inside it.

    {
    "error": {
    "message": "A user access token is required to request this resource.",
    "type": "OAuthException",
    "code": 102,
    "fbtrace_id": "ANq-J-qhCojmu43CABXZ02e"
    }
    }

    Reply
    • I added a section on pagination handling to the post, please check that and let me know if that successfully pulls in more records.

      Reply
      • Hi Thanks for the reply

        I made the change and the number increased from 25 to 39..
        but there are over 700 posts on the actual account.

        Let me know what i need to do in order to get at least 200…

      • I checked your sheet and see 700+ posts as expected, can you please let me know if you’re still having issues?

  6. Hello, I am testing your connector and it looks incredible.
    Could you please help me with one thing.
    Requests of this kind https://graph.facebook.com/v11.0/111111111111111111?fields=id,media_type,media_url,owner,timestamp are ok until I add media_product_type.

    With media_product_type I receive an error:
    Server responded with an error (400) show response{“error”:{“message”:”(#12) introduce IGTV to instagram graph api requires version v10.0 or higher”,”type”:”OAuthException”,”code”:12,”fbtrace_id”:”AZWd7Ax4ws56yEzztnzfdWr”}}

    How can I update instagram graph api to latest version?

    Reply
    • I see, it looks like that metric is a new metric available only in the newest API version. However, I’m not sure why you’re seeing this error, it worked fine for me when I tried https://graph.facebook.com/v11.0/1111111111111111?fields=id,media_type,media_url,owner,timestamp,media_product_type. I’ll look into this some more to see if I can replicate it.

      Reply
    • In theory this is possible as Instagram provides a messaging API. However, the method in this article won’t let you connect to it since messaging and insights require different permissions (Facebook / Instagram approve integrations only for the specific permissions requested).

      Reply
  7. Thank you for this amazing article! I’ve been referencing it a ton lately. I’m having a blast using your data methods to make something really pretty in Data Studio!

    I’m having an issue with pulling more than 25 rows of Post IDs, but it could also just be me not understanding things fully.

    Is this what my API URL Path is supposed to look like? https://graph.facebook.com/(FB User ID)/media&limit=150

    When I delete the “&limit=XXX“, it runs just fine. However if I run it otherwise, I get this error:
    Status
    Completed with errors
    – We received an error from the API server (400) show response{“error”:{“message”:”Unknown path components: \/media&limit=100″,”type”:”OAuthException”,”code”:2500,”fbtrace_id”:”AncPkjCs_XddrZIf0RH8iDK”}}

    I tried it with numbers ranging from 50-250. I’m not sure what to do. Do you know what I’m missing?

    Reply
    • Hey Stacie, thank you for the message. That’s awesome you’ve made a Data Studio report – if you wouldn’t mind sharing I’d love to see a copy.
      For your question, the first URL parameter is always separated with a ? while later parameters are separated with a &, so I think you just need to replace the & with an ? like this:
      https://graph.facebook.com/(FB User ID)/media?limit=100

      Reply
  8. Hello, I would love some advice or guidance!

    I’m currently making API requests for most of the data mentioned in this tutorial. I have also setup the scheduling of API requests daily, weekly and monthly depending on the data.

    However, I’m struggling to work out a solution to remove duplicates or only make api requests for data not currently in the spread sheet. for example, if I’m making an API request for a list of media posted to the Instagram account I obviously keep having duplicates being added.

    Any guidance or ideas would be greatly appreciated. I’m rather new to google sheets and making api requests. However am a quick learner!

    Thanks in advance!
    Blake

    Reply
    • Hey Blake, can you try a URL like this? https://graph.facebook.com/v11.0/YOUR_BUSINESS_ACCOUNT_ID/media?fields=caption,comments_count,id,ig_id,is_comment_enabled,like_count,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username
      Then you’ll have a date associated with each of the posts in your account. This particular API doesn’t provide any way to natively filter data but you can use API Connector’s JMESPath functionality to add in a filter like this:
      data[?timestamp>'2021-02-16T16:59:55+0000'].{caption:caption,comments_count:comments_count,id:id,ig_id:ig_id,is_comment_enabled:is_comment_enabled,like_count:like_count,media_product_type:media_product_type,media_type:media_type,media_url:media_url,owner:owner.id,permalink:permalink,shortcode:shortcode,thumbnail_url:thumbnail_url,timestamp:timestamp,username:username}

      Where it says 2021-02-16T16:59:55+0000, you can also reference a cell instead of hard coding it in, so it dynamically reflects the latest value in your sheet. Then it will only pull in data past that date.

      Reply
      • Thank you kindly for this guidance!

        Would it be possible to get an example of how the JMESpath filter example would look if it’s checking the data.id column in cell C2. From this, I’m confident ill be able to work out the rest.

      • Do you mean you’d like to filter on ID instead of the timestamp? In that case you’d just substitute in “id” for “timestamp” like this:
        data[?id>'17928481555452202'].{caption:caption,comments_count:comments_count,id:id,ig_id:ig_id,is_comment_enabled:is_comment_enabled,like_count:like_count,media_product_type:media_product_type,media_type:media_type,media_url:media_url,owner:owner.id,permalink:permalink,shortcode:shortcode,thumbnail_url:thumbnail_url,timestamp:timestamp,username:username}

      • Hi Ana,

        May I have your email? I’m working on a comprehensive social media dashboard and would love to offer the template along with a detailed setup guide to your website to help get more people using these tools!

      • Hi Ana,

        Could you please provide an example of how this would work “Where it says 2021-02-16T16:59:55+0000, you can also reference a cell instead of hard coding it in, so it dynamically reflects the latest value in your sheet. Then it will only pull in data past that date.” for example referencing the the last row via date or data.id?

      • Sure, first create a dynamic cell in your sheet, setting the format to yyyy-mm-dd. For example like this:
        =text(today()-1,"yyyy-mm-dd")

        Now your filter would reference that cell, like data[?timestamp>'+++Sheet1!A1+++T00:00:00+0000']

  9. Hi again! Yes, so I double-checked to make sure I was using my Instagram Business ID, and the same results came up. Do you have any idea why this may be happening?

    Reply
    • I can only guess that you’ve inadvertently entered a line break or extra character, since as far as I know the only possible cause of this error is an incorrect ID. You can try copying/pasting it in again (or reference the cell). If you’d like me to take a look feel free to send over a screenshot of your business ID and request setup and I can help spot the difference 🙂

      Reply
  10. this seems like a great tool, and I keep running into the following error trying to run the Instagram Insight extension:
    1) post metrics: Completed with errors
    – We received an error from the API server (400) show response
    {“error”:{“message”:”Unsupported get request. Object with ID ‘{102160809832566}’ does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https:\/\/developers.facebook.com\/docs\/graph-api”,”type”:”GraphMethodException”,”code”:100,”error_subcode”:33,”fbtrace_id”:”A6N5D3I1UmsjYXdrlxrbL8i”}}

    Any ideas of what to do?

    Reply
    • Can you please make sure you’re using your Instagram Business ID (not your regular Instagram ID)? I can only get this error when I enter my Data ID or some other ID that’s not the Business ID. As a reminder, you get your Business ID with this query: https://graph.facebook.com/YOUR_DATA_ID?fields=instagram_business_account (full instructions here) Or feel free to reach out through support if you’d like me to take a look at your request setup.

      Reply
  11. sorry if I missed a lesson up there, but is there a way to get total interactions (engagements) of an account within a day? and number of reach of a post within a day?

    I surely appreciate your great lesson up there, and more if you kindly help me with above issue

    Reply
    • Hey Rono, if you want to get engagement from a specific day, you can use a URL like this: https://graph.facebook.com/v12.0/17841403836625216/insights?metric=impressions, reach, website_clicks, profile_views&period=day&since=1639785600&until=1639872000
      If you leave off the since/until parameters, it will give you yesterday and the prior day’s data.

      For reach of a specific post, you’d use a URL like this:https://graph.facebook.com/POST_ID/insights?metric=reach

      Reply
  12. Hey! This is such a great tool. I’m trying to aggregate daily data very similar to Rono in the comment above. I’ve pulled the data but all the dates are populated in this format: “2021-01-01T08:00:00+0000”. This appears to have aggregated the reporting based on the PST timezone rather than as UTC. Do you know if there is a way to aggregate the data using UTC? (I’m trying to match these metrics to other datasources that are using UTC.) According to the documentation, they say that the API is delivering in UTC so I’m wondering if I just have a parameter off or something. https://developers.facebook.com/docs/instagram-api/guides/insights#utc

    -Ted
    https://moonbaselabs.com/

    Reply
    • Thanks, Ted! I get the same results, each day comes back as yyyy-mm-ddT08:00:00+0000, which as you noted corresponds to midnight PST.
      I believe this is Facebook’s default timezone, and I don’t see any way to change it. In one place their documentation alludes to this, though not very clearly (it says Metrics that support lifetime periods will have results returned in an array of 24 hour periods, with periods ending on UTC−07:00)

      Reply
  13. Is there a way of getting just the follower count when using https://graph.facebook.com/v9.0/BUSINESS_ID?fields=followers_count? Whenever I use that command, It also prints off the ID, and headers for the columns.

    Reply
  14. Hey guys,

    Great tool so far!
    I’m having trouble importing Instagram Story data tho.
    Here’s my URL:
    https://graph.facebook.com/v11.0/XXXXX/media?fields=caption,comments_count,id,ig_id,is_comment_enabled,like_count,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username,video_title&limit=100

    The only data.media_product_type I get is “FEED”.
    Where’s my mistake?

    – Mark

    Reply
  15. when I use https://graph.facebook.com/v12.0/111111111111/insights?metric=reach,impressions,profile_views&period=day&since=+++Inputs!B1+++&until=+++Inputs!B2+++
    it only showed “Request processed, no records found”. But I import 14 days data, it is impossible to have this problem.

    Reply
    • Can you please check the raw data response from the API? If you see data there but not in your sheet, then click Output options > View more > JMESPath to make sure there’s no JMESPath expression filtering your data. You can also click Edit Fields and make sure you haven’t filtered out the fields in your request.

      Reply
      • Hmm I can’t seem to replicate this, I just tested several scenarios and could only get the “no records found” error when I filtered out all fields in the field editor. Even using invalid timestamps didn’t produce this error. Mind contacting support so I can take a look?

  16. Hello,

    is there a chance to make that reporting from Instagram to Sheets fully automatic, lets say everyday the API Connector would update the info in Sheets so I dont need to provide any activities manually?

    Thabk you in advance.

    Reply
      • Thank you for your reply, Ana. Let me please have another question: how can I get the number of gained followers and unfollows for the last 24 hours?

        Thank you so much in advance.

      • Hi El, the Instagram API doesn’t have a metric for unfollows. I think the following 2 requests will be useful for you:
        1) https://graph.facebook.com/v12.0/1111111?fields=followers_count. This provides a count of total followers at the time you run the request.
        2) https://graph.facebook.com/v12.0/1111111/insights?metric=follower_count&period=day. This provides a count of new followers per day. You can optionally add since and until parameters to get more data (up to a range of 30 days).
        From here you can calculate the number of unfollows (e.g. If you have 100 followers on day 1, 110 followers on day 2, and 12 new followers, that means you have 2 unfollows).

        It’s a bit clunky but that’s the best we can do with the data provided.

      • So, there is no chance to get the number of followers for the last 7 or 30 days? I can only get the number of actual followers?

        Thanks in advance

      • You can, the second request URL shows how to get the number of new followers by day. You could get data for the last week by adding since/until stamps like this: https://graph.facebook.com/v12.0/17841403836625216/insights?since=1648598400&until=1649203200&metric=follower_count&period=day

  17. Hi Ana,
    Is there any way to import some sort of shares count onto google sheets? I have tried including “shares” or “shares_count” into multiple URLs but it doesn’t read it. Is this a possibility? Or is this data not included? Thank you.

    Reply

Leave a Comment

Table of Contents