Uploading an Instagram Reel using Python and Instagram Graph API

Ritik khandelwal
4 min readDec 5, 2022

--

In this blog, we will be discussing how to upload a reel using python and Instagram Graph API. This a continuation of my previous blog in which I explained how to create a Facebook developer account and how to create an app and all the things that are required for this blog. You can follow this.

To follow this blog you should have.

  1. Instagram access token.
  2. Instagram account_id.

If you got the things that are required then follow me.

So let us upload a reel using the Instagram Graph API but before that, there are some guidelines provided by the Instagram end. They are

  • Container: MOV or MP4 (MPEG-4 Part 14), no edit lists, moov atom at the front of the file.
  • Audio codec: AAC, 48khz sample rate maximum, 1 or 2 channels (mono or stereo).
  • Video codec: HEVC or H264, progressive scan, closed GOP, 4:2:0 chroma subsampling.
  • Frame rate: 23–60 FPS.
  • Picture size:
  • Maximum columns (horizontal pixels): 1920
  • Required aspect ratio is between 0.01:1 and 10:1 but we recommend 9:16 to avoid cropping or blank spaces.
  • Video bitrate: VBR, 25Mbps maximum
  • Audio Bitrate: 128kbps
  • Duration: 15 mins maximum, 3 seconds minimum
  • File size: 1GB maximum

There are two phases for publishing the reels

  1. Upload the video to the container.
  2. Publishing the container.
  3. Upload the video to the Container.
    - For uploading a video you require the Instagram account id, permission to be asked while getting the access token, and the URL of your video that should be publicly available.
    - The permission required for posting a video, photo, or reel is.
    - instagram_basic, instagram_content_publish, pages_read_engagement and pages_show_list.
    - As soon as you have everything ready you are good to follow. So first we will create a URL to which we will be making a post request by adding the Instagram account id Instagram graph URL and ‘/media’ together.
    - We will create a dictionary to store all the parameters that we need to pass along with the URL i.e. access_token, caption, media_type, share_to_feed, thumb_offset, video_url.
    - Now we will make a post request to the URL with all of the parameters that we have defined here and store it inside the response variable.
    - After storing the response we will convert it into JSON.

2. Publishing the container.
- After uploading the reel video in a container we need to check if its upload is done or not by checking the status of the container using the container _id provided when we made the post request.
- The status function takes access_token and ig_container_id as a parameter for the function.
- To check the status we will first make a URL to which we will pass the Instagram container id as a path.
- then we will pass the access_token as a parameter along with the fields as a ‘status_code’ this will inform us of the status of our video and then we can decide to publish the video.
- If the value of the status code is ‘FINISHED’ that means the video has been uploaded and we are good to publish the video.

— Now we got the status as Finished now we can upload the video.
— For publishing the container that contains our reel we need the creation_id i.e. the container_id that we got when we uploaded the video.
— First, we will create a URL using instagram_account_id,graph URL and adding the path ‘\media_publish’.
— Then we will create a dictionary to store the access_token and the container_id that we will pass while making a post request to the URL.
— We will make a post request to the URL we created earlier and pass the parameter and store the output in a variable.
— Then we will convert the response variable into JSON that will give us the media_id. In this way, we can upload a REEL on Instagram.

This is an output I have done on my demo Instagram account.

I hope you like the blog if you have any doubts you can comment I will do my best to solve your doubt. I am always open to suggestions if you have one you can also comment that, this will be a great help for me moving forward.

Thank You.

--

--

Ritik khandelwal

Software engineer, Python developer, Full Stack Web Devloper, trying learn and share