aiograpi

πŸ”₯ Asynchronous Python library for Instagram Private API 2024

View on GitHub

aiograpi

We recommend using our services:

Package PyPI PyPI - Python Version

Asynchronous Instagram Private API wrapper 2024. Use the most recent version of the API from Instagram, which was obtained using reverse-engineering with Charles Proxy and Proxyman.

Instagram API valid for 27 Feb 2024 (last reverse-engineering check)

Support Python >= 3.10

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

  1. Performs Public API (web, anonymous) or Private API (mobile app, authorized) requests depending on the situation (to avoid Instagram limits)
  2. Login by username and password, including 2FA and by sessionid
  3. Challenge Resolver have Email and SMS handlers
  4. Support upload a Photo, Video, IGTV, Reels, Albums and Stories
  5. Support work with User, Media, Comment, Insights, Collections, Location (Place), Hashtag and Direct Message objects
  6. Like, Follow, Edit account (Bio) and much more else
  7. Insights by account, posts and stories
  8. Build stories with custom background, font animation, swipe up link and mention users
  9. In the next release, account registration and captcha passing will appear

Example

Basic Usage

from aiograpi import Client

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

user_id = await cl.user_id_from_username("example")
medias = await cl.user_medias(user_id, 20)

The full example

from aiograpi import Client
from aiograpi.types import StoryMention, StoryMedia, StoryLink, StoryHashtag

cl = Client()
await cl.login(USERNAME, PASSWORD, verification_code="<2FA CODE HERE>")

media_pk = await cl.media_pk_from_url('https://www.instagram.com/p/CGgDsi7JQdS/')
media_path = await cl.video_download(media_pk)
example = await cl.user_info_by_username('example')
hashtag = await cl.hashtag_info('dhbastards')

await 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/aiograpi')],
    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

The first request to fetch media/user is public (anonymous), if instagram raise exception, then use private (authorized). Example (pseudo-code):

async def media_info(media_pk):
    try:
        return await 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 await self.media_info_v1(media_pk)

Detailed Documentation

To learn more about the various ways aiograpi can be used, read the Usage Guide page.