Instagram Management

Learn how to publish photos and respond to comments on Instagram using the Facebook Business SDK.

Prerequisites

  • An Instagram Business Account ID

Follow the steps in the Instagram Getting Started Guide to connect an Instagram Account to a Page.

Endpoints

Refer to each endpoint's reference documentation for parameter and permission requirements.

Getting your Instagram Business Account ID

Before you can publish to Instagram, you need to get your Instagram Business Account ID.

Sample Request

$api = Api::init($app_id, null, $page_access_token_for_ig);
$fields = array(
  'instagram_business_account',
);
$params = array(
);
$page = (new Page(<PAGE_ID_FOR_IG>))->getSelf(
  $fields,
  $params
);
FacebookAdsApi.init(access_token=page_access_token_for_ig)
fields = [
    'instagram_business_account',
]
params = {
}
page = Page(<PAGE_ID_FOR_IG>).api_get(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
Page page = new Page(<PAGE_ID_FOR_IG>, context).get()
  .requestInstagramBusinessAccountField()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
page = FacebookAds::Page.get(<PAGE_ID_FOR_IG> ,'instagram_business_account,').load!
bizSdk.FacebookAdsApi.init(page_access_token_for_ig);
fields = [
  'instagram_business_account',
];
params = {
};
let page = await (new Page(<PAGE_ID_FOR_IG>)).get(
  fields,
  params
);

Publishing Photos

Use the /instagram_business_account/media edge to publish photos.

For example, let's say you have a photo at...

https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2amp;h=750amp;w=1260

... that you want to publish with "#kitten" as its caption. You could use the /instagram_business_account/media edge to create the container and publish using the /user/media_publish edge:

Sample Request

$api = Api::init($app_id, null, $page_access_token_for_ig);
$fields = array(
);
$params = array(
  'image_url' => 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  'caption' => '#kitten',
);
$ig_media = (new ShadowIGUser($ig_account))->createMedia(
  $fields,
  $params
);
$ig_media_id = $ig_media->id;
$fields = array(
);
$params = array(
  'creation_id' => $ig_media_id,
);
$ig_post = (new ShadowIGUser($ig_account))->createMediaPublish(
  $fields,
  $params
);
$fields = array(
);
$params = array(
);
$ig_postss = (new ShadowIGUser($ig_account))->getMedia(
  $fields,
  $params
);
FacebookAdsApi.init(access_token=page_access_token_for_ig)
fields = [
]
params = {
    'image_url': 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
    'caption': '#kitten',
}
ig_media = ShadowIGUser(ig_account).create_media(
    fields=fields,
    params=params,
)
ig_media_id = ig_media.get_id()
fields = [
]
params = {
    'creation_id': ig_media_id,
}
ig_post = ShadowIGUser(ig_account).create_media_publish(
    fields=fields,
    params=params,
)
fields = [
]
params = {
}
ig_postss = ShadowIGUser(ig_account).get_media(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINode igMedia = new ShadowIGUser(ig_account, context).createMedia()
  .setImageUrl("https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
  .setCaption("#kitten")
  .execute();
String ig_media_id = igMedia.getId();
ShadowIGMedia igPost = new ShadowIGUser(ig_account, context).createMediaPublish()
  .setCreationId(ig_media_id)
  .execute();
APINodeList<ShadowIGMedia> igPostss = new ShadowIGUser(ig_account, context).getMedia()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
shadow_i_g_user = FacebookAds::ShadowIgUser.get(ig_account)
ig_media = shadow_i_g_user.media.create({
image_url: 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
caption: '#kitten',
})
ig_media_id = ig_media.id
print 'ig_media_id:', ig_media_id
ig_post = shadow_i_g_user.media_publish.create({
creation_id: ig_media_id,
})
ig_postss = shadow_i_g_user.media({
})
bizSdk.FacebookAdsApi.init(page_access_token_for_ig);
fields = [
];
params = {
  'image_url' : 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  'caption' : '#kitten',
};
let ig_media = await (new ShadowIGUser(ig_account)).createMedia(
  fields,
  params
);
let ig_media_id = ig_media.id;
fields = [
];
params = {
  'creation_id' : ig_media_id,
};
let ig_post = await (new ShadowIGUser(ig_account)).createMediaPublish(
  fields,
  params
);

fields = [
];
params = {
};
let ig_postss = await (new ShadowIGUser(ig_account)).getMedia(
  fields,
  params
);

Publishing Photos with Tagged Users

You can tag public Instagram Users in a photo and they will receive a notification when you publish the media container.

To do this, include the user_tags parameter and an array of objects indicating the Users in the photo as well as their x/y coordinates within the photo itself. This would return a container ID which you would then publish using the /user/media_publish edge.

Sample Request

$api = Api::init($app_id, null, $page_access_token_for_ig);
$fields = array(
);
$params = array(
  'image_url' => 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  'caption' => '#kitten',
  'user_tags' => array(array('username' => 'ellentestaccount','x' => '0.5','y' => '0.8',)),
);
$ig_media_with_tagged_user = (new ShadowIGUser($ig_account))->createMedia(
  $fields,
  $params
);
$ig_media_with_tagged_user_id = $ig_media_with_tagged_user->id;
$fields = array(
);
$params = array(
  'creation_id' => $ig_media_with_tagged_user_id,
);
$ig_media_with_tagged_user = (new ShadowIGUser($ig_account))->createMediaPublish(
  $fields,
  $params
);
$fields = array(
);
$params = array(
);
$ig_postss = (new ShadowIGUser($ig_account))->getMedia(
  $fields,
  $params
);
FacebookAdsApi.init(access_token=page_access_token_for_ig)
fields = [
]
params = {
    'image_url': 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
    'caption': '#kitten',
    'user_tags': [{'username':'ellentestaccount','x':'0.5','y':'0.8',}],
}
ig_media_with_tagged_user = ShadowIGUser(ig_account).create_media(
    fields=fields,
    params=params,
)
ig_media_with_tagged_user_id = ig_media_with_tagged_user.get_id()
fields = [
]
params = {
    'creation_id': ig_media_with_tagged_user_id,
}
ig_media_with_tagged_user = ShadowIGUser(ig_account).create_media_publish(
    fields=fields,
    params=params,
)
fields = [
]
params = {
}
ig_postss = ShadowIGUser(ig_account).get_media(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINode igMediaWithTaggedUser = new ShadowIGUser(ig_account, context).createMedia()
  .setImageUrl("https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
  .setCaption("#kitten")
  .setParam("user_tags", "[{\'username\':\'ellentestaccount\',\'x\':\'0.5\',\'y\':\'0.8\',}]")
  .execute();
String ig_media_with_tagged_user_id = igMediaWithTaggedUser.getId();
ShadowIGMedia igMediaWithTaggedUser = new ShadowIGUser(ig_account, context).createMediaPublish()
  .setCreationId(ig_media_with_tagged_user_id)
  .execute();
APINodeList<ShadowIGMedia> igPostss = new ShadowIGUser(ig_account, context).getMedia()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
shadow_i_g_user = FacebookAds::ShadowIgUser.get(ig_account)
ig_media_with_tagged_user = shadow_i_g_user.media.create({
image_url: 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
caption: '#kitten',
user_tags: [{'username':'ellentestaccount','x':'0.5','y':'0.8',}],
})
ig_media_with_tagged_user_id = ig_media_with_tagged_user.id
print 'ig_media_with_tagged_user_id:', ig_media_with_tagged_user_id
ig_media_with_tagged_user = shadow_i_g_user.media_publish.create({
creation_id: ig_media_with_tagged_user_id,
})
ig_postss = shadow_i_g_user.media({
})
bizSdk.FacebookAdsApi.init(page_access_token_for_ig);
fields = [
];
params = {
  'image_url' : 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  'caption' : '#kitten',
  'user_tags' : [{'username':'ellentestaccount','x':'0.5','y':'0.8',}],
};
let ig_media_with_tagged_user = await (new ShadowIGUser(ig_account)).createMedia(
  fields,
  params
);
let ig_media_with_tagged_user_id = ig_media_with_tagged_user.id;
fields = [
];
params = {
  'creation_id' : ig_media_with_tagged_user_id,
};
let ig_media_with_tagged_user = await (new ShadowIGUser(ig_account)).createMediaPublish(
  fields,
  params
);

fields = [
];
params = {
};
let ig_postss = await (new ShadowIGUser(ig_account)).getMedia(
  fields,
  params
);

Notes

  • The user_tags value must be an array of objects formatted with JSON.
  • You can only tag Users with public Instagram accounts.
  • The object must contain all three properties (username, x, and y) for each user.
  • x and y values must be float numbers that originate from the top-left of the image, with a range of 0.01.0.

Publishing Photos with Locations

You can use our Places Search API to get an ID for any Place that has been entered into our Places Graph. If you include the ID when publishing a photo, the photo will be tagged with the Place's location.

To do this, first query the Places Graph API to get the Place's ID. For example, let's say you took your photo at the Space Needle in Seattle, Washington, USA. You could get the Space Needles's Places Graph object ID like this:

GET https://graph.facebook.com/search?
  type=place
  &q=space needle

This would return an array of objects with "Space Needle" in their name, and their IDs, one of which is for the Space Needle in Seattle:

{
      "name": "Space Needle",
      "id": "88603851976"
}

There are different ways to query the Places Graph. We won't cover that here, so please refer to the Places Search API developer documents to learn the various ways to use it.

Once you have the Place's ID, include the location_id parameter to pass us the id when creating your media object container. This would return a container ID which you would then publish using the /user/media_publish edge.

Sample Request

$api = Api::init($app_id, null, $page_access_token_for_ig);
$fields = array(
);
$params = array(
  'image_url' => 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  'caption' => '#kitten',
  'location_id' => '88603851976',
);
$ig_media_with_location = (new ShadowIGUser($ig_account))->createMedia(
  $fields,
  $params
);
$ig_media_with_location_id = $ig_media_with_location->id;
$fields = array(
);
$params = array(
  'creation_id' => $ig_media_with_location_id,
);
$ig_post_with_location = (new ShadowIGUser($ig_account))->createMediaPublish(
  $fields,
  $params
);
$fields = array(
);
$params = array(
);
$ig_postss = (new ShadowIGUser($ig_account))->getMedia(
  $fields,
  $params
);
FacebookAdsApi.init(access_token=page_access_token_for_ig)
fields = [
]
params = {
    'image_url': 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
    'caption': '#kitten',
    'location_id': '88603851976',
}
ig_media_with_location = ShadowIGUser(ig_account).create_media(
    fields=fields,
    params=params,
)
ig_media_with_location_id = ig_media_with_location.get_id()
fields = [
]
params = {
    'creation_id': ig_media_with_location_id,
}
ig_post_with_location = ShadowIGUser(ig_account).create_media_publish(
    fields=fields,
    params=params,
)
fields = [
]
params = {
}
ig_postss = ShadowIGUser(ig_account).get_media(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINode igMediaWithLocation = new ShadowIGUser(ig_account, context).createMedia()
  .setImageUrl("https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
  .setCaption("#kitten")
  .setParam("location_id", "88603851976")
  .execute();
String ig_media_with_location_id = igMediaWithLocation.getId();
ShadowIGMedia igPostWithLocation = new ShadowIGUser(ig_account, context).createMediaPublish()
  .setCreationId(ig_media_with_location_id)
  .execute();
APINodeList<ShadowIGMedia> igPostss = new ShadowIGUser(ig_account, context).getMedia()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
shadow_i_g_user = FacebookAds::ShadowIgUser.get(ig_account)
ig_media_with_location = shadow_i_g_user.media.create({
image_url: 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
caption: '#kitten',
location_id: '88603851976',
})
ig_media_with_location_id = ig_media_with_location.id
print 'ig_media_with_location_id:', ig_media_with_location_id
ig_post_with_location = shadow_i_g_user.media_publish.create({
creation_id: ig_media_with_location_id,
})
ig_postss = shadow_i_g_user.media({
})
bizSdk.FacebookAdsApi.init(page_access_token_for_ig);
fields = [
];
params = {
  'image_url' : 'https://images.pexels.com/photos/416160/pexels-photo-416160.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  'caption' : '#kitten',
  'location_id' : '88603851976',
};
let ig_media_with_location = await (new ShadowIGUser(ig_account)).createMedia(
  fields,
  params
);
let ig_media_with_location_id = ig_media_with_location.id;
fields = [
];
params = {
  'creation_id' : ig_media_with_location_id,
};
let ig_post_with_location = await (new ShadowIGUser(ig_account)).createMediaPublish(
  fields,
  params
);

fields = [
];
params = {
};
let ig_postss = await (new ShadowIGUser(ig_account)).getMedia(
  fields,
  params
);

Learn more about publishing photos to Instagram, including restrictions, limitations, and errors.

Commenting on an Instagram Business Account Posts

You can get all of the comments on one of your media objects, analyze, and filter the returned data set against specific criteria, then reply to any comments that match your criteria.

First, use the /media/comments edge to get all of the comments and their IDs on your media object. Next, select the comment you want to reply to. In this code example we are replying to the first comment returned in our search. Then, use the comment ID to reply in the comment thread to the User who made the comment.

Sample Request

$api = Api::init($app_id, null, $page_access_token_for_ig);
$fields = array(
);
$params = array(
);
$ig_comments = (new ShadowIGMedia(<IG_POST_ID>))->getComments(
  $fields,
  $params
);
$ig_comment_id = $ig_comments[0]->id;
$fields = array(
);
$params = array(
);
$ig_comment_repliess = (new ShadowIGComment($ig_comment_id))->getReplies(
  $fields,
  $params
);
FacebookAdsApi.init(access_token=page_access_token_for_ig)
fields = [
]
params = {
}
ig_comments = ShadowIGMedia(<IG_POST_ID>).get_comments(
    fields=fields,
    params=params,
)
ig_comment_id = ig_comments[0].get_id()
fields = [
]
params = {
}
ig_comment_repliess = ShadowIGComment(ig_comment_id).get_replies(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINodeList<ShadowIGComment> igComments = new ShadowIGMedia(<IG_POST_ID>, context).getComments()
  .execute();
String ig_comment_id = igComments.get(0).getId();
ShadowIGComment igComment = new ShadowIGComment(ig_comment_id, context).get()
  .execute();
APINodeList<ShadowIGComment> igCommentRepliess = new ShadowIGComment(ig_comment_id, context).getReplies()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
shadow_i_g_media = FacebookAds::ShadowIgMedia.get(<IG_POST_ID>)
ig_comments = shadow_i_g_media.comments({
})
ig_comment_id = ig_comments[0].id
print 'ig_comment_id:', ig_comment_id
shadow_i_g_comment = FacebookAds::ShadowIgComment.get(ig_comment_id)
ig_comment_repliess = shadow_i_g_comment.replies({
})
bizSdk.FacebookAdsApi.init(page_access_token_for_ig);
fields = [
];
params = {
};
let ig_comments = await (new ShadowIGMedia(<IG_POST_ID>)).getComments(
  fields,
  params
);
let ig_comment_id = ig_comments[0].id;
fields = [
];
params = {
};
let ig_comment = await (new ShadowIGComment(ig_comment_id)).get(
  fields,
  params
);

fields = [
];
params = {
};
let ig_comment_repliess = await (new ShadowIGComment(ig_comment_id)).getReplies(
  fields,
  params
);

If you have a lot of comments that you want to reply to, you could batch the replies into a single request.