FIND THE RIGHT ACCOUNT ID

AUTHOR'S NOTE

This article was originally published on Superface blog before the
pivot to agentic tooling platform and is republished here with
company's permission.

I am no longer working with Instagram API and the content of this
article may be outdated.

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

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.

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.

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.

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

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}

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.

GET INSTAGRAM ACCOUNT EASIER WAY

AUTHOR'S NOTE

Note that the following section is outdated. The code is provided for
historical reference only.

Picking a correct Instagram account is a pretty basic integration
task, so we've built an easier way to do that. If you use Node.js,
before you grab fetch and start building your custom abstraction, try
OneSDK. We have an integration ready to get a list of authorized
Instagram accounts. And the same interface works also for Facebook,
LinkedIn, Pinterest, and Twitter – but let's keep it for another
time.

First, install OneSDK into your project:

npm i @superfaceai/one-sdk@2

And paste the following code into profiles.js file:

const { SuperfaceClient } = require('@superfaceai/one-sdk');

const ACCESS_TOKEN = 'YOUR USER ACCESS TOKEN HERE';

const sdk = new SuperfaceClient();

async function getProfiles() {
  const sdkProfile = await sdk.getProfile(
    'social-media/[email protected]'
  );
  const result = await sdkProfile
    .getUseCase('GetProfilesForPublishing')
    .perform(
      {},
      { provider: 'instagram', parameters: { accessToken: ACCESS_TOKEN } }
    );

  try {
    return result.unwrap();
  } catch (error) {
    console.error(error);
  }
}

getProfiles().then((result) => {
  console.log(result.profiles);
});

Don't forget to insert your actual user access token as a value of the
ACCESS_TOKEN variable. You can copy it from Graph API Explorer:

When you run this code, you will get an array with authorized
accounts, their ID, username, and profile image:

$ node profiles.js
[
  {
    id: '17841455280630860',
    name: 'Dev Testing App IG Acct',
    username: 'dev_testing_app',
    imageUrl: 'https://scontent.fprg5-1.fna.fbcdn.net/...'
  }
]

The code behind the integration is open-source and your application
communicates with Instagram API directly without intermediary servers.