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).
- 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. - Direct Chat returns to
redirect_uriwithcodeandstate, or witherror=access_denied. The code works once, for 10 minutes. - Exchange it:
POST https://dmly.dotmagicinfotech.com/oauth/tokenwithgrant_type=authorization_code,code,redirect_uri,code_verifierand the client credentials (HTTP Basic, orclient_idandclient_secretin 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.
| Scope | Allows |
|---|---|
leads:read | See your leads |
automations:read | See your automations |
automations:write | Create and edit automations |
messages:send | Send replies on your behalf |
webhooks:manage | Receive events |
account:read | See 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" } }| Status | error | Meaning |
|---|---|---|
| 400 | invalid_request | A parameter is missing or not valid. |
| 401 | unauthorized | No Authorization header was sent. |
| 401 | invalid_token | The key or token is wrong, revoked or expired. |
| 403 | insufficient_scope | The key or token does not have the scope this endpoint needs. |
| 403 | api_disabled | The account's plan does not include the API. |
| 404 | not_found | The record does not exist in this account. |
| 429 | rate_limited | Too many requests. Wait for the seconds in Retry-After. |
| 500 | server_error | Something 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.
| Endpoint | Does |
|---|---|
GET /automations?status=&instagram_account_id=&cursor= | Lists automations, oldest first. |
POST /automations | Creates 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, /pause | Turns it live or back to draft. |
DELETE /automations/{id} | Deletes it for good. 204. |
GET /automations/{id}/stats | triggers, 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"
}
}statusisdraftorlive; creating without it makes the automation live.paused_reasonisaccount_disconnectedwhen Direct Chat paused it because its Instagram account disconnected, orplan_limitwhen the plan ended and the Free plan allows fewer live automations.keywordis up to 60 characters.match_modeexact(default) matches the whole word, ignoring case;containsalso matches inside longer words.post_idstakes up to 10 ids fromGET /media; empty means every post.public_replyup to 220 characters,dm_messageup to 1000,dm_linkan https address,dm_link_titleup to 20 (Instagram's button limit; default “Open link”).captureisnone,email,phoneoreither.once_per_user(defaulttrue): a person gets the DM once; later comments still get the public reply.templateon 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 insteps. The API can change their trigger, public reply, first DM, link and status. created_viaisdashboard,api_key:Noroauth_client:N. Webhook events carry the same automation as the number afteraut_.
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" }'| Status | error | Meaning |
|---|---|---|
| 402 | plan_limit | Turning it live would pass the plan's live-automation limit. |
| 409 | external_ref_conflict | POST used an external_ref that another automation has; details.id is that automation. |
| 422 | keyword_conflict | A live automation on the same account already answers this keyword on these posts; details.conflicting_id names it. |
| 422 | account_disconnected | The Instagram account is disconnected, so the automation cannot go live. |
| 422 | not_editable_via_api | A multi-step flow was sent a field the API cannot change; details.fields lists them. |
| 422 | invalid_automation | A field is missing or not valid; details.field names it. |
| 422 | unknown_template | template 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.
| Old | Use |
|---|---|
media_id | post_ids (as a one-item list) |
reply_text | public_reply |
dm_text | dm_message |
link_url | dm_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.
| Event | When | data |
|---|---|---|
lead.created | A new person comments or messages | lead |
lead.updated | Someone shares a phone number or email | lead (with email, phone and consent) |
message.received | A direct message arrives | lead, text, message_id |
dm.sent | An automation sends a DM | lead, automation_id, text |
dm.failed | An automation DM fails | lead, automation_id, text, error |
reply.posted | An automation replies to a comment | lead, automation_id, text, comment_id |
automation.triggered | A comment starts an automation | lead, automation_id, automation_name, keyword, text, comment_id, media_id |
instagram_account.connected | An Instagram account connects or reconnects | instagram_account_id, username, reconnect (true when an account that had disconnected came back) |
instagram_account.disconnected | An Instagram account disconnects | instagram_account_id, username, reason |
instagram_account.purged | A disconnected account's contacts and messages are deleted, 30 days later | instagram_account_id, username |
ping | You press Send test | message |
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.