⚠️ Telegram support group moved to aiograpi_support — the previous @instagrapi group has been restricted by Meta and is no longer maintained.
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 the main automation primitives for users, media, stories, direct messages, notes, locations, comments, insights, and uploads.
Private API automation is fragile in production because account trust, proxies, device state, challenges, and rate limits can change independently of the library.
For account-owned business workflows, prefer official Instagram APIs where they cover your use case.
For production private API infrastructure, a hosted provider such as HikerAPI may be a better fit than maintaining accounts, proxies, and challenge handling yourself.
The instagrapi project is best suited for testing, research, and controlled internal automation.
Pillow 13 provides upstream security fixes, including CVE-2026-96778, and requires Python 3.11 or newer. instagrapi==3.1.0 is the last release compatible with Python 3.10; its Pillow dependency range excludes these fixes. Upgrade to Python 3.11+ before installing instagrapi 3.2.0 or newer.
Installation
pip install instagrapi
Private mobile requests use HTTP/2 through curl_cffi, included in the standard installation. Client() needs no transport argument, and login() uses CAA directly. The previous login remains available as login_legacy(). See the login migration guide.
Optional public web TLS impersonation is available as an extra:
pip install "instagrapi[curl]"
For public web endpoints that are sensitive to browser TLS fingerprints:
Private mobile API requests use curl and HTTP/2 by default. See the private HTTP/2 transport guide for saved-session setup, requirements and limitations.
TLS certificate verification is enabled by default. For a trusted debugging MITM proxy, prefer Client(tls_verify="/path/to/proxy-ca.pem"); use Client(tls_verify=False) only for temporary local debugging because it allows session interception.
If your project uses uv, you can add the package with:
uv add instagrapi
Or install it into the active virtual environment:
uv pip install instagrapi
Video uploads can use a built-in MP4 metadata parser when you provide thumbnail=.... Automatic thumbnail generation, StoryBuilder, and video/audio composition still need the optional video dependencies, MoviePy 2.2.1, and executable ffmpeg:
MoviePy 2.2.1 currently declares Pillow<12, but instagrapi requires Pillow>=13.0.0,<14 for upstream security fixes; install MoviePy with --no-deps to retain that dependency range. If your project imports MoviePy directly, migrate any MoviePy 1.x code from moviepy.editor, set_*, resize, and subclip APIs to the MoviePy 2.x API before upgrading.
Practical scripts live in examples/README.md. They cover session login, public lookups, media
downloads, feed uploads, Reels and Trial Reels, story uploads, Direct messages, proxies, challenge handling, and optional
curl-backed public transport.
Session Persistence
from instagrapi import Client
cl = Client()
cl.login(USERNAME, PASSWORD)
cl.dump_settings("session.json")
# reload later; the saved session is validated before reuse
cl = Client()
cl.load_settings("session.json")
cl.login(USERNAME, PASSWORD)
cl.dump_settings("session.json")
login() reuses a valid saved session. If Instagram rejects that session with
login_required, instagrapi clears the stale authorization and logs in again
with the supplied credentials. Dump the settings after login so a refreshed
session is persisted.
If you want more explicit control over the loaded session object:
from instagrapi import Client
cl = Client()
cl.login_by_sessionid("<your_sessionid>")
login_by_sessionid() is best treated as a lightweight compatibility path. For long-lived automation, prefer the normal login() -> dump_settings() -> load_settings()/set_settings() session flow.
If a browser/web sessionid returns login_required or logs the browser out, Instagram rejected that session for the private mobile API. Use a stable password login once, save settings with dump_settings(), and reuse those settings instead of repeatedly importing browser cookies.
Typical Tasks
List and download another user's posts
from instagrapi import Client
cl = Client()
cl.login(USERNAME, PASSWORD)
target_id = cl.user_id_from_username("target_user")
posts = cl.user_medias(target_id, amount=10)
for media in posts:
# download photos to the current folder
cl.photo_download(media.pk)
from instagrapi import Client
cl = Client()
cl.login(USERNAME, PASSWORD)
target_id = cl.user_id_from_username("target_user")
sent = cl.direct_send("Hello from instagrapi", user_ids=[target_id])
print("sent", sent.id)
threads = cl.direct_threads(amount=5)
for thread in threads:
last_message = thread.messages[0] if thread.messages else None
print(thread.id, thread.thread_title, last_message.text if last_message else "")
Work with Direct messages over Realtime MQTT
Realtime MQTT support is experimental. It opens Instagram's private MQTToT
connection after login, emits live callbacks, and uses the same Client.proxy
settings as HTTP requests. The realtime client can receive Direct message sync
events and publish lightweight Direct actions such as text, reactions, typing,
and seen state. Use the regular direct_* methods for media sends and full
thread management.
See the full Realtime MQTT guide for lower-level
subscriptions and event details.
Receive Direct push notifications over FBNS
FBNS uses Instagram's separate push MQTT connection and registers an Android
push token for the logged-in session. It is useful when you need push payloads
such as Direct notification callbacks.
Alternative media-info path (media_info_v2) for ad-tagged / sponsored media that the canonical endpoint refuses
Experimental Realtime MQTT helpers for live events, Direct message sync, lightweight Direct actions, and FBNS push callbacks
Anonymous/public web paths are best treated as opportunistic rather than guaranteed. Instagram can change or restrict them independently of the library, so production-grade workflows should prefer authenticated sessions.