Automate Ahrefs Keyword Research with n8n and Google Gemini AI

Discover how to streamline your SEO keyword research using n8n automation integrating Ahrefs API and Google Gemini AI. This workflow extracts, cleans, queries, and neatly formats relevant keyword data to save hours and improve SEO accuracy.
chatTrigger
lmChatGoogleGemini
httpRequest
+5
Workflow Identifier: 2239
NODES in Use: chatTrigger, lmChatGoogleGemini, httpRequest, code, aggregate, agent, memoryBufferWindow, stickyNote

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

Opening Problem Statement

Meet Lisa, a digital marketer running multiple SEO campaigns for her clients. Every week, Lisa spends several hours manually researching keywords using Ahrefs and other SEO tools to find the best keywords. She often struggles with sifting through large, unstructured keyword data, which results in missed opportunities and a lot of wasted time. Misspelled queries and vague search input further complicate her process, forcing her to double-check data multiple times.

For Lisa, taking hours to extract meaningful insights from raw keyword databases means less time optimizing content strategies and more frustration battling data overload. This inefficiency can delay campaign launches and negatively impact organic traffic growth.

What This Automation Does

This unique n8n workflow automates Ahrefs keyword research by integrating intelligent AI processing and API data extraction to create a smooth, error-resistant pipeline. When running, it:

  • Receives keyword queries via chat messages (like Telegram or WhatsApp)
  • Uses Google Gemini AI to extract and clean the main SEO keyword from the message, correcting any misspellings automatically
  • Calls the Ahrefs Keyword API through RapidAPI integration to retrieve detailed global keyword and related keyword data
  • Processes and extracts the most important keyword metrics using a custom JavaScript code node, including search volume, competition index, and CPC values
  • Formats the data neatly into a clean, readable summary via another AI agent node, ready to be presented back to the user
  • Aggregates all keyword data into a unified output for easy review or further automation

This automation can save Lisa several hours per keyword research session, reduce human error from typos, and deliver comprehensive keyword insights instantly.

Prerequisites βš™οΈ

  • n8n automation tool account (self-hosted options available via Hostinger)
  • Google Gemini (PaLM) API credentials for AI language models πŸ“§πŸ”‘
  • Ahrefs Keyword API access through RapidAPI platform πŸ”ŒπŸ”‘
  • Optional chat system integration (Telegram, WhatsApp, or any compatible webhook source) for receiving keyword queries πŸ’¬

Step-by-Step Guide

Step 1: Setting Up the Trigger (Chat Message Received)

Navigate to Nodes > Add node and select Chat Trigger (LangChain ChatTrigger). This node listens for incoming chat messages that contain keyword queries. Configure its webhook to receive messages from your preferred chat service.

You should see a generated webhook URL. Copy this URL and configure your chat system (e.g., Telegram bot) to send messages here.

Common mistake: Forgetting to link your chat service webhook or testing with incomplete payloads.

Step 2: Extracting and Cleaning the Keyword (Google Gemini AI Model)

Add a Google Gemini Chat Model node. Connect it to the chat trigger node. Use credentials for your Google PaLM API.

Set the system message parameter to instruct the AI to extract exactly one SEO keyword from the user’s message and correct any misspellings. Example system message:

You are a helpful assistant. Your job is to pick the SEO keyword from the message. Output only one keyword with no additional commentary.

The expected outcome is a clean, corrected keyword ready for API querying.

Common mistake: Using a generic prompt that returns additional text instead of a clean keyword.

Step 3: Requesting Keyword Data from Ahrefs API

Insert an HTTP Request node configured as follows:

  • Method: GET
  • URL: https://ahrefs-keyword-tool.p.rapidapi.com/global-volume
  • Query Parameter: keyword dynamically set from the AI-cleaned keyword output
  • Headers include your RapidAPI key and host

This node queries Ahrefs to return detailed keyword metrics.

Common mistake: Not setting the RapidAPI headers correctly or missing the dynamic query parameter.

Step 4: Extracting Main and Related Keyword Data (Code Node)

Add a Code node to process the raw API response. Use the JavaScript below to pull out the main keyword’s data and the top 10 related keywords, extracting key metrics such as average monthly searches, competition index, and CPC values:

// Get the main keyword data (Global Keyword Data)
const mainKeywordData = $input.first().json['Global Keyword Data']?.[0] || {};

// Get the related keywords array
const relatedKeywords = $input.first().json['Related Keyword Data (Global)'] || [];

// Create an output array that includes the main keyword data first
const output = [
  {
    keyword: mainKeywordData.keyword || 'N/A',
    avg_monthly_searches: mainKeywordData.avg_monthly_searches || 'N/A',
    competition_index: mainKeywordData.competition_index || 'N/A',
    competition_value: mainKeywordData.competition_value || 'N/A',
    high_cpc: mainKeywordData['High CPC'] || 'N/A',
    low_cpc: mainKeywordData['Low CPC'] || 'N/A'
  },
  // Map up to 10 related keywords with selected fields
  ...relatedKeywords.slice(0, 10).map(item => ({
    keyword: item.keyword,
    avg_monthly_searches: item.avg_monthly_searches,
    competition_index: item.competition_index,
    competition_value: item.competition_value,
    high_cpc: item['High CPC'],
    low_cpc: item['Low CPC']
  }))
];

return output;

After running, this node outputs a concise array of keyword details.

Common mistake: Not accessing the correct JSON paths from the raw API response, leading to empty outputs.

Step 5: Aggregating Keyword Data

Use the Aggregate node to combine all collected keyword data into a unified dataset. Simply add the node and connect it to the output of the code node.

This ensures the data is ready for formatting or downstream processes.

Step 6: Formatting the Keyword Data for Readability (AI Agent)

Add a LangChain Agent node configured to format the keyword data into a clean, human-readable text summary.

The system message instructs the AI to present the main keyword information and the related keywords clearly with metrics such as average monthly searches, competition index, and CPC values.

Modify the prompt system message in this node to customize your summary output style.

Common mistake: Neglecting to update the system message with your desired format can lead to unstructured outputs.

Step 7: Testing the Full Workflow

Trigger a chat message with a keyword query. Check each node’s output via the n8n UI to ensure the pipeline cleans, fetches, processes, aggregates, and formats data without errors.

Look for clean keyword extraction, valid API responses, accurate data processing, and neatly formatted final summaries.

Common mistake: Overlooking failed API calls or output data mismatches during testing.

Customizations ✏️

  • Change Keyword Input Channel: Replace the Chat Trigger node with a webhook for HTTP requests or a Telegram node for direct integration to customize how keywords enter the workflow.
  • Adjust API Endpoint Parameters: In the HTTP Request node, switch between different Ahrefs API endpoints (like “answer the public keywords” or “keyword overviews”) by editing the URL and query parameters to diversify data outputs.
  • Modify Data Extraction Logic: Edit the JavaScript in the Code node to include additional keyword metrics such as seasonal trends or URL competitiveness metrics depending on your SEO needs.
  • >

  • Customize AI Summary Formatting: Tweak the system message in the Keyword Data Response Formatter LangChain Agent node to add more details or change the format of the output summary.
  • Integrate Memory for Context: Use the Simple Memory node to build conversational context if extending this workflow to multi-turn keyword research chatbots.

Troubleshooting πŸ”§

Problem: “Error: Could not fetch data from Ahrefs API”

Cause: Incorrect RapidAPI key or host headers, or network issues.

Solution: Re-check your RapidAPI credentials in the HTTP Request node headers. Test your internet connection. Use the node’s retry feature to handle transient failures.

Problem: “Blank or no keyword extracted from user message”

Cause: The Google Gemini AI model prompt might be too vague or the user input does not contain clear keywords.

Solution: Refine the system message prompt in the Keyword Query Extraction & Cleaning Agent node to explicitly ask for a single precise SEO keyword. Test with diverse input phrases.

Problem: “Unexpected data structure in Code Node output”

Cause: Ahrefs API response format might have changed or code accessing incorrect data paths.

Solution: Inspect raw API response via n8n execution logs. Adjust JSON paths and fallback values in the JavaScript extract code node accordingly.

Pre-Production Checklist βœ…

  • Verify Google Gemini API credentials and connection.
  • Confirm RapidAPI Ahrefs API key validity and access rights.
  • Test chat/message trigger webhook with sample keywords.
  • Validate that the keyword extraction node cleanly outputs a single, correct keyword.
  • Execute the HTTP request and inspect API response for correct data structure.
  • Run the JavaScript code node and verify it extracts expected fields.
  • Ensure the formatting AI node outputs clean, readable summaries.
  • Run end-to-end test triggering the workflow fully and check for errors.

Deployment Guide

Activate the workflow in n8n by switching it on from the main canvas screen. Confirm that your chat service points to the correct webhook URL.

Monitor execution logs initially for any API or data errors. The retry settings in the HTTP Request node help handle intermittent API downtime.

For scaling, consider rate limits on RapidAPI and API costs. You can extend this workflow by connecting the formatted output to email, Slack, or Google Sheets for reporting.

FAQs

Can I replace Google Gemini with another AI model?

Yes, you can swap the Google Gemini Chat Model nodes with other LangChain supported AI providers in n8n, such as OpenAI GPT variants, as long as you adjust the prompt accordingly.

Does this workflow consume many API credits?

It depends on your usage volume. Each keyword query triggers one Ahrefs API request, so costs accumulate based on your RapidAPI pricing plan.

Is my keyword research data safe?

Yes, all data flows within your n8n instance and secure API connections. If self-hosted, you control all data privacy.

Can this workflow handle bulk keyword queries?

This workflow is designed for single keyword queries via chat. Bulk processing would require modifications to handle arrays of keywords and batch API requests.

Conclusion

By implementing this Ahrefs Keyword Research Workflow, Lisa and SEO professionals like her can dramatically cut down research time from hours to minutes. The combination of AI for query cleaning and the power of Ahrefs API ensures accurate, comprehensive keyword insights delivered in user-friendly summaries.

With this foundation, consider extending your automation to report generation, content calendar creation, or integrating competitor keyword tracking for even deeper SEO mastery.

Start building this workflow today, and turn your keyword research from a tedious task into an efficient, intelligent process.

Promoted by BULDRR AI

Related Workflows

Automate Viral UGC Video Creation Using n8n + Degaus (Beginner-Friendly Guide)

Learn how to automate viral UGC video creation using n8n, AI prompts, and Degaus. This beginner-friendly guide shows how to import, configure, and run the workflow without technical complexity.
Form Trigger
Google Sheets
Gmail
+37
Free

AI SEO Blog Writer Automation in n8n (Beginner Guide)

A complete beginner guide to building an AI-powered SEO blog writer automation using n8n.
AI Agent
Google Sheets
httpRequest
+5
Free

Automate CrowdStrike Alerts with VirusTotal, Jira & Slack

This workflow automates processing of CrowdStrike detections by enriching threat data via VirusTotal, creating Jira tickets for incident tracking, and notifying teams on Slack for quick response. Save hours daily by transforming complex threat data into actionable alerts effortlessly.
scheduleTrigger
httpRequest
jira
+5
Free

Automate Telegram Invoices to Notion with AI Summaries & Reports

Save hours on financial tracking by automating invoice extraction from Telegram photos to Notion using Google Gemini AI. This workflow extracts data, records transactions, and generates detailed spending reports with charts sent on schedule via Telegram.
lmChatGoogleGemini
telegramTrigger
notion
+9
Free

Automate Email Replies with n8n and AI-Powered Summarization

Save hours managing your inbox with this n8n workflow that uses IMAP email triggers, AI summarization, and vector search to draft concise replies requiring minimal review. Automate business email processing efficiently with AI guidance and Gmail integration.
emailReadImap
vectorStoreQdrant
emailSend
+12
Free

Automate Email Campaigns Using n8n with Gmail & Google Sheets

This n8n workflow automates personalized email outreach campaigns by integrating Gmail and Google Sheets, saving hours of manual follow-up work and reducing errors in email sequences. It ensures timely follow-ups based on previous email interactions, optimizing communication efficiency.
googleSheets
gmail
code
+5
Free