Skip to main content

Powerful Instagram Private API โ€” anti-detection, async, Pydantic models

Project description

๐Ÿ“ธ InstaHarvest v2

Powerful Instagram Private API โ€” async, anti-detection, Pydantic models, AI Agent

PyPI Python License Modules Async Tests Coverage Docs

32 sync + 32 async modules โ€ข 230+ functions โ€ข Pydantic models โ€ข AI Agent โ€ข CI/CD โ€ข 475 tests passed

๐Ÿ“– Documentation: mpython77.github.io/instaharvest_v2


Installation

pip install instaharvest-v2

With extras:

pip install instaharvest-v2[dev]      # pytest, pytest-cov, pytest-asyncio
pip install instaharvest-v2[agent]    # AI providers (Gemini, OpenAI, Claude)
pip install instaharvest-v2[web]      # FastAPI web playground
pip install instaharvest-v2[all]      # everything

Quick Start

With cookies (.env file)

from instaharvest_v2 import Instagram

ig = Instagram.from_env(".env")
user = ig.users.get_by_username("cristiano")
print(user.username)        # cristiano
print(user.followers)       # 650000000
print(user["bio"])           # dict-like access works too

With login

ig = Instagram()
ig.login("username", "password")
ig.auth.save_session("session.json")   # save for next time

Load saved session

ig = Instagram()
ig.auth.load_session("session.json")   # no re-login needed

Async mode (50x faster for bulk)

import asyncio
from instaharvest_v2 import AsyncInstagram

async def main():
    async with AsyncInstagram.from_env(mode="fast") as ig:
        # Parallel โ€” all at once!
        tasks = [ig.users.get_by_username(u) for u in usernames]
        profiles = await asyncio.gather(*tasks)

asyncio.run(main())

Challenge auto-resolver

ig = Instagram(
    challenge_callback=lambda ctx: input(f"Code sent to {ctx.contact_point}: ")
)
# Email/SMS challenges resolved automatically!

Anonymous (no login)

ig = Instagram.anonymous()
profile = ig.public.get_profile("cristiano")
posts = ig.public.get_posts("cristiano", max_count=12)

๐ŸŽฌ Reel / Post Scraping (no login, no cookies!)

ig = Instagram()

# By shortcode (from URL: instagram.com/reel/ABC123/)
post = ig.public.get_post_by_shortcode("ABC123")
print(post["likes"])         # 69603
print(post["video_url"])     # full video download URL
print(post["audio"])         # {'title': 'Original audio', 'artist': '...'}

# By full URL
post = ig.public.get_post_by_url("https://instagram.com/reel/ABC123/")

# Works for ALL types: Reels, Photos, Carousels, Videos
# Returns: video_url, display_url, likes, views, comments, audio, owner, carousel_media

๐Ÿค– AI Agent

from instaharvest_v2 import Instagram
from instaharvest_v2.agent import InstaAgent, Permission

ig = Instagram.from_env(".env")
agent = InstaAgent(
    ig=ig,
    provider="gemini",        # 13 AI providers
    permission=Permission.FULL_ACCESS,
    memory=True,
)

agent.ask("Get @cristiano's last 10 posts and save to CSV")
agent.ask("Compare @nike and @adidas engagement rates")
agent.ask("Find the best posting time for my account")

.env Format

SESSION_ID=your_session_id
CSRF_TOKEN=your_csrf_token
DS_USER_ID=your_user_id
MID=optional
IG_DID=optional
DATR=optional
USER_AGENT=optional

API Reference

๐Ÿ‘ค Users

user = ig.users.get_by_username("cristiano")   # โ†’ User model
user = ig.users.get_by_id(123456789)           # โ†’ User model
users = ig.users.search("cristiano")           # โ†’ List[UserShort]
profile = ig.users.get_full_profile("cristiano")  # merged User
bio = ig.users.parse_bio(user)                 # โ†’ BioParsed

๐Ÿ“ท Media

media = ig.media.get_info(media_pk)            # โ†’ Media model
media = ig.media.get_by_shortcode("ABC123")    # โ†’ Media model
likers = ig.media.get_likers(media_pk)         # โ†’ List[UserShort]
comments = ig.media.get_comments_parsed(pk)    # โ†’ List[Comment]

ig.media.like(media_pk)
ig.media.comment(media_pk, "Nice! ๐Ÿ”ฅ")
ig.media.save(media_pk)
ig.media.edit_caption(media_pk, "New caption")
ig.media.pin_comment(media_pk, comment_pk)

๐Ÿ“ฐ Feed

ig.feed.get_user_feed(user_pk)
posts = ig.feed.get_all_posts(user_pk, max_posts=100)
ig.feed.get_liked()
ig.feed.get_saved()
ig.feed.get_tag_feed("fashion")
ig.feed.get_location_feed(location_pk)
ig.feed.get_timeline()
ig.feed.get_reels_feed()

๐Ÿ“– Stories

ig.stories.get_tray()
ig.stories.get_user_stories(user_pk)
ig.stories.mark_seen(story_pks)
ig.stories.get_highlights_tray(user_pk)
ig.stories.create_highlight("My Highlight", story_pks)
ig.stories.react_to_story(story_pk, "๐Ÿ”ฅ")

๐Ÿค Friendships

ig.friendships.follow(user_pk)
ig.friendships.unfollow(user_pk)
followers = ig.friendships.get_all_followers(user_pk)  # โ†’ List[UserShort]
following = ig.friendships.get_all_following(user_pk)  # โ†’ List[UserShort]
ig.friendships.block(user_pk)
ig.friendships.remove_follower(user_pk)
ig.friendships.mute(user_pk)
ig.friendships.restrict(user_pk)
ig.friendships.add_close_friend(user_pk)
ig.friendships.get_mutual_followers(user_pk)

๐Ÿ’ฌ Direct Messages

ig.direct.get_inbox()
ig.direct.send_text(thread_id, "Hello!")
ig.direct.send_media(thread_id, media_pk)
ig.direct.create_thread([user_pk1, user_pk2])
ig.direct.send_link(thread_id, "https://example.com")
ig.direct.send_reaction(thread_id, item_id, "โค๏ธ")

๐Ÿ“ค Upload

ig.upload.post_photo("photo.jpg", caption="My post")
ig.upload.post_video("video.mp4", caption="My reel")
ig.upload.post_story_photo("story.jpg")
ig.upload.post_reel("reel.mp4", caption="Trending")
ig.upload.post_carousel(["img1.jpg", "img2.jpg"], caption="Album")
ig.upload.delete_media(media_pk)

๐Ÿ“ฅ Download

ig.download.download_media(media_pk)
ig.download.download_stories(user_pk)
ig.download.download_highlights(user_pk)
ig.download.download_profile_pic(username="cristiano")
ig.download.download_user_posts(user_pk, max_posts=50)
ig.download.download_by_url("https://instagram.com/p/ABC123/")

๐Ÿ” Search

ig.search.top_search("query")
users = ig.search.search_users("cristiano")    # โ†’ List[UserShort]
ig.search.search_hashtags("fashion")
ig.search.search_places("New York")

โš™๏ธ Account

ig.account.get_current_user()
ig.account.edit_profile(full_name="New Name")
ig.account.set_private()
ig.account.set_public()
ig.account.get_blocked_users()
ig.account.get_login_activity()

๐Ÿ” Auth

ig.login("username", "password")
ig.auth.save_session("session.json")
ig.auth.load_session("session.json")
ig.auth.validate_session()
ig.auth.logout()

More Core Modules

ig.hashtags.get_info("fashion")        # Hashtag info
ig.insights.get_account_insights()     # Analytics
ig.notifications.get_activity_feed()   # Notifications
ig.graphql.get_followers(user_pk)      # GraphQL queries
ig.location.search("New York")         # Location search
ig.collections.get_list()              # Saved collections

๐Ÿ› ๏ธ Advanced Tools

๐Ÿ“Š Analytics

report = ig.analytics.engagement_rate("cristiano")
times = ig.analytics.best_posting_times("nike")
analysis = ig.analytics.content_analysis("adidas")
summary = ig.analytics.profile_summary("messi")
result = ig.analytics.compare(["nike", "adidas", "puma"])

๐Ÿ“ค Export (CSV / JSON)

ig.export.followers_to_csv("nike", "followers.csv", max_count=5000)
ig.export.following_to_csv("nike", "following.csv")
ig.export.post_likers("media_pk", "likers.csv")
ig.export.to_json("cristiano", "profile.json", include_posts=True)

๐ŸŒฑ Growth Engine

ig.growth.follow_users_of("competitor", count=20)
ig.growth.unfollow_non_followers(max_count=50)
non_followers = ig.growth.get_non_followers()
fans = ig.growth.get_fans()
ig.growth.add_whitelist(["friend1", "friend2"])

๐Ÿค– Automation

ig.automation.comment_on_hashtag("fashion", templates=["Nice! ๐Ÿ”ฅ", "Love it! โค๏ธ"])
ig.automation.auto_like_feed(count=20)
ig.automation.auto_like_hashtag("travel", count=30)
ig.automation.watch_stories("target_user")

๐Ÿ“… Scheduler

ig.scheduler.post_at("2026-03-01 09:00", photo="post.jpg", caption="Scheduled!")
ig.scheduler.story_at("2026-03-01 12:00", photo="story.jpg")
ig.scheduler.reel_at("2026-03-01 18:00", video="reel.mp4", caption="Reel time!")
ig.scheduler.start()  # Background worker

๐Ÿ‘๏ธ Account Monitor

watcher = ig.monitor.watch("cristiano")
watcher.on_new_post(lambda data: print("New post!"))
watcher.on_follower_change(lambda old, new: print(f"{old} โ†’ {new}"))
watcher.on_bio_change(lambda old, new: print(f"Bio changed!"))
ig.monitor.start(interval=300)  # Check every 5 min

๐Ÿ“ฅ Bulk Download

ig.bulk_download.all_posts("cristiano", output_dir="./downloads")
ig.bulk_download.all_stories("cristiano", output_dir="./downloads")
ig.bulk_download.everything("cristiano", output_dir="./downloads")

๐Ÿ”ฌ Hashtag Research

analysis = ig.hashtag_research.analyze("python")
related = ig.hashtag_research.related("python", count=30)
suggestions = ig.hashtag_research.suggest("coding", count=20, mix="balanced")
comparison = ig.hashtag_research.compare(["python", "javascript", "rust"])

๐Ÿ—ƒ๏ธ Data Pipeline (SQLite / JSONL)

ig.pipeline.to_sqlite("cristiano", "data.db", include_posts=True, include_followers=True)
ig.pipeline.to_jsonl("cristiano", "data.jsonl", max_posts=100)

๐Ÿง  AI Hashtag Suggester

result = ig.ai_suggest.hashtags_from_caption("Beautiful sunset at the beach")
profile_tags = ig.ai_suggest.hashtags_for_profile("cristiano")
captions = ig.ai_suggest.caption_ideas("travel", style="casual", count=5)

๐Ÿ‘ฅ Audience Finder

lookalike = ig.audience.find_lookalike("competitor", count=50)
overlap = ig.audience.overlap("account_a", "account_b")
insights = ig.audience.insights("my_account")

๐Ÿ’ฌ Comment Manager

comments = ig.comment_manager.get_comments(media_pk, sort="top")
ig.comment_manager.auto_reply(media_pk, keyword="price?", reply="DM us!")
ig.comment_manager.delete_spam(media_pk)
sentiment = ig.comment_manager.sentiment(media_pk)

๐Ÿงช A/B Testing

test = ig.ab_test.create("Caption Test", variants={
    "A": {"caption": "Short and sweet"},
    "B": {"caption": "Long detailed caption with hashtags #test"},
})
ig.ab_test.record(test["id"], "A", likes=100, comments=20)
ig.ab_test.record(test["id"], "B", likes=150, comments=30)
result = ig.ab_test.results(test["id"])
print(f"Winner: {result['winner']}")

Pydantic Models

All API methods return typed Pydantic models with dict-like access:

user = ig.users.get_by_username("cristiano")

# Typed attributes
print(user.username)          # "cristiano"
print(user.follower_count)    # 650000000

# Dict-like access (backward compatible)
print(user["username"])       # "cristiano"

# Convert to dict
data = user.to_dict()

# Extra fields preserved (API changes won't break)
print(user.some_new_field)    # works!

Available models: User, UserShort, Media, Comment, Story, Highlight, DirectThread, DirectMessage, Location, Hashtag, Notification


Features

Feature Description
๐Ÿ›ก๏ธ Anti-detection Browser fingerprint rotation, Gaussian delays, escalation
๐Ÿ”„ Multi-account Automatic session rotation
๐ŸŒ Proxy support SOCKS5/HTTP, weighted rotation, health checking
โฑ๏ธ Rate limiting Per-endpoint sliding window limits
๐Ÿ” Login NaCl encrypted password, 2FA, checkpoint handling
๐Ÿ’พ Session persistence Save/load sessions, no re-login needed
๐Ÿงฉ Challenge handler Auto-resolve email/SMS/consent challenges
โšก Full async parity 32 sync + 32 async modules โ€” complete feature match
๐Ÿ“ฆ Pydantic models Typed returns, dict-like access, backward compatible
๐Ÿค– AI Agent 13 providers, natural language control, memory, webhooks
๐Ÿ“Š 12 Advanced tools Analytics, Export, Growth, Automation, Monitor, Pipeline, etc.
โœ… CI/CD GitHub Actions โ€” lint, test (3 Python versions), security, build
๐Ÿงช 475 tests 35% coverage, pytest-cov, comprehensive unit & integration tests

Speed Modes (Async)

# ๐Ÿข SAFE  โ€” 5 concurrent, human delays
async with AsyncInstagram.from_env(mode="safe") as ig: ...

# โšก FAST  โ€” 15 concurrent, moderate delays
async with AsyncInstagram.from_env(mode="fast") as ig: ...

# ๐Ÿš€ TURBO โ€” 50 concurrent, minimal delays (proxy required)
async with AsyncInstagram.from_env(mode="turbo") as ig: ...

Testing & Quality

# Run all tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=instaharvest_v2 --cov-report=term-missing

Current status:

  • โœ… 475 tests passed
  • ๐Ÿ“Š 35.3% code coverage

Project Structure

instaharvest_v2/
โ”œโ”€โ”€ instagram.py           # Main class (sync)
โ”œโ”€โ”€ async_instagram.py     # Main class (async)
โ”œโ”€โ”€ client.py              # HTTP client (curl_cffi)
โ”œโ”€โ”€ async_client.py        # Async HTTP client
โ”œโ”€โ”€ challenge.py           # Challenge auto-resolver
โ”œโ”€โ”€ anti_detect.py         # Anti-detection system
โ”œโ”€โ”€ session_manager.py     # Session auto-refresh
โ”œโ”€โ”€ proxy_manager.py       # Proxy rotation
โ”œโ”€โ”€ rate_limiter.py        # Rate limiting
โ”œโ”€โ”€ multi_account.py       # Multi-account manager
โ”œโ”€โ”€ exceptions.py          # Error classes
โ”œโ”€โ”€ models/                # Pydantic models
โ”‚   โ”œโ”€โ”€ user.py            # User, UserShort, BioParsed
โ”‚   โ”œโ”€โ”€ media.py           # Media, Caption
โ”‚   โ”œโ”€โ”€ comment.py         # Comment
โ”‚   โ”œโ”€โ”€ story.py           # Story, Highlight
โ”‚   โ”œโ”€โ”€ direct.py          # DirectThread, DirectMessage
โ”‚   โ”œโ”€โ”€ location.py        # Location
โ”‚   โ”œโ”€โ”€ notification.py    # Notification models
โ”‚   โ””โ”€โ”€ public_data.py     # PublicProfile, PublicPost
โ”œโ”€โ”€ api/                   # API modules (33 sync + 33 async)
โ”‚   โ”œโ”€โ”€ users.py           # User profiles
โ”‚   โ”œโ”€โ”€ media.py           # Post interactions
โ”‚   โ”œโ”€โ”€ feed.py            # User feeds
โ”‚   โ”œโ”€โ”€ friendships.py     # Follow/unfollow
โ”‚   โ”œโ”€โ”€ search.py          # Search
โ”‚   โ”œโ”€โ”€ stories.py         # Stories & highlights
โ”‚   โ”œโ”€โ”€ direct.py          # Direct messages
โ”‚   โ”œโ”€โ”€ upload.py          # Photo/video upload
โ”‚   โ”œโ”€โ”€ download.py        # Media downloads
โ”‚   โ”œโ”€โ”€ auth.py            # Login/logout
โ”‚   โ”œโ”€โ”€ analytics.py       # Engagement analytics
โ”‚   โ”œโ”€โ”€ export.py          # CSV/JSON export
โ”‚   โ”œโ”€โ”€ growth.py          # Smart follow/unfollow
โ”‚   โ”œโ”€โ”€ automation.py      # Bot framework
โ”‚   โ”œโ”€โ”€ scheduler.py       # Post scheduling
โ”‚   โ”œโ”€โ”€ monitor.py         # Account monitoring
โ”‚   โ”œโ”€โ”€ bulk_download.py   # Bulk media download
โ”‚   โ”œโ”€โ”€ hashtag_research.py # Hashtag analysis
โ”‚   โ”œโ”€โ”€ pipeline.py        # Data pipeline (SQLite/JSONL)
โ”‚   โ”œโ”€โ”€ ai_suggest.py      # AI hashtag/caption
โ”‚   โ”œโ”€โ”€ audience.py        # Lookalike audience
โ”‚   โ”œโ”€โ”€ comment_manager.py # Comment management
โ”‚   โ”œโ”€โ”€ ab_test.py         # A/B testing
โ”‚   โ”œโ”€โ”€ public_data.py     # Public data analytics
โ”‚   โ”œโ”€โ”€ discover.py        # Similar user discovery
โ”‚   โ””โ”€โ”€ async_*.py         # All 33 async mirrors
โ”œโ”€โ”€ agent/                 # AI Agent system
โ”‚   โ”œโ”€โ”€ core.py            # InstaAgent main class
โ”‚   โ”œโ”€โ”€ providers/         # AI providers (Gemini, OpenAI, Claude, etc.)
โ”‚   โ”œโ”€โ”€ tools.py           # 10 built-in tools
โ”‚   โ”œโ”€โ”€ memory.py          # Conversation memory
โ”‚   โ”œโ”€โ”€ templates.py       # 10 task templates
โ”‚   โ”œโ”€โ”€ tui.py             # Terminal UI (Rich)
โ”‚   โ”œโ”€โ”€ web.py             # Web UI (FastAPI)
โ”‚   โ”œโ”€โ”€ webhook.py         # Notifications (Telegram, Discord)
โ”‚   โ”œโ”€โ”€ cost_tracker.py    # Token usage & pricing
โ”‚   โ””โ”€โ”€ vision.py          # Multimodal image analysis
tests/                     # 475 tests
docs/                      # MkDocs documentation
.github/workflows/         # CI/CD pipeline

CI/CD

GitHub Actions pipeline (.github/workflows/ci.yml):

Job Description
Lint flake8 + mypy
Test Python 3.10, 3.11, 3.12 + coverage
Security bandit + safety
Build Package + twine check

โš ๏ธ Legal Disclaimer

This library is an unofficial, third-party tool designed for educational and research purposes only. It is not affiliated with, authorized, maintained, sponsored, or endorsed by Instagram or Meta Platforms, Inc.

By using this software, you agree that:

  1. You are solely responsible for any actions you take using this library.
  2. The authors and maintainers are NOT responsible or liable for any bans, blocks, suspensions, or other penalties applied to your Instagram accounts or IP addresses.
  3. You will use this tool in compliance with Instagram's Terms of Service and all applicable local laws.

Use at your own risk. The software is provided "AS IS", without warranty of any kind.


License

MIT License. See LICENSE for details.

Project details


Download files

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

Source Distribution

instaharvest_v2-1.0.8.tar.gz (427.4 kB view details)

Uploaded Source

Built Distribution

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

instaharvest_v2-1.0.8-py3-none-any.whl (462.5 kB view details)

Uploaded Python 3

File details

Details for the file instaharvest_v2-1.0.8.tar.gz.

File metadata

  • Download URL: instaharvest_v2-1.0.8.tar.gz
  • Upload date:
  • Size: 427.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for instaharvest_v2-1.0.8.tar.gz
Algorithm Hash digest
SHA256 888d0dcdcf8881e45548c4420538afadaaea2e7ca09f840dd139821c452ddc9d
MD5 447b6550da4c640d5a566cfbfb7a6d26
BLAKE2b-256 16288cf71ba6c43a7f7050c04a07bae9fdef4d23fb51b49396050233fe0163dd

See more details on using hashes here.

File details

Details for the file instaharvest_v2-1.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for instaharvest_v2-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 025153475b167f4a966422b714133b9d75d52bb542b1672874ea179c574cbf1e
MD5 e6c697da60b1406f6542d08139f2fa29
BLAKE2b-256 89956d63b17903fbc61745ed8d00b0a256e675ef1b34f95b5e13d8ccad7ae3bc

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