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
- Acesse o ChatGPT Plus.
- Clique em "Explorar GPTs" no menu lateral.
- Selecione "Criar novo GPT".
- Personalize o comportamento do GPT conforme sua necessidade.
- Na aba Ações, clique em "Criar Nova Ação":
- Dê um nome à ação (por exemplo, "Enviar para N8N").
- Configure o método HTTP como POST.
- Insira a URL do WebHook gerado pelo N8N.
2. Set up the WebHook in N8N
- Abra o painel do N8N e crie um novo fluxo.
- Adicione um nó WebHook:
- Escolha um nome para o nó.
- Configure o método como POST.
- Copie a URL do WebHook gerado para usar no Chat Personalizado.
- Teste o WebHook enviando uma requisição diretamente pelo ChatGPT para verificar se os dados estão sendo recebidos corretamente.
3. Send ChatGPT Data to the WebHook
- No ChatGPT, ao interagir com o Chat Personalizado, acione a ação criada.
- Os dados processados pelo ChatGPT serão enviados automaticamente para o WebHook do N8N.
4. Create the Flow in N8N
- Após receber os dados no WebHook, configure outros nós no N8N para processar as informações. Exemplos:
- Armazenar os dados em uma planilha (Google Sheets).
- Enviar notificações por e-mail.
- Criar tarefas em ferramentas de gestão como Trello ou ClickUp.
- Atualizar um banco de dados ou sistema de CRM.
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.