Skip to main content

Fast and effective Instagram Private API wrapper

Project description

instagrapi

Package PyPI PyPI - Python Version Checked with mypy

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.

Support Python >= 3.9

Installation

pip install instagrapi

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)

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.

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 or Mobile API depending on the situation
  • Supports login by password, 2FA, and sessionid
  • 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

Documentation And Support

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

ASCII Logo
░██                         ░██                                                         ░██
                            ░██
░██░████████   ░███████  ░████████  ░██████    ░████████ ░██░████  ░██████   ░████████  ░██
░██░██    ░██ ░██           ░██          ░██  ░██    ░██ ░███           ░██  ░██    ░██ ░██
░██░██    ░██  ░███████     ░██     ░███████  ░██    ░██ ░██       ░███████  ░██    ░██ ░██
░██░██    ░██        ░██    ░██    ░██   ░██  ░██   ░███ ░██      ░██   ░██  ░███   ░██ ░██
░██░██    ░██  ░███████      ░████  ░█████░██  ░█████░██ ░██       ░█████░██ ░██░█████  ░██
                                                     ░██                     ░██
                                               ░███████                      ░██
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)]
)

Documentation

Ecosystem And Hosted Options

If you need async Python, use aiograpi.

If you need hosted infrastructure instead of maintaining accounts, proxies, and challenge handling yourself, consider:

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

To release, you need to call the following commands:

python -m build
twine upload dist/*

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.4.1.tar.gz (118.3 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.4.1-py3-none-any.whl (132.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: instagrapi-2.4.1.tar.gz
  • Upload date:
  • Size: 118.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for instagrapi-2.4.1.tar.gz
Algorithm Hash digest
SHA256 548fb4e66cafba1926ba7ea8c2b75f5863631b0da8c80268fddd05720923e513
MD5 126d2a12ed39e701e44427d2eb18b7df
BLAKE2b-256 8a096fc44beafd63d1412920b2fdf5110c286a711dddbd5f724c544a65790031

See more details on using hashes here.

File details

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

File metadata

  • Download URL: instagrapi-2.4.1-py3-none-any.whl
  • Upload date:
  • Size: 132.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for instagrapi-2.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a4952942a70b97756bf92096f33e02be2bfc0c455ea817712a01e70c165f7cec
MD5 1abf3736e4ca7ca909553c7a66234900
BLAKE2b-256 4e2169648e118545be36c6bbb5e0887dadb057193421012c6bd6eb1bb2f86a6d

See more details on using hashes here.

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