Skip to main content

igapi-rs

PyPI Python License

Instagram 私有 API 的 Python 绑定,基于 Rust + PyO3 构建,高性能、异步优先。

支持 Android / iOS / Web 三平台模拟。

安装

pip install igapi-rs

环境要求

  • Python 3.10+

快速开始

import asyncio
import igapi
from igapi.android import Client

async def main():
    # 创建客户端
    client = Client()

    # 登录
    await client.login("username", "password")

    # 获取用户信息
    user_api = igapi.UserApi(client)
    user = await user_api.info(12345678)
    print(f"用户: @{user.username}")
    print(f"粉丝数: {user.follower_count}")

    # 搜索用户
    results = await user_api.search("instagram")
    for user in results:
        print(f"@{user.username}")

    # 获取用户动态
    feed_api = igapi.FeedApi(client)
    items, next_cursor = await feed_api.user(12345678)
    for media in items:
        print(f"媒体: {media.id}, 点赞: {media.like_count}")

asyncio.run(main())

API 参考

Android Client

from igapi.android import AccountInfo, Client

Client(
    proxy: str | None = None,
    danger_accept_invalid_certs: bool = False,
    http1_only: bool = False,
    account: AccountInfo | None = None,
)

创建 Android 平台客户端。

参数:

  • proxy (可选): 代理 URL(例如 "http://localhost:8080")
  • danger_accept_invalid_certs (可选): 是否接受无效 TLS 证书
  • http1_only (可选): 是否强制 HTTP/1.1
  • account (可选): igapi.android.AccountInfo

方法:

await login(username: str, password: str) -> None

登录 Instagram。

异常:

  • ValueError: 密码错误、用户无效
  • PermissionError: 需要重新登录 / 账号锁定 / 需要同意条款
  • igapi.TwoFactorRequired / igapi.ChallengeRequired: 需要 2FA / 安全验证
  • RuntimeError: 网络或 API 错误

await is_logged_in() -> bool

检查当前是否已登录。

会话与登出(全平台)

  • await logout() -> None:调用登出端点并清除内存登录态(登出后仍可重新 login())
  • await save_session(path: str) -> None:用 FileSessionStorage 将当前会话写入本地 JSON 文件
  • await restore_session(path: str) -> None:从 save_session() 写入的 JSON 文件恢复会话
  • export_account() -> AccountInfo:导出完整账号状态(Android/Web,同步,不需 await;iOS 暂不支持)
  • export_account_string() -> str:导出平台账号字符串(Android/Web,同步;iOS 暂不支持)

save_session/restore_session 的 JSON 格式与 export_account_string() 的账号字符串格式不同,两者不可混用读写。

资源 API 一览

所有资源 API 均以 igapi.XxxApi(client) 构造,方法为 async。完整签名见 Python LLM 速查 或 LLM 索引:

资源 API 主要方法 可用平台
igapi.UserApi info / search / id_from_username / info_by_username / web_id_from_username 全平台;web_id_from_username 仅 Web
igapi.MediaApi info / info_by_shortcode 全平台
igapi.FeedApi user / user_all 全平台
igapi.UploadApi photo / configure / photo_web / configure_web(web) 全平台
igapi.PostApi user_posts / create_photo 仅 igapi.web.Client
igapi.DirectApi create_group_thread(web) / create_group_thread_graphql(web) / send_text(web) / send_text_to_thread(web) / send_text_broadcast(android+web) Android / Web

平台命名空间

平台必须通过显式命名空间选择,不再从 igapi 顶层导入全局客户端或全局 AccountInfo:

from igapi.android import AccountInfo as AndroidAccountInfo
from igapi.android import Client as AndroidClient
from igapi.ios import Client as IosClient
from igapi.web import AccountInfo as WebAccountInfo
from igapi.web import Client as WebClient

android = AndroidClient()
ios = IosClient()
web = WebClient()

Android 从结构化账号信息恢复 session:

from igapi.android import AccountIdentity, AccountInfo, Client, DeviceInfo, SessionInfo

account = AccountInfo(
    identity=AccountIdentity("username", "password"),
    device=DeviceInfo("android_id", "phone_id", "uuid", "device_id"),
    session=SessionInfo(sessionid="...", ds_user_id=123456789),
)
client = Client(account=account)

Android 和 Web 均支持账号字符串入口恢复 session:

from igapi.android import AccountInfo as AndroidAccountInfo
from igapi.web import AccountInfo, Client

android_account = AndroidAccountInfo.parse(
    "username:password||android_id;phone_id;uuid;device_id|sessionid=...; ds_user_id=...||"
)
account = AccountInfo.parse("username:password||csrf_token|sessionid=...; ds_user_id=...||")
client = Client(account=account)

account_str = client.export_account_string()

类型定义

User

class User:
    pk: int                 # 用户 ID
    username: str           # 用户名
    full_name: str          # 全名
    is_private: bool        # 是否私密账户
    profile_pic_url: str    # 头像 URL
    follower_count: int     # 粉丝数
    following_count: int    # 关注数

Media

class Media:
    id: str                 # 媒体 ID
    media_type: int         # 类型(1=图片, 2=视频, 8=轮播)
    caption_text: str       # 描述文字
    like_count: int         # 点赞数
    comment_count: int      # 评论数

Feed API 不返回独立的 Feed 类型,而是直接返回二元组:

items, next_cursor = await igapi.FeedApi(client).user(12345678)

错误处理

import asyncio
import igapi
from igapi.android import Client

async def main():
    client = Client()

    try:
        await client.login("user", "pass")
    except igapi.TwoFactorRequired as e:
        print(f"需要双因素认证: {e}")
    except igapi.ChallengeRequired as e:
        print(f"需要安全验证: {e}")
    except PermissionError as e:
        print(f"登录错误: {e}")
    except ValueError as e:
        print(f"无效输入: {e}")
    except RuntimeError as e:
        print(f"错误: {e}")

asyncio.run(main())

使用代理

# HTTP 代理
from igapi.web import Client, Proxy, ProxyType

client = Client(proxy=Proxy.http("localhost", 8080))

# HTTPS 目标站也通常走 HTTP CONNECT 代理
client = Client(proxy=Proxy.http("proxy.example.com", 8080, username="user", password="pass"))

# 代理池常见格式(显式解析为 Proxy 对象)
client = Client(proxy=Proxy.from_url("proxy.example.com:8080:user:pass"))

# SOCKS 代理(Web 客户端支持)
proxy = Proxy(
    proxy_type=ProxyType.SOCKS5H,
    host="localhost",
    port=1080,
)
client = Client(proxy=proxy)

示例

更多完整示例请参见仓库根目录的 docs/getting-started/快速上手.md 和 docs/api/。

开发

构建

# 调试构建
maturin develop --manifest-path src/py/Cargo.toml

# 发布构建
maturin develop --release --manifest-path src/py/Cargo.toml

# 构建 wheel 包
maturin build --release --manifest-path src/py/Cargo.toml

测试

# 运行测试
python -m pytest tests/

# 类型检查
mypy --strict examples/

性能

Python 绑定使用 PyO3,保持接近原生 Rust 的性能:

  • API 调用通过 Tokio 异步处理
  • 最小化序列化开销
  • 尽可能使用零拷贝数据访问

许可证

MIT OR Apache-2.0

链接

Metadata

Release files for igapi-rs 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for igapi-rs 0.1.7
File Size Uploaded Provenance Actions
igapi_rs-0.1.7.tar.gz 160.6 kB With provenance Details for igapi_rs-0.1.7.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for igapi-rs 0.1.7
File Interpreter ABI Platform Actions
igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details for igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_x86_64.whl
igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ ARM64 Details for igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_aarch64.whl
igapi_rs-0.1.7-cp312-cp312-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64, macOS 10.13+ universal2 (ARM64, x86-64), macOS 10.13+ x86-64 Details for igapi_rs-0.1.7-cp312-cp312-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl

Total release size: 25.9 MB

Release files / igapi_rs-0.1.7.tar.gz

Download URL igapi_rs-0.1.7.tar.gz
Size 160.6 kB
Tags Source
SHA-256 checksum
How to use checksums
54fde2494647522f2f835b8d84a50fba9921024cef3a5424664ec6e2e8e2f92a
BLAKE2b-256 checksum
How to use checksums
35b89115db1519d591233631356d39951ffc0e6f14020c8b4679d88413009e54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2026.

Transparency log

Release files / igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_x86_64.whl
Size 6.7 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
5c91af68c39aca758f8d721a1e96c4ea9d82e26680bfd8775fb5c712a415c808
BLAKE2b-256 checksum
How to use checksums
fc3198babdd7617e47a29f15b9f7d82477bff1101afbd16e8df04c3e65a66232
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2026.

Transparency log

Release files / igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_aarch64.whl

Download URL igapi_rs-0.1.7-cp312-cp312-manylinux_2_28_aarch64.whl
Size 6.6 MB
Tags CPython 3.12 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
de2b9f72732b3fbf5fc03d2f6a26c9e5a8e32adfba111f399fc1cb502f4c1d2e
BLAKE2b-256 checksum
How to use checksums
b41b8994a86f0c3825647397f01b7b54936e1d1d5ce66d7ba53b802e7e9bf675
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2026.

Transparency log

Release files / igapi_rs-0.1.7-cp312-cp312-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl

Download URL igapi_rs-0.1.7-cp312-cp312-macosx_10_13_x86_64.macosx_11_0_arm64.macosx_10_13_universal2.whl
Size 12.4 MB
Tags CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64) macOS 10.13+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4817bcdf210a50e2efb8f00e598a9b58fa69518156ade60b5e2b94ffb5c95009
BLAKE2b-256 checksum
How to use checksums
f247ddd0e39495c8cd18e4b1a0d9032c51c7a6c96254f8bf5c0263f3ec5ae4e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2026.

Transparency log

Security for igapi-rs 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for igapi_rs-0.1.7.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page