Instagram DM Automation

Instagram Comment-to-DM Automation with the Official API

How to build Instagram comment to DM automation with the official Meta API: comment keywords trigger a public reply plus a private DM. No third-party tools needed.

Syakir Rahman
Syakir Rahman Updated September 20, 2026

I built comment-to-DM automation for our own Instagram in a day with AI help. Someone comments a keyword like GUIDE on a post, and they get a public reply plus a private DM with the link. It runs on the official Meta API with no third-party tools. I hosted it on my own existing server, but a serverless function like a Cloudflare Worker works fine too.

Almost every failure was silent: no error, just nothing happening. These six steps are the full build, in order. Each one gates the next.

Create the Meta app with Instagram messaging permissions

Create a Business type app at developers.facebook.com, then add the Instagram API with Instagram Login product. You do not need a Facebook Page.

Request these four scopes together, up front:

  • instagram_business_basic
  • instagram_business_manage_messages
  • instagram_business_manage_comments
  • instagram_business_manage_insights (for stats later; adding scopes later means re-authenticating)

Register your redirect URIs exactly, including /api/instagram/callback. Scheme, host, and path must match character for character.

Add your own Instagram account as a tester

Only tester accounts trigger anything before App Review, so add your own account under Roles, Instagram Testers. Two requirements:

  1. It must be a Business or Creator account. Convert it in the Instagram app under Settings, Account type and tools.
  2. Accept the invite inside the Instagram app, under Settings, Apps and Websites, Tester Invites. There is no notification. It waits there until you tap accept.

Publish the app and subscribe to webhooks

Meta sends zero webhooks in dev mode. Switch the app to Live. With Standard Access this exposes nothing to the public; data still flows only for testers. It just turns webhooks on. The dashboard Test button only sends sample payloads, so a green test proves reachability, not working logic.

Subscribe to comments and messages on the Instagram object, and verify the callback URL. Note: tunnel URLs rotate, and each rotation invalidates the callback. If webhooks stop after a restart, re-verify first.

Build the backend and connect it to your server

The flow: Meta posts a signed payload to your webhook endpoint, you verify the signature, match the comment against your keyword rules, then call the API twice (public reply, private DM). I run this on my own server (Astro endpoints plus Postgres for tokens, posts, rules, and processed comment IDs). A Cloudflare Worker with any small store works the same way.

What the docs do not spell out:

  • Token exchange has three hops: code to short-lived token to 60-day long-lived token. Store it encrypted, track expiry, refresh on schedule. An expired token fails with errors that look like scope problems.
  • The dashboard tick is not enough. Each account must also grant the subscription via the subscribed_apps edge after OAuth, or no events arrive.
  • Verify then parse. Check the X-Hub-Signature-256 HMAC against your App Secret before parsing.
  • Dedupe by comment ID. Meta retries deliveries. Record each processed comment ID and skip repeats, or a retry becomes a duplicate public reply.
  • Match through the synced post. Webhook IDs and token IDs live in different namespaces, so join on the comment’s media ID against your synced posts instead.
  • Policy is code, not copy. One DM per comment within 7 days, one follow-up within 24 hours. Enforce it in the send path, and include a human contact line plus opt-out in every message.

Test the webhooks end to end

Expose your local server (I used cloudflared), re-verify the callback URL against the tunnel address, then comment from your tester account on a synced post.

Healthy logs always look the same: request line, payload, sends complete. In that order.

The rule that would have saved me days: no log lines at all means the request never reached your server. That is a routing problem, not a code bug. Confirm with a signed test post to your own webhook URL. If your own test logs but real comments do not, the fault is Meta-side (dev mode, tester status, subscription, rotated tunnel). If even your own test logs nothing, the fault is routing.

Test the comment-to-DM automations

Comment keywords from a second tester account and check:

  • Contains vs exact. GUIDE fires; GUIDANCE must not fire in exact mode.
  • Non-followers. The DM still sends but often lands in Message Requests. Tell users to check Requests.
  • Dedupe. Replaying a comment ID sends nothing new; two fresh comments send once each.
  • Token failures. Expired token refreshes and retries; revoked permission shows a reconnect prompt.

For follow-ups, use CTA buttons via generic templates rather than pasted URLs. Standard Access covers your own account; a public rollout needs App Review for Advanced Access (paperwork, not code, and no Tech Provider status needed for your own account).

Six steps, one backend, zero third-party tools. Work in order and comment-keyword to reply-plus-DM runs on your own account.

Frequently asked questions

Frequently Asked Questions

Can I build Instagram comment-to-DM automation without third-party tools?

Yes. The official Instagram API with Instagram Login has everything you need. The comments webhook tells you when someone comments, the replies endpoint posts a public reply, and the messages endpoint sends a private DM. You only need a small backend to receive webhooks and call the API, which can be your own server or a serverless function like a Cloudflare Worker.

Why is my Instagram webhook not firing?

The most common cause is that the app is still in development mode, because Meta delivers no webhooks in dev mode. The second most common cause is a tester invite that was added but never accepted inside the Instagram app. The third is a tunnel URL that rotated, which invalidates the verified callback URL.

Which App ID and secret do I use for Instagram Login?

Use the Instagram App ID and App Secret from the Business Login settings of the Instagram API with Instagram Login product, not the generic App ID in App Settings under Basic. They look similar and the dashboard does not make the difference clear, which is why this step fails silently for many people.

How many DMs can the automation send per comment?

Meta policy allows exactly one private DM per triggering comment, and only within 7 days of the comment. One follow-up message is allowed within 24 hours. Every automated message should include a way to reach a human and an opt-out line.

Do I need App Review to automate DMs on my own account?

No. With Standard Access the automation works for tester accounts, which covers your own Instagram account. You only need App Review for Advanced Access when you want it to work for the public, for example other creators connecting their accounts.

Does the commenter need to follow me to get the DM?

No. The DM still sends for non-followers, but Instagram may place it in the Message Requests folder instead of the main inbox. Tell your audience to check Requests.