Skip to content

What it is

Instagrapi is a modern Python library for Instagram automation. It allows you to programmatically interact with Instagram, including posting photos and stories, managing followers, reading feeds, and downloading media.

Instagrapi provides methods for logging in, uploading media, interacting with followers, downloading stories, and analyzing Instagram data. It supports both personal and business accounts and includes features for handling reels, IGTV, and metadata.

Installation

pip install instagrapi

Getting started

The smallest useful thing you can do with it, and what each part means.

Login to Instagram
from instagrapi import Client
client = Client()
client.login('your_username', 'your_password')
Authenticates your Instagram account using username and password.
Get user followers
followers = client.user_followers('user_id')
for uid, user in followers.items():
    print(user.username)
Retrieves and prints the list of followers for a specific user ID.
Upload a photo
client.photo_upload('image.jpg', 'Caption for the photo')
Uploads a photo to your Instagram account with the specified caption.

Advanced usage

Where the library earns its place over a simpler alternative.

Download stories
stories = client.user_stories('user_id')
for story in stories:
    client.story_download(story.pk, f'{story.pk}.jpg')
Downloads all active stories for a user and saves them locally.
Post a story
client.photo_story('story_image.jpg', 'Story caption')
Uploads a photo as a story to your Instagram account.
Get user feed
medias = client.user_medias('user_id', 10)
for media in medias:
    print(media.caption_text)
Retrieves the latest 10 posts from a user and prints the captions.
Follow a user
client.user_follow('user_id')
Follows the specified user by user ID.
Unfollow a user
client.user_unfollow('user_id')
Unfollows the specified user by user ID.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

ClientLoginRequired
Ensure you are logged in before making requests or refresh the session.
RateLimitError
Avoid sending too many requests in a short period; implement delays.
MediaNotFound
Verify the media ID exists and is accessible for the logged-in account.

Best practices

  • Use environment variables to store login credentials securely.
  • Respect Instagram’s terms of service and avoid aggressive automation.
  • Use try/except to handle network errors and account restrictions.
  • Rate-limit requests to prevent temporary account blocks.
  • Keep session files to maintain login and reduce repeated authentication.

Background

Why it exists, and what it was reacting to.

Instagrapi was created to provide an easy-to-use and fast Python interface for Instagram automation. Unlike the official Instagram API, Instagrapi is lightweight, does not require app registration, and supports a wide range of Instagram features, making it popular among developers for personal projects, analytics, and social media automation.