Published on

Instagram API: Find the right account ID

Authors
  • Jan Vlnas
    Name
    Jan Vlnas
    Title
    Superface Alumni
    Social media profiles
    Fediverse

This article was updated on 6 March 2024 to remove references to functionality that Superface no longer offers. The steps outlined below for locating an Instagram Accout ID are still valid.

To manage your Instagram account through an API, you need two things: a user access token and a business account ID. Getting an access token is easy, but figuring out the account ID takes a few steps and sometimes causes a confusion.

Prerequisites: This tutorial assumes you have an Instagram business account connected with a Facebook page and Facebook application with Instagram Graph API enabled. Check my previous article on how to set up a test account for Instagram API.

If you are currently troubleshooting requests to Instagram's API and can't wrap your head around their IDs, skip right away to common ID confusions.

Get an access token

For the following steps, I will use Graph API Explorer. This is a useful tool for trying out Facebook's APIs and also to obtain access tokens for authorized API access.

In the right sidebar, select the previously created application under “Meta App”. Make sure “User Token” is selected and add the following permissions: instagram_basic, pages_show_list, and instagram_content_publish (this will come handy in later tutorials).

Detail of Graph API Explorer sidebar with selected Meta App, User Token, and required permissions

Finally, click “Generate Access Token”. Facebook will display a pop-up with prompts to authorize access to your Instagram and Facebook pages. Make sure to select both your testing Instagram account and the associated Facebook page.

Facebook authorization dialog with Instagram account selected

Find your Instagram account ID

If you authorized the application correctly, you should be able to list Facebook pages the application has access to.

Enter the following path to the Graph API Explorer: me/accounts. After submitting, you should see Facebook pages you gave your application access to.

Graph API Explorer with path set to me/accounts and authorized page in response, page's ID is highlighted.

Now, copy the value id of your page and enter the following path:

<page ID>?fields=instagram_business_account

In the response, you will see both the Facebook's page ID and the ID of your Instagram account. Copy the value of id under instagram_business_account – this ID is necessary for further interactions with your Instagram account via API.

Graph API Explorer with path set to Facebook page ID and fields set to instagram_business_account. In response, there is a nested field “id” under instagram_business_account object.

Get account details

With this ID, we can retrieve basic information about our Instagram account, like its username, name, and profile picture. Try querying the following path with your Instagram business account ID:

<business account ID>?fields=id,name,username,profile_picture_url
Graph API Explorer with Instagram account details

All in single request

There is also an undocumented way to retrieve Instagram account details in a single request from me/accounts. This way, we can skip intermediary requests and retrieve authorized Instagram accounts directly:

me/accounts?fields=instagram_business_account{id,name,username,profile_picture_url}
Graph API Explorer with Instagram account details retrieved from me/accounts edge

In Facebook's Graph API, it is possible to traverse some edges within a single request – this is what the curly braces in the fields query parameter are for. In other words, we are telling the API, “give me these fields for instagram_business_account edge under me/accounts edge”.

Common ID confusions

A common source of mistakes when dealing with Graph API is use of an incorrect type ID. If the API doesn't behave like you expect, check if you have the right ID. In case of Instagram Graph API, we are dealing with the following IDs:

  • Facebook Page ID – retrieved from me/accounts endpoint identifies Page node and acts as an entry point to get an Instagram account associated with the Page.
  • Instagram (Business) Account ID – retrieved from Page node under instagram_business_account edge. Documentation refers to it as IG User node. It must be accessed with a user access token.
  • ig_id or Instagram User ID – this is an ID of the Instagram account from the legacy, deprecated API. It is intended for migration of applications using the pre-graph API, but it's not used anywhere else. It's also represented as a number, while Graph API IDs are strings.

Was this tutorial useful or did you run into any problems? Let me know on Twitter!

Cover photo credit: Solen Feyissa on Unsplash

Automate the impossible.
Superface. The LLM-powered automation agent that connects to all your systems.

Try it now