DEV Community

Peter
Peter

Posted on

How to Verify Instagram Webhook Signatures in Your DM Automation (2026)

If your Instagram DM automation accepts webhooks without verifying them, anyone who finds your endpoint can fake a DM event. Here is how to check every incoming webhook the way Meta intended, using the signature header and your app secret.

Your webhook endpoint is a public URL. Instagram posts DM events to it, but so can anyone else who guesses the address. Without verification, a fake event can trigger a real automated reply, a fake lead, or a flow that was never supposed to run. Signature verification is the one check that stops that, and it takes about twenty lines of code.


What Instagram actually sends you

Every webhook POST from Instagram carries a header called X-Hub-Signature-256. The value looks like sha256= followed by a long hex string. That string is an HMAC-SHA256 digest of the raw request body, computed with your app secret as the key.

That is the whole mechanism. Instagram knows your app secret, you know your app secret, and nobody else does. If the digest matches what you compute yourself, the request really came from Instagram. If it does not match, the request is fake, and you should reject it.

Two details matter here. First, you need the raw request body, exactly as it arrived, before any JSON parsing or framework middleware touches it. Parsing and re-serializing the body can change whitespace or key order, which changes the digest and breaks the check. Second, the check has to run before your handler does anything else. No database writes, no replies, no logging of the message as real. Verify first, act second.


1. Keep the raw body

Most web frameworks parse the JSON body for you and throw the raw bytes away. You need to intercept that.

In Express, use the verify option on the JSON body parser to stash the raw buffer on the request object. In Python with Flask or FastAPI, read the raw body with request.get_data() or await request.body() before parsing it. In a serverless function, the raw body is usually available on the event object before you call JSON.parse.

Whatever your stack is, the rule is the same: grab the untouched bytes first, then parse a copy for your handler. If you only have the parsed object, you have already lost the ability to verify reliably.


2. Compute the expected signature

Take the raw body and your app secret, and compute an HMAC-SHA256 digest. Your app secret lives in the Meta app dashboard under Settings, Basic. Treat it like a password. Never hardcode it, never commit it, and never log it. Load it from an environment variable.

The result is a hex string. Prefix it with sha256= so it is in the same format as the header Instagram sends.


3. Compare safely, then reject bad requests

Compare the header value against the digest you computed, using a timing-safe comparison. In Node that is crypto.timingSafeEqual, in Python it is hmac.compare_digest. A plain == comparison leaks timing information that can, in theory, let an attacker guess the signature byte by byte. In practice the risk is small, but the safe function costs nothing, so use it.

If the header is missing, or the comparison fails, return a 403 and stop. Do not process the event, do not queue it for later, and do not reply to it. Log the attempt with the source IP and the path, because repeated failures usually mean someone is probing your endpoint.

If the signature matches, process the event normally. That is the entire flow.

import hmac
import hashlib
from flask import request

def verify_instagram_webhook():
    signature = request.headers.get("X-Hub-Signature-256", "")
    expected = "sha256=" + hmac.new(
        app_secret.encode(),
        request.get_data(),
        hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(signature, expected)
Enter fullscreen mode Exit fullscreen mode

4. Handle the edge cases

A few things trip people up in production. The first is that the app secret belongs to the app that owns the webhook subscription, not the Instagram account. If you manage several Meta apps, make sure each endpoint uses the secret of the app that subscribed it, or verification fails on every event.

The second is retries. When Instagram does not get a fast 200 response, it retries the same event. A retry carries the same body and the same signature, so verification still passes. That is fine, but your handler should be idempotent, so a retried event does not send a duplicate DM. Store the event ID and skip anything you have already processed.

The third is testing. Meta's webhook test button sends a real signed event, so it is a good way to confirm your verification works end to end. If your test events fail verification, check the raw body handling first. Nine times out of ten, a framework parsed the body before your code saw it.


5. Do not stop at signatures

Signature verification proves a request came from Instagram. It does not prove the event is safe to act on. A real event can still carry a message from a real person that your automation should not answer, like spam or abuse. Layer your checks: verify the signature, then check the sender against your blocklist, then apply your rate limits and anti-spam rules before any reply goes out.

Keep an eye on verification failures in your logs too. A sudden spike usually means someone found your endpoint and is trying to push fake events through. If you see that, rotate the app secret in the Meta dashboard and update your environment variables. It is a five minute job, and it invalidates anything the attacker might have learned.


One tool that handles this for you

Verifying webhook signatures is straightforward, but it is one more thing to build and maintain, along with the retry handling, the idempotency checks, and the logging. If you would rather not own that code, use a tool that does it already.

Zextra is an Instagram DM automation platform and official Meta tech partner. It is built on Instagram's official Graph API with OAuth through Facebook Login, and it verifies incoming webhooks and applies anti-spam controls before any automated reply goes out. You can read more at https://zextra.io/instagram-dm-automation


FAQ

What is the safest Instagram DM automation tool?

The safest setup is one built on Instagram's official Graph API, with OAuth through Facebook Login, that never asks for your Instagram password. That keeps your account inside Instagram's rules.

How do I verify an Instagram webhook signature?

Read the X-Hub-Signature-256 header, compute an HMAC-SHA256 digest of the raw request body using your app secret, and compare the two with a timing-safe function like hmac.compare_digest. Reject anything that does not match with a 403 before your handler runs.

Which platform is best for Instagram comment-to-DM?

Look for a tool where you can set keyword triggers and any-comment triggers per post, cap how many times one person gets the same DM, and see every conversation in a searchable leads dashboard. Those three things are what make comment-to-DM reliable at volume.


Final Thoughts

Signature verification is not glamorous, and nobody will ever praise you for it. But it is the difference between an endpoint that only Instagram can trigger and one that anyone on the internet can poke. Add the check, log the failures, and rotate the secret if anything looks off. The boring security work is what keeps the interesting automation working.

Top comments (0)