Replicate Competitor Ads with n8n – Technical & Ethical Implementation Guide
This guide documents a production-ready n8n workflow template (The Recap AI – Facebook Ad Thief) that automates the process of analyzing Facebook ads and generating new ad images that feature your own product. It explains the workflow architecture, node-by-node behavior, configuration requirements, and ethical constraints so that advanced users can deploy and adapt it safely.
1. Workflow Overview
The n8n template automates a full pipeline from Facebook Ad Library URL input to generated ad creatives stored in Google Drive. At a high level, the workflow:
- Receives a Facebook Ad Library URL and a product image via a form trigger.
- Invokes an Apify actor to scrape ad metadata and images from the provided URL.
- Downloads the reference ad images, archives them to Google Drive, and converts them to base64.
- Combines the reference images with your product image to build a structured prompt.
- Calls Google Gemini generative endpoints through HTTP Request nodes to:
- Generate or refine an image prompt.
- Create new ad images that visually resemble the reference ad but feature your product and branding.
- Performs prohibited-content checks on the model output.
- Converts the generated base64 images back to binary and uploads them to Google Drive.
The objective is to accelerate creative experimentation and A/B testing by programmatically generating ad variants that are inspired by competitor ads, while intentionally replacing competitor branding with your own.
2. System Architecture & Data Flow
The workflow orchestrates multiple external services and n8n nodes. The data flow is roughly:
- Input collection
- User submits:
- Facebook Ad Library URL (source ad).
- Product image (your brand asset).
- User submits:
- Ad scraping
- n8n calls an Apify Facebook Ad Library scraper actor with the URL.
- Actor returns ad metadata and image URLs.
- Reference image handling
- Workflow downloads the ad images.
- Uploads them to a designated Google Drive folder for archival.
- Converts images to base64 for use in Gemini API requests.
- Prompt construction
- Product image is also converted to base64.
- Reference and product images are aggregated.
- A structured text prompt is generated to instruct Gemini how to:
- Replace competitor branding.
- Preserve layout and style.
- Maintain ad copy and calls to action, except for brand-specific elements.
- Image generation
- HTTP Request nodes call Google Gemini (gemini-2.5 endpoints) to:
- Draft or refine the image-generation prompt.
- Generate one or more ad image variants.
- HTTP Request nodes call Google Gemini (gemini-2.5 endpoints) to:
- Safety checks
- Response is inspected for prohibited content flags.
- Flagged outputs are not stored or used.
- Output storage
- Valid generated images are decoded from base64.
- Binary files are created and uploaded to an output folder in Google Drive.
3. Node-by-Node Breakdown
3.1 Entry & Input Handling
form_trigger
- Role: Primary trigger for the workflow.
- Inputs collected:
- Facebook Ad Library URL (string field).
- Product image (file upload field).
- Behavior: When a user submits the form, n8n starts the workflow and passes both the URL and the file reference to downstream nodes.
convert_product_image_to_base64
- Role: Convert the uploaded product image into a base64-encoded string.
- Reason: Image-generation APIs typically accept image data as base64 within JSON payloads rather than as multipart file uploads.
- Output: A base64 string representing your product image, used later in prompt construction and Gemini requests.
3.2 Scraping & Reference Image Preparation
scrape_ads (Apify actor)
- Type: Apify integration node calling a Facebook Ad Library scraper actor.
- Credentials: Requires configured Apify OAuth credentials.
- Key parameters:
- Actor ID: Set to the Facebook Ad Library scraper actor ID.
- Input: Includes the Facebook Ad Library URL from
form_trigger. - limitPerSource: Controls the number of ads scraped per URL.
- Output: JSON structure containing:
- Ad metadata (e.g., creative details, cards).
- Image URLs for each scraped ad.
- Notes:
- If the URL is invalid or the actor fails, this node will error. Handle by testing with a single known-good URL first.
- Respect Apify usage limits and Facebook terms of service.
iterate_ads
- Role: Iterate through each ad returned by the Apify actor.
- Behavior:
- Loops over the list of scraped ads.
- For each ad, extracts the primary image URL or card image URL.
- Output: One execution per ad, with ad-level context passed to downstream nodes.
download_image
- Type: HTTP Request or dedicated file-download node, depending on template version.
- Role: Download the reference image from the ad image URL.
- Output: Binary image data for the reference ad image.
- Edge cases:
- Broken or expired URLs will cause download failures.
- Non-image MIME types should be treated as errors or filtered out.
upload_ad_reference (Google Drive)
- Role: Archive the downloaded reference ad image to Google Drive.
- Credentials: Uses Google Drive OAuth credentials.
- Configuration:
- Target folder: A dedicated folder for reference ads.
- File name: Often derived from ad ID or timestamp for traceability.
- Purpose: Maintain a record of the original reference creatives for auditing and later review.
convert_ad_image_to_base64
- Role: Convert each downloaded reference ad image into base64.
- Output: Base64-encoded string for each reference ad image, used as part of the Gemini prompt context.
3.3 Aggregation & Prompt Construction
aggregate
- Role: Aggregate multiple inputs:
- Product image (base64) from
convert_product_image_to_base64. - Reference ad images (base64) from
convert_ad_image_to_base64.
- Product image (base64) from
- Behavior:
- Combines data into a single structure that can be used to construct a unified prompt.
- May limit the number of reference images to avoid very large payloads.
build_prompt
- Role: Generate the textual instructions that will be sent to the image-generation model.
- Prompt strategy:
- Brand replacement rules:
- Explicitly instruct the model to replace competitor brand text (for example, “AG1”) with your brand (for example, “ThriveMix”).
- Include partial-token handling such as “A”, “AG”, or “G1” so that partial branding is also removed or replaced.
- Style and layout preservation:
- Describe lighting, shadows, color grading, and overall mood to match the original ad.
- Instruct the model to maintain composition and layout where possible.
- Product placement rules:
- If multiple products appear in the reference image, instruct the model to place your product in the primary, visually dominant position.
- Copy and CTA handling:
- Retain the original ad copy and call-to-action structure.
- Replace only the brand names and brand-specific terms with your own.
- Brand replacement rules:
- Output: A finalized text prompt string, plus references to the base64 images that will be provided to Gemini.
3.4 Gemini Integration (Prompt & Image Generation)
generate_ad_image_prompt (Google Gemini)
- Type: HTTP Request node.
- Role: Call Gemini generative language or multimodal endpoint to refine or validate the prompt before image generation.
- Endpoint: Configured to use gemini-2.5 API endpoints.
- Authentication:
- Uses a Google Cloud API key or similar credential configured in n8n via httpHeaderAuth.
- Behavior:
- Sends the constructed prompt and relevant context.
- Receives a refined prompt or additional structured instructions to improve final image quality.
generate_ad_image (Google Gemini)
- Type: HTTP Request node.
- Role: Request final image generation from Gemini using the refined prompt and base64 images.
- Endpoint: Gemini image-generation endpoint for the gemini-2.5 model family.
- Inputs:
- Text prompt from
build_promptorgenerate_ad_image_prompt. - Base64-encoded product and reference images.
- Text prompt from
- Output:
- Base64-encoded generated image(s).
- Metadata that may include safety or content flags.
- Edge cases:
- Large base64 payloads can cause request size issues.
- Unsupported image dimensions or formats may result in API errors.
3.5 Safety & Output Storage
check_if_prohibited
- Role: Evaluate Gemini’s response for prohibited or sensitive content.
- Inputs:
- Model safety metadata or flags from the Gemini response.
- Behavior:
- If the result indicates disallowed content, the workflow branches to a path that skips storage and downstream use.
- Otherwise, execution continues to image decoding and upload.
- Purpose: Enforce platform and internal safety policies before any asset is persisted or used in campaigns.
get_image
- Role: Convert base64-encoded generated images back into binary format.
- Output: Binary image files suitable for upload to Google Drive or other storage systems.
upload_image (Google Drive)
- Role: Store generated ad images in a dedicated Google Drive folder.
- Credentials: Uses the same Google Drive OAuth credentials as the reference upload node.
- Configuration:
- Output folder: A separate folder for generated creatives, distinct from reference ads.
- File naming: Can include timestamp, campaign ID, or source ad identifier.
- Result: A collection of generated ad images ready for manual review and potential deployment.
4. Setup & Configuration Checklist
4.1 Prerequisites
- Running n8n instance (self-hosted or cloud).
- Access to:
- Apify account with Facebook Ad Library scraper actor.
- Google Drive API with OAuth credentials.
- Google Cloud project with access to Gemini generative endpoints.
4.2 Installation Steps
- Import the template
- Install n8n.
- Import the provided workflow JSON into your n8n canvas.
- Configure Apify
- Create or configure Apify OAuth credentials in n8n.
- Open the
scrape_adsnode and set:- Actor ID to the Facebook Ad Library scraper actor.
- Any required input fields for the actor (including the URL from
form_trigger).
- Set limitPerSource to control how many ads you scrape per run.
- Configure Google Drive
- Create Google Drive OAuth credentials in n8n.
- In
upload_ad_reference, set:- Credential to the new Google Drive OAuth.
- Folder ID or path for storing reference ads.
- In
upload_image, set:- Same or different credential, as needed.
- Folder ID or path for storing generated creatives.
- Configure Gemini API access
- In Google Cloud, provision an API key or appropriate credential that can call Gemini gemini-2.5 endpoints.
- In n8n, create an HTTP Header Auth or
