This document shows you how to upload media to the Meta servers using the Attachment Upload API. This media can then be used in Instagram messages.
Note: You can upload and send an attachment in a single API call.
This guide assumes you have read the Messenger Platform Overview and implemented the needed components for sending and receiving messages and notifications.
You need the following:
MESSAGING
task on the Pageinstagram_basic
instagram_manage_comments
instagram_manage_messages
pages_messaging
image
(which include GIFs), video
, or audio
Media Type | Supported Format | Supported Size Maximum |
---|---|---|
Audio | acc, m4a, wav, mp4 | 25MB |
Image | png, jpeg, gif | 8MB |
Video | mp4, ogg, avi, mov, webm | 25MB |
You can upload audio files, images, and videos from a URL.
To upload media from a URL, you can send a POST
request to the /PAGE-ID/message_attachments
endpoint with the platform set as Instagram and the message attachment type set to the type of media you are uploading, audio
, image
, or video
. Add the URL and is_reusable
in the payload. Set is_reusable
to true so that the media can be used in multiple messages.
Note: All keys within the message
object, such as attachment
, type
, and payload
are strings.
curl -i -X POST "https://graph.facebook.com/LATEST-API-VERSION/PAGE-ID/message_attachments ?message={ "attachment": { "type": "MEDIA-TYPE", "payload": { "url": "URL", "is_reusable": "true", }, } } &platform=instagram &access_token=PAGE-ACCESS-TOKEN"
Upon success, your app will receive an ID for the attachment. You can now include this ID in your messages.
{ "attachment_id": "ATTACHMENT-ID" }
You can upload audio files and videos from a server. Images must be uploaded from a URL.
To upload media from a server, you can send a POST
request to the /PAGE-ID/message_attachments
endpoint with the message attachment payload containing the URL and the platform set as Instagram. If you want to use the media in multiple messages, include the is_reusable
set to true in the payload.
curl -i -X POST "https://graph.facebook.com/LATEST-API-VERSION/PAGE-ID/message_attachments &platform=instagram &message={'attachment':{'type':'MEDIA-TYPE-AUDIO-OR-VIDEO','is_reusable':'true'}} &filedata=FILE-PATH;type=PATH-TYPE ?access_token=PAGE-ACCESS-TOKEN"
Upon success, your app will receive an ID for the attachment. You can now include this ID in your messages.
{ "attachment_id": "ATTACHMENT-ID" }
Now that you have uploaded media, you can send it in a message.
To send a message that contains the media you uploaded, send a POST
request to the /PAGE-ID/messages
endpoint with the recipient
parameter containing the Instagram-scoped ID (IGSID) and the message
parameter containing an attachment
object with the type
set to MEDIA_SHARE
and payload.id
set to the attachment ID.
Your business must own the media to be used in the message.
curl -i -X POST "https://graph.facebook.com/v21.0
/PAGE-ID/messages
?recipient={id: IGSID}
&message={
attachment:
{
type:MEDIA_SHARE,
payload:{attachment_id:ATTACHMENT-ID}
}
}
&access_token=PAGE-ACCESS-TOKEN"
Upon success, your app will receive the following JSON response:
{ "recipient_id": "IGSID", "message_id": "MESSAGE-ID" }
You can also upload media and send it in a single API request.
To upload and send media in one request, send a POST
request to the /PAGE-ID/messages
endpoint with the recipient
parameter containing the Instagram-scoped ID (IGSID) and the message
parameter containing an attachment
object with the type
set to audio
, image
, or video
and payload
containing the URL and is_reusable
set to true.
Formatted for readability.
curl -i -X POST "https://graph.facebook.com/LATEST-API-VERSION/PAGE-ID/messages ?recipient={id: IGSID} &message={ 'attachment': { 'type':'video', 'payload':{'url':'https://you-video-url.mp4'}, 'is_reusable': 'true', } } &access_token=PAGE-ACCESS-TOKEN"
Upon success, your app will receive the following JSON response:
{ "recipient_id": "IGSID", "message_id": "MESSAGE-ID", "attachment_id": "ATTACHMENT-ID" }
To upload and send a audio or video from your server, send a POST
request to the /PAGE-ID/messages
endpoint with the recipient
parameter containing the Instagram-scoped ID (IGSID) and the message
parameter containing an attachment
object with the type
set to AUDIO
or VIDEO
and filedata
parameter the file's location and type. The format for filedata
values looks like @/path_on_my_server/video.mp4;type=video/mp4.
Formatted for readability.
curl -i -X POST "https://graph.facebook.com/LATEST-API-VERSION/PAGE-ID/messages ?recipient={id: IGSID} &message={ 'attachment': { 'type':'MEDIA-TYPE-AUDIO-OR-VIDEO' 'is_reusable':'true' } } &'filedata':'FILE-PATH;type=PATH-TYPE' &access_token=PAGE-ACCESS-TOKEN"
Upon success, your app will receive the following JSON response:
{ "recipient_id": "IGSID", "message_id": "MESSAGE-ID", "attachment_id": "ATTACHMENT-ID" }