Overview
In this lesson, you will learn how to build an AI-powered content automation workflow using n8n. This workflow automatically generates SEO-optimized blog posts, social media posts for multiple platforms, and YouTube video scripts based on topics you add to a Google Sheet. By the end, you will understand how to dynamically configure prompts, use OpenAI nodes effectively, and automate content publishing tasks with minimal manual intervention.
If you're new to OpenAI integration, consider reviewing the AI Automation with n8n and OpenAI lesson first to get a solid foundation.
Setting Up the Content Automation Workflow
1. Preparing Your Google Sheets
You will use two Google Sheets to manage your content creation process:
- Content Generation Topics Sheet: This sheet holds the topics you want to generate content for, along with metadata such as audience, tone, word count, domain/niche, and status.
- AI Prompts Sheet: This sheet stores system and user prompts tailored for different content platforms like WordPress, Twitter, LinkedIn, Facebook, and YouTube.
Content Generation Topics Sheet Structure
| Topic | Audience | Tone | Word Count | Domain/Niche | Status |
|---|---|---|---|---|---|
| Top five AI tools for solopreneurs | Solopreneurs/Freelancers | Professional | 1200 | AI tools | Pending |
- Status column tracks whether content has been generated (
Pending,Completed, etc.). - You can add additional columns like
Blog Nameif managing multiple blogs.
AI Prompts Sheet Structure
| Content Platform | System Prompt | User Prompt |
|---|---|---|
| WordPress | You are a 15-year experienced professional blog writer specializing in {{domain_niche}}. | Write a blog post on the topic {{topic}} with a target word count of {{word_count}} and tone {{tone}}. |
| You are a 15-year experienced social media copywriter creating engaging Twitter threads. | Create a 5-7 tweet Twitter thread based on the blog post below, following the specified rules. | |
| You are an expert LinkedIn content strategist. | Generate LinkedIn posts based on the blog post below. | |
| You are a content marketing expert specializing in Facebook carousels. | Create a Facebook carousel post based on the blog post below. | |
| YouTube | You are a 15-year experienced YouTube content strategist and scriptwriter. | Convert the blog post below into a YouTube video outline with title, hook, timestamps, and CTA. |
Note: The prompts use placeholders wrapped in double curly braces (e.g., {{topic}}), which will be dynamically replaced with actual values from the Topics Sheet.
Building the Workflow in n8n
2. Creating the Trigger
You will trigger this workflow either manually or automatically when a new topic is added or updated in the Google Sheet.
Use the Google Sheets Trigger node configured with:
- Document: Your AI Content Generation sheet
- Sheet: Content Generation Topics
- Trigger on: Row Added (or Row Added/Updated)
- Polling interval: Every hour (or your preferred schedule)
Also add a Manual Trigger node for testing and manual runs.
3. Fetching Pending Topics
Add a Google Sheets - Get Rows node to fetch topics with status Pending:
- Use the same document and sheet (
Content Generation Topics). - Add a filter on the
Statuscolumn where value equalsPending. - Set
Return Allto false andReturn First Matching Rowto true to process one topic at a time.
This setup ensures you process topics sequentially and avoid flooding your blog with multiple posts at once.
4. Fetching AI Prompts
Add another Google Sheets - Get Rows node to fetch all AI prompts:
- Document: Same as above
- Sheet:
AI Prompts - Return all rows (all platforms)
This node will return system and user prompts for WordPress, Twitter, LinkedIn, Facebook, and YouTube.
For more details on configuring Google Sheets nodes, refer to the Google Sheets Node Documentation{:target="_blank"}.
Dynamically Replacing Placeholders in Prompts
Since the prompts contain placeholders like {{topic}}, {{word_count}}, and {{domain_niche}}, you need to replace these dynamically with actual values from the topic row.
5. Using a Code Node to Replace Placeholders
Add a Code node with the following logic:
Input:
- The row from the Topics Sheet (topic, audience, tone, word count, domain/niche)
- The rows from the AI Prompts Sheet (system prompt, user prompt)
Process:
- Iterate over each prompt row.
- Replace all placeholders in both system and user prompts with the corresponding values from the topic row.
- Output an array of prompts ready for consumption by the OpenAI nodes.
Example JavaScript snippet for placeholder replacement:
const topicData = items[0].json.topicData; // Topic row data
const prompts = items[1].json.prompts; // Array of AI prompt rows
function replacePlaceholders(text, data) {
return text.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || '');
}
const updatedPrompts = prompts.map(prompt => ({
contentPlatform: prompt.contentPlatform,
systemPrompt: replacePlaceholders(prompt.systemPrompt, topicData),
userPrompt: replacePlaceholders(prompt.userPrompt, topicData),
}));
return updatedPrompts.map(prompt => ({ json: prompt }));
Note: If you're using n8n cloud, use the Ask AI feature in the Code node to generate this script by describing what you want in plain English.
Generating Content with OpenAI Node
6. Branching Based on Content Platform
Add an IF node to check if the contentPlatform is WordPress (case-insensitive):
- If true, generate the blog post first.
- If false, generate social media posts or YouTube scripts based on the blog post content.
For mastering complex workflow branching, consider the Master Conditional Logic in n8n lesson.
7. Generating the Blog Post
Add an OpenAI - Chat Completion node configured as:
- Model:
gpt-4o-minior your preferred OpenAI model. - Messages:
- System: Use the dynamically replaced system prompt for WordPress.
- User: Use the dynamically replaced user prompt for WordPress.
- Output: Plain text containing HTML-formatted blog post content.
Refer to the OpenAI Node Documentation{:target="_blank"} for detailed configuration options.
8. Cleaning Up the Blog Post HTML
The AI output might include unwanted markdown formatting (like triple backticks). Use a Set node with expressions and JavaScript replace functions to clean the output:
const rawContent = $json["message"]["content"];
const cleanedContent = rawContent
.replace(/```html/g, '')
.replace(/```/g, '')
.trim();
return { cleanedContent };
This cleaned HTML will be ready to publish.
9. Publishing the Blog Post to WordPress
Add a WordPress node configured to:
- Operation: Create Post
- Status: Draft (recommended for review before publishing)
- Title: Extract from the blog post content or topic title (use expressions)
- Content: Use the cleaned HTML content
Set up WordPress credentials with your blog URL, username, and password.
Generating Social Media Posts and YouTube Scripts
10. Passing Blog Post Content to Other Platforms
For platforms other than WordPress, you will:
- Use the blog post content generated in step 7 as input.
- Use the corresponding system and user prompts for each platform (Twitter, LinkedIn, Facebook, YouTube).
- Send prompts to the OpenAI node iteratively for each platform.
This approach ensures social media posts and video scripts are aligned with the blog content.
11. Dynamic Iteration Over Platforms
Instead of hardcoding multiple OpenAI nodes, loop over the AI prompts array, filtering for non-WordPress platforms, and invoke a single OpenAI node in iteration mode.
For a practical example of AI content generation workflows, see the AI Blog Writer with n8n lesson.
Final Workflow Summary
- Trigger on new or updated topics marked as
Pending. - Fetch the topic details and AI prompts.
- Use a code node to replace placeholders in prompts dynamically.
- Branch for WordPress vs. social media platforms.
- Generate SEO optimized blog post in WordPress branch.
- Clean the generated HTML content.
- Publish the blog post as draft in WordPress.
- For social media platforms and YouTube:
- Generate posts/scripts based on the blog content.
- Output results for review or scheduled publishing.
- Optionally update the status in the Topics Sheet to
Completed.
Common Mistakes and Troubleshooting
- Placeholders Not Replaced: Ensure your code node correctly matches the placeholder keys to the column names in your topics sheet. Use consistent naming.
- OpenAI Node Errors: Check your API key and model selection. Use smaller models like
gpt-4o-minifor faster, cost-effective responses. - HTML Content Formatting Issues: AI might return markdown or extra characters. Always clean up the output before publishing.
- Workflow Trigger Frequency: Avoid too frequent triggers to prevent API rate limits or duplicate posts.
- Status Update Missing: Remember to add a node to update the topic status to avoid regenerating content for the same topic.
- Prompt Length Limits: Keep prompts concise but informative to avoid exceeding token limits.
For additional troubleshooting tips, consult the n8n Documentation{:target="_blank"}.
Useful n8n Documentation Links
Quick Reference Cheat Sheet
| Step | n8n Node | Key Settings |
|---|---|---|
| Trigger workflow | Manual Trigger + Google Sheets Trigger |
Document & Sheet: Content Generation Topics; Trigger on Row Added |
| Fetch pending topics | Google Sheets - Get Rows |
Filter: Status = Pending; Return first matching row |
| Fetch AI prompts | Google Sheets - Get Rows |
Sheet: AI Prompts; Return all rows |
| Replace placeholders | Code |
JavaScript to replace {{placeholders}} with topic values |
| Branch on platform | IF |
Condition: contentPlatform equals "WordPress" (ignore case) |
| Generate blog post | OpenAI - Chat Completion |
Use dynamic system and user prompts for WordPress |
| Clean blog post HTML | Set |
Replace unwanted markdown characters |
| Publish blog post | WordPress |
Operation: Create Post; Status: Draft; Title and Content from previous node |
| Generate social media posts | OpenAI - Chat Completion |
Use dynamic prompts for each platform with blog content input |
| Update status | Google Sheets - Update Row |
Set Status = Completed |
Conclusion
By following this tutorial, you can create a powerful AI-driven content automation system that generates blog posts, social media content, and video scripts with minimal manual effort. Leveraging dynamic prompts stored in Google Sheets combined with n8n’s flexible nodes and OpenAI’s language models, your content creation process becomes scalable, efficient, and customizable. Always remember to review and edit AI-generated content to maintain quality and authenticity.