Distinguish between Business and Creators with the Instagram Graph API

Tags: php, graphql
Published: 2021-05-22 08:41:19

>TL;DR: Use Try/Catch on an empty Content Publishing call and look for a specific exception code to differentiate between Business and Creators accounts.

If you use the new [Instagram Graph API](https://developers.facebook.com/docs/instagram-api/) (not to be confused with the [Instagram Basic Display API](https://developers.facebook.com/docs/instagram-basic-display-api/)) you know that these APIs can be used by Instagram Business Accounts and Creators Accounts.

Until recently, the difference between the two didn’t matter as much, since most features are available to both groups. However, when the new [Instagram Content Publishing Api](https://developers.facebook.com/docs/instagram-api/guides/content-publishing/) was released, Facebook made it only available for Business Accounts, not Creators.

This means that an Instagram Creator can Log In via Facebook and allow the permission for Content Publishing; but when you actually try to publish the content on their page, the API will throw an exception, saying that the user is not allowed to do so.

The main concern we encountered for [Lnk.Bio](https://lnk.bio/) is that we need to immediately inform the user that they won’t be able to use this specific feature. Otherwise they might start scheduling the posts for the future and we wouldn’t actually be able to publish them.

At first, we thought this information would be readily available within the [IG User](https://developers.facebook.com/docs/instagram-api/reference/ig-user/) object, returned when you query the Instagram Graph API asking information about the logged user. However this endpoint doesn’t contain any information about the account type (Business/Creator). What makes this even stranger is that instead the Instagram Basic Display API provide this information in the `account_type` field of the [User Object](https://developers.facebook.com/docs/instagram-basic-display-api/reference/user). The returned value can be one of BUSINESS, MEDIA_CREATOR, or PERSONAL, which is exactly what we were looking for. I can’t really find a reason why this is available in the Basic Display API and not the Graph API.

Looking at [StackOverflow](https://stackoverflow.com/questions/57306647/is-there-a-way-to-tell-if-its-an-instagram-creator-or-business-account?noredirect=1#comment119514442_57306647) it looked like the most common approach for the Graph API is to [scrape the user profile](https://stackoverflow.com/a/58806624/1529324), since this information is published in the page JSON. But this is of course not allowed by Instagram T&C and is also not a solid solution, as it would be subject to network pitfalls, page errors, and also Instagram anti-crawler policy that would block your IPs after too many requests. So this was a no go.

In the end the best solution we found is to perform an empty POST request simulating the publishing of an Instagram content to the connected account, catch the exception and look for a specific Exception Code 10 in order to identify the type of account.

In order not to actually publish or create anything on the user account, you can perform an empty call (POST method without any POST data) to the /userid/media endpoint. Then catch the FacebookResponseException and look for the code=10.

In PHP code:

```
public function isBusiness() {
    try {
        $this->conn->post("/".$this->fb_id."/media", [], $this->getToken());
        return true;
    } catch (FacebookResponseException $e) {
        if ($e->getCode() == 10) {
        return false;
    } catch (\Exception $e) {
        return true;
    }
}
```

As you can see from the code above, all other exceptions are interpreted as Business Account, while only the Exception with code 10 is interpreted as Creator Account.

If you have a better approach to this without performing any call, let me know in the comments!