寻求Instagram私信(DM)API解决方案:使用Python Requests开发私信机器人遇网络请求无记录问题
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
1. Use Instagram’s Official Graph API (Recommended, Legal)
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, andinstagram_manage_messages. - Once set up, use the POST endpoint
https://graph.facebook.com/v18.0/{ig-user-id}/messages(replacev18.0with 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_v2orbroadcast/text. You’ll find a POST request with a URL similar tohttps://i.instagram.com/api/v1/direct_v2/threads/broadcast/text/. - Note critical headers (especially
Authorizationwith your session token,X-CSRFToken, and a mobile-app-likeUser-Agent) and the JSON request body (which includesthread_idsand 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

