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:
keyworddynamically 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.