Help on creating Instagram automatic DM responses

I’M REPOSTING THIS. I HAVE MISTAKENLY SHARED MY KEYS IN THE OTHER POST. :')

Describe the problem/error/question

Hi everyone, I’m having issues attempting to make my n8n workflow answer Instagram DMs automatically using an agent. I tried lots of different approaches with no luck. I’m not a full expert on APIs and such but I made it work the rest of the ending nodes related to answer comments.

I’m using the Instagram API and this documentation: Messaging - Instagram Platform - Documentation - Meta for Developers

I would thank any help on this.

Thanks in advance,
Juan

What is the error message (if any)?

1- Bad Authentication

2- Bad request - please check your parameters

Unsupported post request. Object with ID ‘935743498382082’ does not exist, cannot be loaded due to missing permissions, or does not support this operation

Please share your workflow

Share the output returned by the last node

I’m having many issues. One was about Authentication, other was related to the following:

Bad request - please check your parameters

Unsupported post request. Object with ID ‘935743498382082’ does not exist, cannot be loaded due to missing permissions, or does not support this operation

Information on your n8n setup

  • **n8n version: 1.81.4
  • **Database (default: SQLite): None
  • **n8n EXECUTIONS_PROCESS setting (default: own, main): Own
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): Web App
  • **Operating system: Sequoia

The first error is telling you you’re having authentication issues (it’s not receiving a correct API key or credentials). Without more information, like on which specific node is it failing and/or throwing errors, it’s hard to say exactly why.

I noticed your HTTP request nodes have no headers being sent… in an HTTP request, the headers usually contain your API keys or credentials, so my best guess is that’s where the issue is.

Check their documentation to see what headers you need to send in your request.

EDIT: I ran out of daily posts because I’m a new account, but here’s my reply:

You’re trying to message with Instgram yea? I noticed your last node is using the facebook url instead of the instagram one.

The main problems are:

  1. Incorrect API Endpoint: The URL is using the sender’s ID instead of your professional account’s ID and the wrong domain (graph.facebook.com instead of graph.instagram.com).
  2. Incorrect Recipient in the Body: The recipient parameter is set to your professional account’s ID instead of the user’s ID.
  3. Improper Body Structure: The request body isn’t formatted as required by the Instagram API (nested JSON objects).

Fields2 Node Fix: (Fix the Recipient)


{
  "parameters": {
    "assignments": {
      "assignments": [
        {
          "id": "dda29a22-5777-4a9c-bf9f-643b723f8759",
          "name": "recipient",
          "value": "={{ $('Edit Fields1').item.json.senderID }}", // Changed to user's ID
          "type": "string"
        },
        {
          "id": "9f7fa7fd-f952-4b56-a1f6-cee234eec6cf",
          "name": "message",
          "value": "={{ $json.output }}", // Message from OpenAI, assuming this is correct
          "type": "string"
        }
      ]
    },
    "options": {}
  },
  "type": "n8n-nodes-base.set",
  "typeVersion": 3.4,
  "position": [1280, 720],
  "id": "f025b960-5161-4bea-af61-a5d3ff251844",
  "name": "Edit Fields2"
}

Responde Mensaje Node (Fix URL and Body)

{
  "parameters": {
    "method": "POST",
    "url": "https://graph.instagram.com/v22.0/{{ $('Edit Fields1').item.json.recipientID }}/messages",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendHeaders": true,
    "headerParameters": {
      "parameters": [
        {
          "name": "Content-Type",
          "value": "application/json"
        }
      ]
    },
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "{\n  \"recipient\": {\n    \"id\": \"{{ $json.recipient }}\"\n  },\n  \"message\": {\n    \"text\": \"{{ $json.message }}\"\n  }\n}",
    "options": {}
  },
  "type": "n8n-nodes-base.httpRequest",
  "typeVersion": 4.2,
  "position": [1500, 720],
  "id": "0e173d6d-c20b-4826-8531-84ad5eb8d7a9",
  "name": "Responde Mensaje",
  "credentials": {
    "httpHeaderAuth": {
      "id": "5Go6Ho5olx1BZHoo",
      "name": "Insta Secret"
    }
  }
}
2 Likes

Hi Cameron, thanks for the quick response.

My bad, I copy’ed my last post’s workflow without further explanation. The one that is failing is the last one on bottom right. The token is going thru the header but I corrected that because I was showing my keys in my last post. I created a new custom credential in order to hide that.

Did you have any luck trying to accomplish this in the past? Comments endpoint work like a charm, but I tried several hours with the messaging endpoint with no luck. The rest of the flow works fine, that one node has clearly bad configuration… Hope I learn a big lesson while fixing this one haha

Is there anything else I coud share with you in order to get this up and working? :slight_smile:

Thanks again!
Juan

1 Like

@CameronDWills your solution worked like a charm!! :rocket::clap:

Thanks a lot for your help, patience and knowledge. :muscle:

Best regards,
Juan

@juanfcpiccolo @CameronDWills hey I use a postman for sending message but i have a error

"error": {
        "message": "The user you requested could not be found.",
        "type": "IGApiException",
        "code": 100,
        "error_subcode": 2534014,
        "fbtrace_id": "AMs43TXbKvNfqj1uijFHjZx"
    }

So I have a question. What is * recipient:{id:<IGSID>} i dont know where I find IGSID. Can you help me? thank you

my postman request:

curl --location --request POST 'https://graph.instagram.com/v21.0/me/messages' \
--header 'Authorization: Bearer <MY-BEARER-TOKEN>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "recipient": {
        "id": "<IGSID>"
    },
    "message": {
        "text": "test send private DM"
    }
}'

Sure! the IGSID is the sender ID (the one that sends you the message).

The IG_ID, which you have to use while making the POST request is yours (first appeared as recipient ID, which is you/your professional account).

They switch, the sender becomes the recipient, and yours (first, recipient) becomes the sender.

Like this: https://graph.instagram.com/v22.0/<IG_ID>/messages (credits to Cam).

Hope this helps!

Let me know!
Juan

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.