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 instagrapiGetting started
The smallest useful thing you can do with it, and what each part means.
from instagrapi import Client
client = Client()
client.login('your_username', 'your_password')followers = client.user_followers('user_id')
for uid, user in followers.items():
print(user.username)client.photo_upload('image.jpg', 'Caption for the photo')Advanced usage
Where the library earns its place over a simpler alternative.
stories = client.user_stories('user_id')
for story in stories:
client.story_download(story.pk, f'{story.pk}.jpg')client.photo_story('story_image.jpg', 'Story caption')medias = client.user_medias('user_id', 10)
for media in medias:
print(media.caption_text)client.user_follow('user_id')client.user_unfollow('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.
