DEV Community 👩‍💻👨‍💻

Jan Vlnas for Superface

Posted on • Updated on • Originally published at superface.ai

Instagram API: Find the right account ID

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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.

Get Instagram account easier way

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
Enter fullscreen mode Exit fullscreen mode

And paste the following code into profiles.js file:

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

// Replace the value with the token from Graph Explorer
const ACCESS_TOKEN = 'YOUR USER ACCESS TOKEN HERE'

const sdk = new SuperfaceClient();

async function getProfiles() {
  const sdkProfile = await sdk.getProfile('social-media/publishing-profiles@1.0.1');
  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);
});
Enter fullscreen mode Exit fullscreen mode

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:

Graph API Explorer right sidebar with button “Copy to clipboard” next to the access token highlighted

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/...'
  }
]
Enter fullscreen mode Exit fullscreen mode

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

Conclusion

With the Instagram account ID, we can start managing the account via API. In the following articles I will cover publishing of images and videos, authorization flow, and retrieving posts and comments. If you don't want to miss it, follow Superface on Dev.

Was this tutorial useful or did you run into any problems? Let me know in the comments! 👇

Top comments (0)

🌚 Life is too short to browse without dark mode