Automating Instagram Posts with Python and Instagram Graph API

Automating Instagram Posts with Python and Instagram Graph API

In this tutorial we will learn about automating instagram posts with python and instagram graph API. Yes, you can now schedule your instagram posts programatically without relying on limiting and expensive apps like Hootsuite and Later. You can create your own code to post to instagram up to 25 posts a day using the Instagram Graph API. 

What is included in this automating instagram posts with Python and Instagram Graph API

  • Connect your facebook page to your instagram account
  • Create facebook developer account and generate access tokens for Instagram API
  • Get your instagram graph API user ID for your instagram page
  • Publish an image to your instagram page
  • Python code for publishing an image

Before we get started, it is important to note that you can only ever schedule posts for an instagram business account, not a personal account. An influencer account is accepted as a business account, you need to set this up from the instagram app. 

Go to your profile > Click on the hamburger menu at the top right > click on settings right at the bottom > and select account > scroll to the bottom and you will see an option to switch to business / professional account. Go through the steps and you are set. 

Connecting your facebook page to instagram account

Log in to your facebook page, go to settings > instagram. You should see something like below:

If you don’t see this, there will be a button that allows you to connect the facebook page to an instagram account. Before you click on the button, it helps if you already have instagram logged in another window — browser has a cookie. You will be able to just connect that account, re-enter your password and you are done. 

Generate access tokens for instagram graph API

This is similar to what we did in the tutorial:

Instagram graph API access keys can be found in the same place as the facebook graph API keys, in fact you can use the same application that was created to post to facebook to generate the tokens — you just have to adjust the access rights. These are the access rights you need to add:

Visit: to add permissions to your app. Make sure you select the correct app from the dropdown if you have more than one app. 

These are all the permissions you will now have after adding the instagram permissions:


After you have added these permissions, click on Generate access token and you will go through the login process for both the facebook page and instagram page — the first time.

Note the access token you get from Graph API explorer is a short life token — only valid for an hour, you need to exchange this token with a long life token valid for 60days. You need the USER access token for instagram operations. 

Run the command below: 

curl -i -X GET "{graph-api-version}/oauth/access_token?  

You will need your: (1) App ID, (2) App secret and (3) Short life access token for this request. If successful, you will receive a long life token in response.

Get instagram business page User ID

All requests to the instagram graph API require a page user ID, you can get it easily using the user access token from above and the facebook page ID. The facebook page ID can be found from the about section of the facebook page you connected to the instagram account at the beginning of this tutorial.

curl -i -X GET \

That is it, you can now send posts directly to instagram using the API.

Publish a post to your instagram page using Instagram Graph API

Posts are sent to instagram in a two step process: (1) You create a container with the image, instagram returns you a creation id, (2) you publish the creation id to send the post to instagram.

NOTE: All requests need your user access token (preferably the long life token).

curl -i -X POST{ig_user_id}/media?image_url={public_http_path_to_image}&caption={url_encoded_caption}&access_token={user_access_token}

What you need for this request is:

  • IG user ID from section above
  • Path to image you want to post (ideally square image for IG and the path must be publicly accesible, like http or https server)
  • URL encoded caption
  • User access token

Then send the post using the creation id you will receive:


Publish to instagram using Python code

The fun begins, it would take way too much effort to go though all these steps just to post one image. The power of python is the ability to pre-schedule posts in advance, even create posts on the fly using html, the possibilities are endless:

import requests
import config
import json
def postInstagramQuote():
#Post the Image
image_location_1 = ''
post_url = '{}/media'.format(config.ig_user_id)
payload = {
'image_url': image_location_1,
'caption': 'Get jobs online on #career #hiring #jobs #job #jobssouthafrica #hiringnow,
'access_token': config.user_access_token
r =, data=payload)
result = json.loads(r.text)
if 'id' in result:
creation_id = result['id']
second_url = '{}/media_publish'.format(config.ig_user_id)
second_payload = {
'creation_id': creation_id,
'access_token': config.user_access_token
r =, data=second_payload)
print('--------Just posted to instagram--------')
print('HOUSTON we have a problem')

Subsequently, you can use the python schedule library to schedule these posts regularly.

For more, watch our youtube tutorial where we take it a step further to create actual images using python and imgkit from HTML, so you can create fresh content daily for instagram using code.

Youtube video on automating instagram posts with python and instagram graph API

Automate instagram posts with python and instagram graph API

Read more on instagram automation:

Leave a Reply

Your email address will not be published.

Subscribe to our mailing list

And get this 💃💃 Free😀 eBook !

Holler Box