Source

Using Python to Post Stories on Instagram

Hi everyone! 👋 In my book (Practical Python Projects), I initially had a section showing the readers how to upload stories on Instagram in an automated fashion. I ended up removing the code from the last draft of the book for various reasons. During the span of a few months, the library I was using to post on Instagram was removed from GitHub.

Just because this is not an officially supported feature by Instagram and the third-party libraries on GitHub come and go every couple of months, I decided not to add the code for automation in the book that is tied to any such third-party library. Just because I had initially advertised that I will show you how to upload stories to Instagram in the book, I decided to post the redacted part of the chapter on my blog.

Getting started

To upload the stories on Instagram, we need to either reverse engineer Instagram API ourselves or use an already existing library. I searched on Google for Instagram API in Python. What I ended up learning was that there is no actively maintained official Python API. I decided to use this unofficial API (this library has been taken down now). I searched around to check if there was a story upload function/method somewhere. My search led me to this closed pull request. The author of the pull request had closed it before it got merged with the source repo. I ended up downloading (cloning) the repository and made the required changes (by following the afore-mentioned PR) to make the story feature working.

You can download the code from my GitHub repository and then install the library from the downloaded folder by running:

$ python setup.py install

Now we can import InstagramAPI in our Python file:

from InstagramAPI import InstagramAPI

Now we need to login to Instagram using the API:

user = "your instagram username"
password = "your instagram password"
InstagramAPI = InstagramAPI(user, password)
InstagramAPI.login() 

Note: Make sure you change the user and password variable values.

Next, we need to upload the image using the uploadPhoto method:

photo_path = 'image_to_upload.jpg'
InstagramAPI.uploadPhoto(photo_path, is_story=True)

Final Code

The final code should look like this:

from InstagramAPI import InstagramAPI

user = "your instagram username"
password = "your instagram password"
InstagramAPI = InstagramAPI(user, password)
InstagramAPI.login() 

photo_path = 'image_to_upload.jpg'
InstagramAPI.uploadPhoto(photo_path, is_story=True)

Now save this code to app.py and run it. If everything works out, the image_to_upload.jpg should now be uploaded on your Instagram account as a story.

Warning

If you decide to automate interactions with Instagram, make sure that you don’t log in with each new request. That will get your account flagged. Instead, save the authentication tokens and continue using those for any subsequent requests. Last I remember, the auth tokens remain valid for 90 days!

Conclusion

That’s it! I hope you find this super short tutorial useful. If you are interested in making more fun stuff similar to this, make sure you buy my book: Practical Python Projects. I will see you in the next article! 👋

Newsletter

×

If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

Morteza Sabihi

Hi Thanks for your greate article. I want to create app that it publish posts and stories to inatagram and everybody can use this app. But problem is that nobody trust app to enter instagram username & password. And i saw there are many platform that using inatagram authentication and they didnt get user instagram username and password directly and user authenticate in instagram. I want to implement this in my aplication but i dont kniw how to do it and i didnt find anything. Can you help me my friend? Thank you

Yasoob Khalid
In reply to Morteza Sabihi

Hi Morteza,

Most of these apps use this https://developers.facebook.com/docs/instagram-api The primary difference is that the Graph API only works for business accounts. It doesn’t work with normal user accounts.

Muhammad Zaheer
In reply to Morteza Sabihi

A.o.a Bro how are you. Fitst of all I wanna thank you because of your python practicals book I got a grip on programming. I have a problem with InstagramAPI . It gives me the following output. Fail to import moviepy. Need only for Video upload. Request return 405 error! {‘message’: ‘’, ‘status’: ‘fail’} Request return 404 error! Login success!

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊

OK