DEV Community

Youvandra Febrial
Youvandra Febrial

Posted on

Building AI Automation Workflows with n8n and OpenAI

🚀 Building AI Automation Workflows with n8n & OpenAI

Ever wished you could get a chatbot to write your commit messages while you’re sipping a cold brew?

You’re not alone. In this article we’ll walk through how to glue n8n (the open‑source workflow engine) together with OpenAI’s API to create smart, reusable automations. No heavy‑weight infra, just a few clicks, a sprinkle of JavaScript, and a lot of fun. 🎉


🎯 Why Combine n8n + OpenAI?

  • Zero‑code UI – Design flows visually, then drop in a tiny code node when you need custom logic.
  • Scalable – Run locally, on Docker, or in the cloud; n8n handles the orchestration, OpenAI does the heavy AI lifting.
  • Reusable – Once you’ve built a “Generate commit message” workflow, you can call it from any repo, CI pipeline, or Slack bot.

Think of n8n as the conductor and OpenAI as the soloist. Let’s get them to jam together.


🛠️ Prerequisites

What you need Why
n8n (Docker or self‑hosted) To create and run the workflow
OpenAI API key To talk to GPT‑4/3.5
Node.js (optional) For local testing of the JS code node
A Git repo (any language) To see the automation in action

If you already have n8n running, skip the Docker step; otherwise:

# Quick start with Docker
docker run -d \
  --name n8n \
  -p 5678:5678 \
  -v ~/.n8n:/home/node/.n8n \
  n8nio/n8n
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:5678 in your browser and you’re ready to roll.


📚 Step‑by‑Step: “AI‑Powered Commit Message Generator”

1️⃣ Create a New Workflow

  1. Click + New Workflow.
  2. Rename it to Generate Commit Message.
  3. Drag a Webhook node onto the canvas – this will be the entry point.

2️⃣ Capture the Diff

Add a GitHub node (or a simple HTTP Request node if you prefer raw Git).

Configure it to fetch the latest diff:

{
  "method": "GET",
  "url": "https://api.github.com/repos/{{ $json.owner }}/{{ $json.repo }}/commits/{{ $json.sha }}/diff",
  "headers": {
    "Authorization": "Bearer {{ $credentials.githubApiKey }}"
  }
}
Enter fullscreen mode Exit fullscreen mode

Tip: If you’re using a private repo, store the token in CredentialsAPI Key to keep it out of the workflow JSON.

3️⃣ Call OpenAI

Drop a HTTP Request node after the diff node. Set it to POST https://api.openai.com/v1/chat/completions with these headers:

Header Value
Authorization Bearer YOUR_OPENAI_API_KEY
Content-Type application/json

Body (JSON):

{
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant that writes concise, conventional commit messages."
    },
    {
      "role": "user",
      "content": "Here is a git diff:\n{{ $json.diff }}\nWrite a commit message (max 50 chars)."
    }
  ],
  "temperature": 0.2,
  "max_tokens": 60
}
Enter fullscreen mode Exit fullscreen mode

n8n will replace {{ $json.diff }} with the diff you fetched earlier.

4️⃣ Parse the Response

Add a Set node to extract the generated message:

{
  "commitMessage": "={{ $json.choices[0].message.content.trim() }}"
}
Enter fullscreen mode Exit fullscreen mode

5️⃣ Push the Commit (optional)

If you want the workflow to automatically commit, add another GitHub node:

  • Operation: Create Commit
  • Message: {{ $json.commitMessage }}
  • Branch: {{ $json.branch }}

⚠️ Caution: Auto‑committing is powerful but can be risky. Consider gating it behind a PR review or a Slack approval step.

6️⃣ Respond to the Caller

Finally, attach a Return node to the webhook so the caller (e.g., your CI job) gets the message back:

{
  "status": "success",
  "commitMessage": "{{ $json.commitMessage }}"
}
Enter fullscreen mode Exit fullscreen mode

🧩 Bonus: Add a “Human in the Loop” Slack Prompt

Sometimes you want a quick sanity check before the commit lands. Insert a Slack node after the OpenAI step:

  1. Send Message → channel #dev‑ops
  2. Text: AI suggests: *{{ $json.commitMessage }}* – approve? (yes/no)

Then add a Wait for Trigger node that listens for a reply. If the reply is yes, continue to the GitHub commit node; otherwise, abort.


📌 Tips & Tricks

  • Cache diffs – Use the Cache node to avoid hitting the GitHub API multiple times in the same workflow run.
  • Prompt engineering – Adding a few examples in the system prompt (e.g., "feat: add login flow""Added login flow") dramatically improves output quality.
  • Rate limits – OpenAI’s free tier is generous, but add a Rate Limit node if you expect many CI calls.
  • Environment variables – Store OPENAI_API_KEY and GITHUB_TOKEN in the Environment section of n8n for easier rotation.
  • Debugging – Turn on Execution Mode → Manual and hit Run to see each node’s output in the UI.

🎉 Wrap‑Up

You now have a fully functional AI‑powered workflow that:

  1. Receives a webhook (or CI trigger).
  2. Pulls the latest code diff.
  3. Lets GPT‑4 craft a conventional commit message.
  4. Optionally asks for human approval.
  5. Commits the change back to GitHub.

The beauty of n8n is that you can clone this pattern for any repetitive dev task—auto‑generated documentation, issue triage, even code review summaries. The sky’s the limit when you pair a flexible orchestrator with a powerful LLM. 🚀

What will you automate next? Drop a comment below, share your workflow JSON, or ask for help tweaking the prompt. Let’s build the future of developer productivity together!


References

Happy automating! 🎈

Top comments (0)