Skip to main content

Your First Automation

This guide walks you through building a real automation end-to-end. We'll create a workflow that monitors a Gmail inbox for new emails containing OTP codes and extracts them automatically.

Prerequisites

Step 1: Create the Automation

  1. Go to Dashboard → Automations
  2. Click + New Automation
  3. Select Workflow Builder (n8n engine) — this gives us the Code node for email parsing

Step 2: Add the Manual Trigger

The Workflow Builder opens with a Manual Trigger node already placed. This lets you test by clicking "Run" from the dashboard.

For production, you can later switch to a Schedule Trigger to check for new emails every few minutes.

Step 3: Add an IMAP Email Node

  1. Click the + button to add a new node
  2. Search for "Code" and select the Code node
  3. Paste this JavaScript code that connects to Gmail via IMAP:
const Imap = require('imap');
const { simpleParser } = require('mailparser');

const imapConfig = {
user: 'your-email@gmail.com',
password: 'your-app-password',
host: 'imap.gmail.com',
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false }
};

// Connect and fetch the latest email
const imap = new Imap(imapConfig);

return new Promise((resolve, reject) => {
imap.once('ready', () => {
imap.openBox('INBOX', false, (err, box) => {
if (err) reject(err);
const searchCriteria = ['UNSEEN', ['SUBJECT', 'verification code']];
imap.search(searchCriteria, (err, results) => {
if (err) reject(err);
if (!results || results.length === 0) {
imap.end();
resolve([{ json: { otp: null, message: 'No new OTP emails found' } }]);
return;
}
const latest = results[results.length - 1];
const fetch = imap.fetch([latest], { bodies: '' });
fetch.on('message', (msg) => {
msg.on('body', (stream) => {
simpleParser(stream, (err, parsed) => {
const otpMatch = parsed.text.match(/\b(\d{6})\b/);
imap.end();
resolve([{
json: {
otp: otpMatch ? otpMatch[1] : null,
subject: parsed.subject,
from: parsed.from.text,
date: parsed.date
}
}]);
});
});
});
});
});
});
imap.once('error', reject);
imap.connect();
});
  1. Replace your-email@gmail.com and your-app-password with your credentials

Step 4: Test the Automation

  1. Send yourself a test email with a 6-digit code in the subject (e.g., "Your verification code is 123456")
  2. Go back to Dashboard → Automations
  3. Find your automation and click Run
  4. Click View Runs to see the execution result
  5. The output will show the extracted OTP code, subject, sender, and date

Step 5: Activate for Production

To run this automatically:

  1. Open the automation in Workflow Builder
  2. Replace the Manual Trigger with a Schedule Trigger
  3. Set the interval (e.g., every 1 minute)
  4. Toggle the automation to Active from the dashboard

What You've Learned

  • How to create an automation using the Workflow Builder
  • How to use the Code node for custom logic
  • How to test and monitor runs from the dashboard
  • How to switch from manual to scheduled triggers

Next Steps

  • Triggers — Learn about all trigger types
  • Actions — Explore pre-built actions for 1,000+ apps
  • Error Handling — Add retry logic and failure notifications