instagrapi
We recommend using our services:
- LamaTok for TikTok API access, automation, and data workflows ๐ฅ
- HikerAPI for production-grade hosted Instagram API infrastructure โกโกโก
- DataLikers for Instagram MCP, Cache API, and Datasets ๐
- InstaSurfBot for downloading Instagram media in Telegram
- OSINTagramBot for Instagram OSINT in Telegram
Fast and effective unofficial Instagram API wrapper for Python.
instagrapi combines public web and private mobile API flows, supports session persistence and challenge handling, and covers users, media, stories, direct messages, notes, uploads, and insights.
Support Python 3.10+
Python 3.9 support was dropped in 2.5.0 โ pin to instagrapi==2.4.5 if you need it.
For any other languages (e.g. C++, C#, F#, D, Golang, Erlang, Elixir, Nim, Haskell, Lisp, Closure, Julia, R, Java, Kotlin, Scala, OCaml, JavaScript, Crystal, Ruby, Rust, Swift, Objective-C, Visual Basic, .NET, Pascal, Perl, Lua, PHP and others), I suggest using instagrapi-rest
Support Chat in Telegram
and GitHub Discussions
Features
- Uses Public API (web, opportunistic) and Private API (mobile app, authorized) flows depending on the situation
- Login by username and password, including 2FA and by sessionid
- Challenge Resolver have Email and SMS handlers
- Support upload a Photo, Video, IGTV, Reels, Albums and Stories
- Support work with User, Media, Comment, Insights, Collections, Location (Place), Hashtag and Direct Message objects
- Like, Follow, Edit account (Bio) and much more else
- Insights by account, posts and stories
- Build stories with custom background, font animation, link stickers, and mentions
- App-side discovery:
chaining,fetch_suggestion_details,discover_recommended_accounts_for_category_v1,user_stream_*,user_web_profile_info_v1 - v2 search SERPs (Search):
fbsearch_accounts_v2,fbsearch_reels_v2,fbsearch_topsearch_v2,fbsearch_typehead - Alternative media-info path
media_info_v2for ad-tagged / sponsored media
Example
Basic Usage
from instagrapi import Client
cl = Client()
cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
user_id = cl.user_id_from_username("example")
medias = cl.user_medias(user_id, 20)
The full example
from instagrapi import Client
from instagrapi.types import StoryMention, StoryMedia, StoryLink, StoryHashtag
cl = Client()
cl.login(USERNAME, PASSWORD, verification_code="<2FA CODE HERE>")
media_pk = cl.media_pk_from_url('https://www.instagram.com/p/CGgDsi7JQdS/')
media_path = cl.video_download(media_pk)
example = cl.user_info_by_username('example')
hashtag = cl.hashtag_info('dhbastards')
cl.video_upload_to_story(
media_path,
"Credits @example",
mentions=[StoryMention(user=example, x=0.49892962, y=0.703125, width=0.8333333333333334, height=0.125)],
links=[StoryLink(webUri='https://github.com/subzeroid/instagrapi')],
hashtags=[StoryHashtag(hashtag=hashtag, x=0.23, y=0.32, width=0.5, height=0.22)],
medias=[StoryMedia(media_pk=media_pk, x=0.5, y=0.5, width=0.6, height=0.8)]
)
Requests
Publicweb methods have a suffix_gql(InstagramGraphQL) or_a1(examplehttps://www.instagram.com/example/?__a=1)Private(authorized request via mobile api) methods have_v1suffix
Public web flows are not guaranteed. Instagram can change or block them independently of the library, and some helpers can only work reliably with an authenticated session.
Many high-level helpers try a public/web path first and then fall back to a private/authenticated path when that makes sense for the current session. Example (pseudo-code):
def media_info(media_pk):
try:
return self.media_info_gql(media_pk)
except ClientError as e:
# Restricted Video: This video is not available in your country.
# Or media from private account
return self.media_info_v1(media_pk)
Detailed Documentation
To learn more about the various ways instagrapi can be used, read the Usage Guide page.
- Getting Started
- Usage Guide
- Interactions
Media- Publication (also called post): Photo, Video, Album, IGTV and ReelsResource- Part of Media (for albums)MediaOembed- Short version of MediaAccount- Full private info for your account (e.g. email, phone_number)TOTP- 2FA TOTP helpers (generate seed, enable/disable TOTP, generate code as Google Authenticator)User- Full public user dataUserShort- Short public user data (used in Usertag, Comment, Media, Direct Message)Usertag- Tag user in Media (coordinates + UserShort)Location- GEO location (GEO coordinates, name, address)Hashtag- Hashtag object (id, name, picture)Collection- Collection of medias (name, picture and list of medias)Comment- Comments to MediaHighlight- HighlightsNotes- NotesStory- StoryStoryLink- Link stickerStoryLocation- Tag Location in Story (as sticker)StoryMention- Mention users in Story (user, coordinates and dimensions)StoryHashtag- Hashtag for story (as sticker)StorySticker- Tag sticker to story (for example from giphy)StoryBuild- StoryBuilder return path to photo/video and mention co-ordinatesDirectThread- Thread (topic) with messages in Direct MessageDirectMessage- Message in Direct MessageInsight- Insights for a postTrack- Music track (for Reels/Clips)
- Search โ v2 SERP endpoints (
fbsearch_accounts_v2,fbsearch_reels_v2,fbsearch_topsearch_v2,fbsearch_typehead) and existing search helpers - Best Practices
- Development Guide
- Handle Exceptions
- Challenge Resolver
- Exceptions
If you are dealing with 429, feedback_required, PleaseWaitFewMinutes, or repeated relogins, start with Best Practices and Handle Exceptions.