How to use ChatGPT in N8N via WebHook without API

When we talk about automation and workflow using Artificial Intelligence, the most common solution is to use the GPT API. However, this approach can incur significant costs, especially depending on the model used.

But what if it were possible to use ChatGPT Plus, which costs only 20 USD per month, to send information directly to N8N? Imagine automating tasks like creating an article written by ChatGPT, registering users, or performing other actions that previously required external forms and manual field entry.

Fortunately, ChatGPT offers this possibility through communication via POST, using Custom Chats. In this article, we will explore how to set up this integration efficiently.

Advantages of using ChatGPT

In addition to the savings with the API of more expensive models, you may already have to pay for ChatGPT Plus, so you need to make the most of the platform's subscription. Another advantage is that ChatGPT's responses are usually better than those from the API, even when the temperature is set correctly.

Using ChatGPT instead of the API also allows freedom in manually editing prompts. If you create a Workflow with the API, you may need to edit the Workflow every time to make changes. Since automation exists in the Chat, you can converse with ChatGPT itself to send the data in any way you want, even allowing you to use a single Chat to trigger various different automations.

You can also send images and other information via Webhook, which would probably be much more expensive using API.

Connecting ChatGPT to N8N

In the video below, I explain better how I did this automation, but throughout the article I will help you set it up. Everything I did was done with the help of GPT itself, so ask it how to generate the code and create your automation according to your needs.

Setting up ChatGPT with N8N via WebHook

1. Create a Custom Chat in ChatGPT

  • Access ChatGPT Plus.
  • Click on "Explore GPTs" in the sidebar menu.
  • Select "Create new GPT".
  • Customize the behavior of GPT according to your needs.
  • In the flap Actions, click on “Create New Action”:
    • Give a name to the action (for example, "Send to N8N").
    • Configure the HTTP method as POST.
    • Insert the URL of the WebHook generated by N8N.

2. Set up the WebHook in N8N

  • Open the N8N panel and create a new workflow.
  • Add a node WebHook:
    • Choose a name for the node.
    • Configure the method as POST.
    • Copy the URL of the generated WebHook to use in the Custom Chat.
  • Test the WebHook by sending a request directly through ChatGPT to check if the data is being received correctly.

3. Send ChatGPT Data to the WebHook

  • No ChatGPT, when interacting with the Custom Chat, trigger the created action.
  • The data processed by ChatGPT will be automatically sent to the N8N WebHook.

4. Create the Flow in N8N

  • After receiving the data in the WebHook, set up other nodes in N8N to process the information. Examples:
    • Store the data in a spreadsheet (Google Sheets).
    • Send email notifications.
    • Create tasks in management tools like Trello or ClickUp.
    • Update a database or CRM system.

Code Used

The code below sends both the response and the question asked in the chat to N8N; now it is up to you to configure the prompt and N8N correctly to use the data as you wish. ChatGPT may also be able to send and separate more data; just configure new codes and prompts correctly.

openapi: 3.1.0
info:
  title: Enviar pergunta e resposta ao n8n
  description: Envia a pergunta e a resposta geradas pelo ChatGPT para o seu Webhook no n8n.
  version: 1.0.1
servers:
  - url: https://LINKDOWEBHOOKN8N
    description: Webhook configurado no n8n
paths:
  /:
    post:
      operationId: sendQuestionAndResponseToN8n
      summary: Envia a pergunta e a resposta para o n8n.
      description: Envia um JSON contendo a pergunta, a resposta gerada pelo ChatGPT e metadados opcionais.
      x-openai-isConsequential: false # Permite que o usuário autorize permanentemente sem confirmações futuras
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                question:
                  type: string
                  description: A pergunta enviada ao ChatGPT que será enviada ao n8n.
                response:
                  type: string
                  description: A resposta gerada pelo ChatGPT que será enviada ao n8n.
                metadata:
                  type: object
                  description: Metadados adicionais que podem ser usados para contexto.
                  properties:
                    userId:
                      type: string
                      description: Um identificador opcional para o usuário.
                    timestamp:
                      type: string
                      format: date-time
                      description: O horário em que a resposta foi gerada.
                    source:
                      type: string
                      description: A origem da solicitação, se aplicável.
      responses:
        '200':
          description: Dados enviados com sucesso.
        '400':
          description: Solicitação inválida.

x-openai-isConsequential: false

In some cases, you may encounter a permission question to connect to an external domain every time ChatGPT takes an action; the way to bypass this is with the option x-openai-isConsequential: false which allows you to enable the Always Allow option.

It is essential to use this function in your code to further expedite the process, as it is a manual action to contact chat to establish the connection with N8N and send the information.