You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Instagram私信(DM)API解决方案:使用Python Requests开发私信机器人遇网络请求无记录问题

Troubleshooting Instagram DM API for Your Bot

Hey there! Let’s work through this Instagram DM API problem you’re hitting. It’s super common to struggle with finding the right requests because Instagram’s DM system doesn’t use straightforward HTTP calls like you might expect—here’s what you need to know:

Why You Aren’t Seeing the Request in Dev Tools

Instagram’s web-based DM interface relies heavily on WebSockets (check the "WS" tab in your browser’s dev tools instead of the standard "Network" tab) for real-time messaging. That’s why you won’t spot a typical POST/GET request when sending a message there. The mobile app, however, uses traditional HTTPS requests for DMs, which are far easier to capture.

Solutions & Next Steps

This is the only way to build a DM bot without risking your account getting banned. Here’s how to get started:

  • Apply for a Meta Developer account, create an app, and get approved for required permissions: instagram_basic, pages_show_list, and instagram_manage_messages.
  • Once set up, use the POST endpoint https://graph.facebook.com/v18.0/{ig-user-id}/messages (replace v18.0 with the latest API version and {ig-user-id} with your bot’s Instagram user ID) to send messages.
  • The request body should look like this:
    {
      "recipient": {"id": "TARGET_USER_ID"},
      "message": {"text": "Your message here"}
    }
    
  • Authenticate using a valid access token tied to your app.

2. Capture Mobile App Requests for Private API Use (Unofficial, Risky)

If you need a quick test or can’t go through the official API process, capture the private API calls the Instagram mobile app makes:

  • Use tools like Charles Proxy or Mitmproxy to intercept your phone’s HTTPS traffic. You’ll need to install the tool’s root certificate on your device to decrypt SSL traffic.
  • Open the Instagram app, send a test DM, then filter captured requests for keywords like direct_v2 or broadcast/text. You’ll find a POST request with a URL similar to https://i.instagram.com/api/v1/direct_v2/threads/broadcast/text/.
  • Note critical headers (especially Authorization with your session token, X-CSRFToken, and a mobile-app-like User-Agent) and the JSON request body (which includes thread_ids and your message text).
  • Replicate this request with Python Requests, making sure to include all required cookies and headers to avoid being blocked.

3. Use a Pre-Built Python Library (Unofficial, Convenient)

Skip building everything from scratch with libraries like instagrapi, which wraps Instagram’s private API. Here’s a quick example:

from instagrapi import Client

cl = Client()
cl.login("YOUR_USERNAME", "YOUR_PASSWORD")

# Send a DM to a user by their username
user_id = cl.user_id_from_username("target_username")
cl.direct_send("Hello from my bot!", user_ids=[user_id])

Keep in mind: using private APIs violates Instagram’s Terms of Service, so your account could be restricted or banned. Stick to testing with throwaway accounts.

Key Notes

  • Prioritize the official Graph API for long-term projects—it’s the only supported, safe method.
  • Private API endpoints change frequently, so any custom implementation might break without warning.
  • Mimic real user behavior (avoid spamming messages quickly) to avoid triggering Instagram’s anti-bot systems.

内容的提问来源于stack exchange,提问作者Usama Javed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:47:45