Direct Chat API

Read your account and Instagram accounts from your own software, or connect an app with one button. Base URL: https://dmly.dotmagicinfotech.com/api/v1. Requests and responses are JSON. Times are UTC in ISO 8601.

Authentication

Create a key in Direct Chat under API keys. It is shown once, so store it safely. Send it on every request:

curl https://dmly.dotmagicinfotech.com/api/v1/me \
  -H "Authorization: Bearer dc_live_..."

Keys belong to one Direct Chat account and can only see that account's data. Revoke a key on the same page; it stops working at once.

Connect button (OAuth 2.0)

Apps registered with Direct Chat can add a Connect to Direct Chat button instead of asking for a key. It uses the authorization code flow with PKCE (S256 only).

  1. Send the person to https://dmly.dotmagicinfotech.com/oauth/authorize?client_id=...&redirect_uri=...&scope=leads:read%20account:read&state=...&code_challenge=...&code_challenge_method=S256. They sign in if needed, choose one Instagram account or all of them, and press Allow.
  2. Direct Chat returns to redirect_uri with code and state, or with error=access_denied. The code works once, for 10 minutes.
  3. Exchange it: POST https://dmly.dotmagicinfotech.com/oauth/token with grant_type=authorization_code, code, redirect_uri, code_verifier and the client credentials (HTTP Basic, or client_id and client_secret in the form).
{
  "access_token": "dc_at_...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "dc_rt_...",
  "scope": "leads:read account:read",
  "account_id": "acc_7",
  "webhook_secret": "whsec_..."
}

webhook_secret is included once, in the first token response, when the app has a webhook URL. Direct Chat signs events to that URL with it.

Access tokens last an hour. Refresh with grant_type=refresh_token and refresh_token; you get a new pair and the old refresh token stops working. Refresh tokens last 30 days. POST https://dmly.dotmagicinfotech.com/oauth/revoke with token revokes a token and its pair. Use the access token exactly like an API key. It sees only the Instagram accounts the person shared, and only its granted scopes. Token errors follow RFC 6749: { "error": "invalid_grant", "error_description": "..." }.

Scopes

Each key has the scopes chosen when it was made. An endpoint that needs a scope the key lacks answers 403 insufficient_scope.

ScopeAllows
leads:readSee your leads
automations:readSee your automations
automations:writeCreate and edit automations
messages:sendSend replies on your behalf
webhooks:manageReceive events
account:readSee your plan and Instagram accounts

Rate limits

Each key can make 120 requests a minute. Past that, the API answers 429 rate_limited with a Retry-After header in seconds.

Errors

Every error has the same body:

{ "error": "insufficient_scope", "message": "This key or token does not have the account:read scope.", "details": { "required_scope": "account:read" } }
StatuserrorMeaning
400invalid_requestA parameter is missing or not valid.
401unauthorizedNo Authorization header was sent.
401invalid_tokenThe key or token is wrong, revoked or expired.
403insufficient_scopeThe key or token does not have the scope this endpoint needs.
403api_disabledThe account's plan does not include the API.
404not_foundThe record does not exist in this account.
429rate_limitedToo many requests. Wait for the seconds in Retry-After.
500server_errorSomething went wrong on our side. Try again.

Pagination

Lists take ?limit= (1 to 100, default 50) and ?cursor=. They answer { "data": [...], "next_cursor": "..." }. Pass next_cursor as cursor to get the next page; it is null on the last page.

GET /me

Scope account:read. The account, its plan, limits and usage this billing period. A limit of null means unlimited.

{
  "id": "acc_7",
  "name": "Amrit365",
  "created_at": "2026-10-03T09:11:49.000Z",
  "plan": { "code": "growth", "name": "Growth", "period_start": "2026-11-01T00:00:00.000Z", "period_end": "2026-12-01T00:00:00.000Z" },
  "limits": { "instagram_accounts": 2, "live_automations": 30, "active_contacts": 2500, "api_enabled": true, "max_api_keys": 5 },
  "usage": { "instagram_accounts": 1, "live_automations": 4, "active_contacts": 312, "max_api_keys": 1, "api_requests": 1840 }
}

GET /instagram-accounts

Scope account:read. The account's Instagram accounts. status is connected, reconnect_needed or disconnected. A disconnected account also has disconnected_at and reason: revoked (Instagram ended the connection), expired (the login expired), by_you (disconnected in Direct Chat) or moved (connected to another Direct Chat account).

Change, 6 Nov 2026: disconnected accounts are now listed, with status: "disconnected", disconnected_at and reason. Fields of connected accounts are unchanged.

When an account disconnects, Direct Chat deletes its Instagram login at once. Its contacts, leads and messages are deleted 30 days later unless it reconnects first, and an instagram_account.purged event is sent at that moment so a connected app can delete its own copies.

{
  "data": [
    { "id": "iga_1", "username": "amrit365", "status": "connected", "connected_at": "2026-10-21T10:15:30.000Z" },
    { "id": "iga_2", "username": "amrit365.sale", "status": "disconnected", "connected_at": "2026-09-02T08:00:00.000Z", "disconnected_at": "2026-11-04T06:12:09.000Z", "reason": "revoked" }
  ],
  "next_cursor": null
}

Automations

Scope automations:read for GET, automations:write for the rest. An automation answers comments on one Instagram account: by keyword (comment_keyword) or every comment on chosen posts (any_comment_on_post). It posts the public reply under the comment and sends one DM, with an optional link button and an optional question for a phone number or email (capture). {{username}} in the public reply or DM becomes the person's Instagram name.

EndpointDoes
GET /automations?status=&instagram_account_id=&cursor=Lists automations, oldest first.
POST /automationsCreates one. 201.
PUT /automations/by-ref/{external_ref}Creates (201) or updates (200) the automation with your reference. Use this to sync from your own system.
GET /automations/{id}, GET /automations/by-ref/{external_ref}One automation.
PATCH /automations/{id}Changes the fields you send.
POST /automations/{id}/activate, /pauseTurns it live or back to draft.
DELETE /automations/{id}Deletes it for good. 204.
GET /automations/{id}/statstriggers, dms_sent, replies_posted, unique_users, last_triggered_at.
{
  "data": {
    "id": "aut_42",
    "name": "Kids Gummies post",
    "status": "live",
    "paused_reason": null,
    "instagram_account_id": "iga_1",
    "trigger_type": "comment_keyword",
    "keyword": "GUMMIES",
    "match_mode": "exact",
    "post_ids": ["17895695668004550"],
    "public_reply": "Sent it to your DMs, {{username}}!",
    "dm_message": "Here's the Kids Gummies page you asked for 👇",
    "dm_link": "https://amrit365.com/products/kids-gummies",
    "dm_link_title": "See the pack",
    "capture": "phone",
    "once_per_user": true,
    "external_ref": "post-2026-10-21-gummies",
    "created_via": "oauth_client:1",
    "editable": "full",
    "steps": null,
    "created_at": "2026-11-11T08:00:00.000Z",
    "updated_at": "2026-11-11T08:00:00.000Z"
  }
}
  • status is draft or live; creating without it makes the automation live. paused_reason is account_disconnected when Direct Chat paused it because its Instagram account disconnected, or plan_limit when the plan ended and the Free plan allows fewer live automations.
  • keyword is up to 60 characters. match_mode exact (default) matches the whole word, ignoring case; contains also matches inside longer words. post_ids takes up to 10 ids from GET /media; empty means every post.
  • public_reply up to 220 characters, dm_message up to 1000, dm_link an https address, dm_link_title up to 20 (Instagram's button limit; default “Open link”). capture is none, email, phone or either.
  • once_per_user (default true): a person gets the DM once; later comments still get the public reply.
  • template on create fills the keyword, public reply, DM and capture from a template; any field you send wins.
  • Flows built in Direct Chat with several messages have editable: "partial" and their messages in steps. The API can change their trigger, public reply, first DM, link and status.
  • created_via is dashboard, api_key:N or oauth_client:N. Webhook events carry the same automation as the number after aut_.
curl -X PUT https://dmly.dotmagicinfotech.com/api/v1/automations/by-ref/post-2026-10-21-gummies \
  -H "Authorization: Bearer dc_at_..." -H "Content-Type: application/json" \
  -d '{ "instagram_account_id": "iga_1", "template": "price", "keyword": "GUMMIES",
        "post_ids": ["17895695668004550"], "dm_link": "https://amrit365.com/products/kids-gummies" }'
StatuserrorMeaning
402plan_limitTurning it live would pass the plan's live-automation limit.
409external_ref_conflictPOST used an external_ref that another automation has; details.id is that automation.
422keyword_conflictA live automation on the same account already answers this keyword on these posts; details.conflicting_id names it.
422account_disconnectedThe Instagram account is disconnected, so the automation cannot go live.
422not_editable_via_apiA multi-step flow was sent a field the API cannot change; details.fields lists them.
422invalid_automationA field is missing or not valid; details.field names it.
422unknown_templatetemplate is not one of GET /templates.

Deprecated names. These older field names are still accepted on input and will be removed later. Responses always use the new names.

OldUse
media_idpost_ids (as a one-item list)
reply_textpublic_reply
dm_textdm_message
link_urldm_link
capture_mode (email_or_phone)capture (either)

GET /media

Scope automations:read. The 25 most recent posts and reels of ?instagram_account_id= (or the first shared account): id, caption (first 200 characters), permalink, timestamp, media_type.

GET /templates

Scope automations:read. The templates template accepts, each with the keyword, public reply, DM and capture it fills.

POST /messages/send

Scope messages:send. Sends a DM to someone who has messaged one of your Instagram accounts. Name the person with contact_id (from leads and events) or their ig_user_id. text is up to 1000 characters; control characters are removed. An optional link (url https, title up to 20 characters, default “Open link”) becomes a button. in_reply_to_event_id is kept with the message for your records.

curl -X POST https://dmly.dotmagicinfotech.com/api/v1/messages/send \
  -H "Authorization: Bearer dc_at_..." -H "Content-Type: application/json" \
  -d '{ "instagram_account_id": "iga_1", "contact_id": "ct_42", "text": "Yes, size M is in stock.",
        "link": { "url": "https://amrit365.com/kurta-m", "title": "Buy size M" } }'
HTTP/1.1 202 Accepted
{ "data": { "message_id": "msg_311", "status": "queued", "contact_id": "ct_42", "window_closes_at": "2026-11-12T09:41:00.000Z" } }

The answer is 202: the message is queued and goes out within seconds, ahead of automation DMs when the account is at Direct Chat's safe hourly pace. The result arrives as a dm.sent or dm.failed event carrying api_message_id. Instagram allows a DM only within 24 hours of the person's last message; outside it the answer is 422 outside_messaging_window with details.window_closed_at. A disconnected account is 422 account_disconnected. In Direct Chat's inbox the message shows “via” your app.

POST /comments/{comment_id}/reply

Scope messages:send. Replies to a comment Direct Chat received (comment ids come in automation.triggered events). Publicly by default, with text up to 220 characters; the result arrives as reply.posted. With "private": true it sends the commenter one private DM instead (up to 1000 characters), which Instagram allows once per comment and within 7 days of it: outside that, 422 outside_messaging_window; a second one, 409 already_replied. The answer is 202 with message_id, as above.

GET /leads

Scope leads:read. People who shared a phone number or email when an automation asked, oldest first. Add ?since= (ISO 8601) for leads changed since then. A connected app sees only the Instagram accounts it was given. Phones are in E.164. consent_marketing is true only when the person sent the details in answer to the automation's question. Deleted leads are not listed.

{
  "data": [
    {
      "id": "ct_42",
      "instagram_account_id": "iga_1",
      "instagram_user_id": "17841400000000000",
      "username": "priya.styles",
      "email": "priya@example.com",
      "phone": "+919876543210",
      "consent_marketing": true,
      "consent_at": "2026-11-06T08:21:04.000Z",
      "keyword": "PRICE",
      "source": { "automation_id": "12", "automation_name": "Kurta price list" },
      "created_at": "2026-11-06T08:19:40.000Z",
      "updated_at": "2026-11-06T08:21:04.000Z"
    }
  ],
  "next_cursor": null
}

Webhooks

Add an endpoint under Settings → Webhooks and choose its events. Direct Chat sends each event as a JSON POST of at most 8 KB to your https address. Answer with any 2xx status within 10 seconds. Apps connected with the Connect button get the same events at their webhook address, signed with the webhook_secret from the token response.

EventWhendata
lead.createdA new person comments or messageslead
lead.updatedSomeone shares a phone number or emaillead (with email, phone and consent)
message.receivedA direct message arriveslead, text, message_id
dm.sentAn automation sends a DMlead, automation_id, text
dm.failedAn automation DM failslead, automation_id, text, error
reply.postedAn automation replies to a commentlead, automation_id, text, comment_id
automation.triggeredA comment starts an automationlead, automation_id, automation_name, keyword, text, comment_id, media_id
instagram_account.connectedAn Instagram account connects or reconnectsinstagram_account_id, username, reconnect (true when an account that had disconnected came back)
instagram_account.disconnectedAn Instagram account disconnectsinstagram_account_id, username, reason
instagram_account.purgedA disconnected account's contacts and messages are deleted, 30 days laterinstagram_account_id, username
pingYou press Send testmessage
POST /your/webhook HTTP/1.1
Content-Type: application/json
X-DirectChat-Event: lead.updated
X-DirectChat-Event-Id: evt_5f0c1e9a2b7d4c3e8f6a1b2c
X-DirectChat-Signature: t=1762417264,v1=5d41…,v2=9b2e…

{
  "id": "evt_5f0c1e9a2b7d4c3e8f6a1b2c",
  "type": "lead.updated",
  "created_at": "2026-11-06T08:21:04.000Z",
  "account_id": "acc_7",
  "data": {
    "lead": { "id": "ct_42", "instagram_user_id": "17841400000000000", "username": "priya.styles", "email": "priya@example.com", "phone": "+919876543210", "consent_marketing": true, "...": "as in GET /leads" },
    "instagram_account_id": "iga_1"
  }
}

Checking the signature

X-DirectChat-Signature is t=<unix seconds>,v1=<hex>,v2=<hex>. Both are HMAC-SHA256 with your endpoint's signing secret: v1 signs <t>.<raw body>, and v2 signs <t>.POST.<path>.<raw body>, so a captured event cannot be replayed to another address. Check v2, refuse times more than 5 minutes off, and use X-DirectChat-Event-Id to ignore repeats.

import crypto from "node:crypto";

// raw: the request body exactly as received; path: the URL path, such as /your/webhook
function verify(secret, header, raw, path) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  if (Math.abs(Date.now() / 1000 - Number(parts.t)) > 300) return false;
  const v2 = crypto.createHmac("sha256", secret).update(`${parts.t}.POST.${path}.${raw}`).digest("hex");
  return crypto.timingSafeEqual(Buffer.from(v2, "hex"), Buffer.from(parts.v2 || "", "hex"));
}

Retries

A delivery that fails or times out is tried again after 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours, 24 hours, then marked failed. An endpoint that fails 100 times in a row is turned off and the account owner gets an email. The delivery log on the Webhooks page keeps 30 days and can send any event again with the same event id.