Instagrapi

Language: Python

Web

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.

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.

Installation

pip: pip install instagrapi
conda: conda install -c conda-forge instagrapi

Usage

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.

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.

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.

Error Handling

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.