Instagram Management

Publish photos and respond to comments on Instagram using the Facebook Business SDK.

Requirements

Endpoints

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

Get your Instagram Business Account ID

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

Sample Code

$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
);

Publish Photos

Send a POST request 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 can use the /instagram_business_account/media edge to create the container and publish using the /user/media_publish edge:

Sample Code

$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 IGUser($ig_account))->createMedia(
  $fields,
  $params
);
$ig_media_id = $ig_media->id;
$fields = array(
);
$params = array(
  'creation_id' => $ig_media_id,
);
$ig_post = (new IGUser($ig_account))->createMediaPublish(
  $fields,
  $params
);
$fields = array(
);
$params = array(
);
$ig_postss = (new IGUser($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 = IGUser(ig_account).create_media(
    fields=fields,
    params=params,
)
ig_media_id = ig_media.get_id()
fields = [
]
params = {
    'creation_id': ig_media_id,
}
ig_post = IGUser(ig_account).create_media_publish(
    fields=fields,
    params=params,
)
fields = [
]
params = {
}
ig_postss = IGUser(ig_account).get_media(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINode igMedia = new IGUser(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();
IGMedia igPost = new IGUser(ig_account, context).createMediaPublish()
  .setCreationId(ig_media_id)
  .execute();
APINodeList<IGMedia> igPostss = new IGUser(ig_account, context).getMedia()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
i_g_user = FacebookAds::IgUser.get(ig_account)
ig_media = 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 = i_g_user.media_publish.create({
creation_id: ig_media_id,
})
ig_postss = 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 IGUser(ig_account)).createMedia(
  fields,
  params
);
let ig_media_id = ig_media.id;
fields = [
];
params = {
  'creation_id' : ig_media_id,
};
let ig_post = await (new IGUser(ig_account)).createMediaPublish(
  fields,
  params
);

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

Publish 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 and y coordinates within the photo itself. This would return a container ID which you would then publish using the /user/media_publish edge.

Sample Code

$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 IGUser($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 IGUser($ig_account))->createMediaPublish(
  $fields,
  $params
);
$fields = array(
);
$params = array(
);
$ig_postss = (new IGUser($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 = IGUser(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 = IGUser(ig_account).create_media_publish(
    fields=fields,
    params=params,
)
fields = [
]
params = {
}
ig_postss = IGUser(ig_account).get_media(
    fields=fields,
    params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINode igMediaWithTaggedUser = new IGUser(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();
IGMedia igMediaWithTaggedUser = new IGUser(ig_account, context).createMediaPublish()
  .setCreationId(ig_media_with_tagged_user_id)
  .execute();
APINodeList<IGMedia> igPostss = new IGUser(ig_account, context).getMedia()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
i_g_user = FacebookAds::IgUser.get(ig_account)
ig_media_with_tagged_user = 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 = i_g_user.media_publish.create({
creation_id: ig_media_with_tagged_user_id,
})
ig_postss = 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 IGUser(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 IGUser(ig_account)).createMediaPublish(
  fields,
  params
);

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

Caveats

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

Publish Photos with Locations

Include the place ID when publishing a photo to tag the photo with the place's location.

Get a Place ID

You can use our Places Search API to get an ID for any place that has been entered into our Places Graph. For example, if you took your photo at the Space Needle in Seattle, Washington, USA, you could get the Space Needles's Places Graph object ID.

Sample Request

curl -i -X GET "https://graph.facebook.com/search?
  type=place
  &q=space needle
  &access_token="{access-token}"

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, Seattle, Washington",
      "id": "114040329970973"
}

Include the location_id parameter when creating your media object container. This would return a container ID which you would then publish using the /user/media_publish edge.

Sample Code

$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 IGUser($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 IGUser($ig_account))->createMediaPublish(
  $fields,
  $params
);
$fields = array(
);
$params = array(
);
$ig_postss = (new IGUser($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 = IGUser(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 = IGUser(ig_account).create_media_publish(
fields=fields,
params=params,
)
fields = [
]
params = {
}
ig_postss = IGUser(ig_account).get_media(
fields=fields,
params=params,
)
context = new APIContext(page_access_token_for_ig).enableDebug(false);
APINode igMediaWithLocation = new IGUser(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();
IGMedia igPostWithLocation = new IGUser(ig_account, context).createMediaPublish()
  .setCreationId(ig_media_with_location_id)
  .execute();
APINodeList<IGMedia> igPostss = new IGUser(ig_account, context).getMedia()
  .execute();
FacebookAds::Session.default_session.access_token = page_access_token_for_ig
i_g_user = FacebookAds::IgUser.get(ig_account)
ig_media_with_location = 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 = i_g_user.media_publish.create({
creation_id: ig_media_with_location_id,
})
ig_postss = 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 IGUser(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 IGUser(ig_account)).createMediaPublish(
  fields,
  params
);

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

Learn More

Comment on an Instagram Business Account Posts

You can get comments on your media objects, analyze these comments, filter against specific criteria, then reply to any comments that match your criteria.

Step 1. Use the /media/comments edge to get all comments and their IDs.

Step 2. Select the comment to which you want to reply and use the comment ID to reply in the comment thread to the User.

Sample Code

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

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

Learn More