Skip to main content

Fast and effective Instagram Private API wrapper

Project description

instagrapi

⚠️ Telegram support group moved to aiograpi_support — the previous @instagrapi group has been restricted by Meta and is no longer maintained.

PyPI Python License Package Docs

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.

aiograpi - Asynchronous Python library for Instagram Private API

Support Python 3.10+

Python 3.9 support was dropped in 2.5.0. Upstream security patches for Pillow 12.x and pytest 9.x are not backported to Python 3.9, leaving conditional pins permanently exposed to known CVEs. Users who need Python 3.9 should pin to instagrapi==2.4.5.

Installation

pip install instagrapi

Optional public web TLS impersonation support is available as an extra:

pip install "instagrapi[curl]"

Use it only for public web endpoints that are sensitive to browser TLS fingerprints:

cl = Client(public_transport="curl", public_transport_impersonate="chrome136")

See the public transport guide for live comparison results and caveats.

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:

pip install "instagrapi[video]"
pip install --no-deps "moviepy==2.2.1"

MoviePy 2.2.1 currently declares Pillow<12, but instagrapi keeps Pillow>=12.2.0 for security fixes; the --no-deps install keeps the safe Pillow version. 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.

Android users should see Pydroid and ffmpeg and Termux.

Quick Start

from instagrapi import Client

cl = Client()
cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)

user_id = cl.user_id_from_username(ACCOUNT_USERNAME)
medias = cl.user_medias(user_id, 20)

Runnable Examples

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 without entering credentials again
cl = Client()
cl.load_settings("session.json")
cl.login(USERNAME, PASSWORD)

If you want more explicit control over the loaded session object:

from instagrapi import Client

cl = Client()
cl.set_settings(cl.load_settings("session.json"))
cl.login(USERNAME, PASSWORD)

Login using a sessionid

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)

See examples/session_login.py for a standalone script demonstrating these login methods.

Search locations by name or exact pk

from instagrapi import Client

cl = Client()
cl.login(USERNAME, PASSWORD)

places = cl.location_search_name("Times Square")
place = places[0]
same_place = cl.location_search_pk(place.pk)

print(same_place.name, same_place.pk)

Work with Notes

from instagrapi import Client

cl = Client()
cl.login(USERNAME, PASSWORD)

notes = cl.get_notes()
print(cl.get_note_text_by_user(notes, "instagram"))

note = cl.create_note("Hello from instagrapi", audience=0)
cl.delete_note(note.id)

Features

  • Uses Web API and Mobile API flows where available
  • Supports login by password, 2FA, sessionid, and Bloks 2FA fallback/helpers for newer verification flows
  • Includes email/SMS-based challenge resolver hooks
  • Uploads and downloads photos, videos, albums, IGTV, reels, and stories
  • Works with users, media, comments, locations, hashtags, collections, notes, direct messages, and insights
  • Supports story building with mentions, hashtags, link stickers, and media stickers
  • Includes helpers for current location search and notes flows
  • Supports mobile follower sorting with date_followed_latest and date_followed_earliest
  • App-side discovery surfaces: chaining, fetch_suggestion_details, discover_recommended_accounts_for_category_v1, user_stream_*, user_web_profile_info_v1
  • v2 search SERPs: fbsearch_accounts_v2, fbsearch_reels_v2, fbsearch_topsearch_v2, fbsearch_typehead
  • Alternative media-info path (media_info_v2) for ad-tagged / sponsored media that the canonical endpoint refuses

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.

Documentation And Support

API reference and full usage guide live at subzeroid.github.io/instagrapi:

For other languages, consider instagrapi-rest. For async Python, see aiograpi.

Tutorials

Hands-on guides for real instagrapi work — login flows, sessions, proxies, scraping, posting, error handling — live at instagrapi.com/guides:

Comparing instagrapi to other tools:

Additional 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)
subzeroid = cl.user_info_by_username('subzeroid')
hashtag = cl.hashtag_info('dhbastards')

cl.video_upload_to_story(
    media_path,
    "Credits @subzeroid",
    mentions=[StoryMention(user=subzeroid, 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)]
)

Related Projects

If you need async Python, use aiograpi.

For other languages, see instagrapi-rest. For hosted production Instagram API infrastructure, see HikerAPI.

Related services:

  • Cloqly for premium rotating proxies and stable automation traffic
  • DataLikers for Instagram MCP, Cache API, and datasets
  • LamaTok for TikTok API access, automation, and data workflows
  • InstaSurfBot for downloading Instagram media in Telegram
  • OSINTagramBot for Instagram OSINT in Telegram

HikerAPI Affiliate Program

Refer users to HikerAPI and earn a percentage of their API spending:

Plan Commission
Start trial plan ($0.02/req) 50%
Standard ($0.001/req) 25%
Business ($0.00069/req) 15%
Ultra ($0.0006/req) 10%

Extras: 2-level referral system, no caps, lifetime attribution

Payouts: USDT / USDC (TRC-20 or ERC-20), minimum 20 USDT, request anytime from the dashboard

Contributing

List of contributors

For local setup, tests, linting, and pull request expectations, see CONTRIBUTING.md and the development guide.

Maintainer release commands:

git tag -a X.Y.Z -m "Release X.Y.Z"
git push origin X.Y.Z
git push codeberg X.Y.Z

The tag-based publish.yml workflow publishes to PyPI via trusted publishing and creates the GitHub release.

License

instagrapi is distributed under the MIT License.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

instagrapi-2.7.6.tar.gz (168.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

instagrapi-2.7.6-py3-none-any.whl (183.2 kB view details)

Uploaded Python 3

File details

Details for the file instagrapi-2.7.6.tar.gz.

File metadata

  • Download URL: instagrapi-2.7.6.tar.gz
  • Upload date:
  • Size: 168.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for instagrapi-2.7.6.tar.gz
Algorithm Hash digest
SHA256 2ae7a79956e9982d0f6c0c01db3c680554c270e805865f90f8474fdf5451799b
MD5 d2f27eb4ed5d85144966b8e638c7acf0
BLAKE2b-256 fdc9a9fd66f045938b50584926bd07ea64c582a732937f4bedab91a04cb472a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for instagrapi-2.7.6.tar.gz:

Publisher: publish.yml on subzeroid/instagrapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file instagrapi-2.7.6-py3-none-any.whl.

File metadata

  • Download URL: instagrapi-2.7.6-py3-none-any.whl
  • Upload date:
  • Size: 183.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for instagrapi-2.7.6-py3-none-any.whl
Algorithm Hash digest
SHA256 de55059817d93ae8d95f80e990ef9c0e9411a0318de0b53880f09f87e6d55c83
MD5 b71fa3d36bc55d6b77435b790d7a32c8
BLAKE2b-256 cd4a93bdc2c1f708c5a80c77a7df4668d45f66a7f84e5e38d13503d3206ef695

See more details on using hashes here.

Provenance

The following attestation bundles were made for instagrapi-2.7.6-py3-none-any.whl:

Publisher: publish.yml on subzeroid/instagrapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page