n8n integration

This document provides detailed instructions on how to use the n8n workflow automation tool to connect multiple MaiAgent AI assistants. Through n8n, you can easily create workflows to achieve multi-assistant integration.

Prerequisites

  1. Registered n8n account

  2. Valid MaiAgent API key

  3. AI assistant built and assistant ID obtained

  4. Basic understanding of APIs and workflows

Basic Concepts

The MaiAgent API provides functionality to converse with AI assistants. We will use n8n to automate these processes and connect multiple AI assistants.

Workflow Examples

Example 1: Create a simple AI assistant chaining workflow

This workflow will receive a user's chat message, then process it sequentially through two different AI assistants, forming a chained processing flow.

Step 1: Set up the chat trigger node

  1. Add a "When chat message received" node to the n8n workflow

  2. This will serve as the workflow's starting point and trigger when a user sends a message

Step 2: Set up the first HTTP Request node

  1. Add an "HTTP Request" node connected to the "When chat message received" node

  2. Configure the following settings:

    • Method: POST

    • URL:https://api.maiagent.ai/api/v1/chatbots/{id}/completions/

    • id: assistant ID

    • Authentication: Headers

    • Headers: Add Authorization header, value is Api-Key YOUR_API_KEY(Please replace with your actual API key)

    • Request content type: JSON

    • Request body:

    {
      "conversation": "",
      "message": {
        "content": "{{ $json.chatInput }}",
        "attachments": []
      },
      "is_streaming": false
    }

Step 3: Set up the second HTTP Request node

  1. Add another "HTTP Request" node connected to the first HTTP Request node

  2. Configure the following settings:

    • Method: POST

    • URL:https://api.maiagent.ai/api/v1/chatbots/{id}/completions/

    • id: assistant ID

    • Authentication: Headers

    • Headers: Add Authorization header, value is Api-Key YOUR_API_KEY

    • Request content type: JSON

    • Request body:

    {
      "conversation": "",
      "message": {
        "content": {{ JSON.stringify($json.content) }},
        "attachments": []
      },
      "is_streaming": false
    }

This configuration passes the first AI assistant's response as input to the second AI assistant, achieving chained processing.

Chaining Use Cases

Use Case 1: Multi-expert opinion aggregation

Have assistants with different expertise provide their perspectives, then aggregate them into a comprehensive response:

  1. Have a legal assistant analyze legal issues

  2. Have a technical assistant analyze technical feasibility

  3. Have a business assistant analyze business impact

  4. Finally aggregate the assistants' input to provide a combined recommendation

Use Case 2: Translation improvement workflow

  1. Initial assistant performs the translation

  2. Secondary assistant checks translation quality

  3. Final assistant polishes and optimizes the expression

Use Case 3: Content generation and review workflow

  1. Creative assistant generates content

  2. Review assistant checks accuracy and appropriateness

  3. Editing assistant polishes and optimizes formatting

Advanced Application: Create a multi-turn conversation workflow

Example 2: Use conversation ID to maintain multi-turn conversations

Step 1: Create a conversation initialization node

  1. Add an HTTP Request node for the initial conversation

  2. Configure similarly to before, but save the conversation ID:

// Save the conversation ID for later use
const conversationId = $json.conversationId;
$workflow.vars.conversationId = conversationId;

return {
  json: {
    ...$json,
    savedConversationId: conversationId
  }
};

Step 2: Set up the subsequent conversation node

  1. Add a new HTTP Request node configured to use the saved conversation ID:

    {
      "conversation": "{{$workflow.vars.conversationId}}",
      "message": {
        "content": "Continue the previous question, please provide more details"
      },
      "is_streaming": false
    }

Troubleshooting

Common issues and solutions:

  1. API authentication errors: Ensure the API key format is correct, including the "Api-Key" prefix

  2. Request timeouts: For complex queries, increase the timeout of the HTTP Request node

  3. Streaming mode handling: Use is_streaming: true , the reply mode will change to streaming mode

Conclusion

Through n8n, you can easily integrate and automate multiple MaiAgent AI assistants to create powerful AI workflows, allowing assistants with different expertise to collaborate on complex tasks. From simple conversations to complex multi-assistant chained processing, n8n provides flexible and powerful automation capabilities.

Appendix: Full workflow JSON

Below is the full workflow JSON configuration for Example 1, which you can import directly into n8n:

{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.maiagent.ai/api/v1/chatbots/{id}/completions/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Api-Key YOUR_API_KEY"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"conversation\": \"\",\n  \"message\": {\n    \"content\": \"{{ $json.chatInput }}\",\n    \"attachments\": []\n  },\n  \"is_streaming\": false\n} ",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -240,
        -180
      ],
      "id": "dfc1157d-7569-4caf-8b1d-256aa74515ac",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        -500,
        -180
      ],
      "id": "82748324-6021-4660-98ef-481bdfc73569",
      "name": "When chat message received",
      "webhookId": "5f833c90-a7f9-4c66-8fff-cecc3568cd2a"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.maiagent.ai/api/v1/chatbots/{id}/completions/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Api-Key YOUR_API_KEY"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"conversation\": \"\",\n  \"message\": {\n    \"content\": {{ JSON.stringify($json.content) }},\n    \"attachments\": []\n  },\n  \"is_streaming\": false\n} ",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        20,
        -180
      ],
      "id": "f0a80b1f-1bed-491b-bc5e-cddb87ccd2d5",
      "name": "HTTP Request1"
    }
  ],
  "pinData": {},
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "c914db70-985a-48d0-a1ca-47c2a0d4f244",
  "meta": {
    "instanceId": "2eb51fcaa84ba34149e7fc138f6cac3b20a0038764b284579d6ceaea90b669f8"
  },
  "id": "pvMcKA5hHmwAKuZT",
  "tags": []
}

Import Results


If you have any questions, please refer to MaiAgent official API documentationor n8n official documentation.

Last updated

Was this helpful?