Asynchronous AI Generation Using Replicate Polling Loop - n8n Workflow

Use this comprehensive n8n workflow to automate content generation with the jfirma1/test_model on Replicate. This n8n template includes a custom polling loop for reliable results.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI Developers & Integrators: Users who need to incorporate long-running, asynchronous AI models (like those hosted on Replicate) into their standard business processes.
n8n Power Users: Individuals looking for advanced examples of using the Code n8n node and flow control structures (If and Wait) to handle external APIs.
Content Automation Specialists: Anyone seeking reliable n8n templates for generating creative content based on prompts.
Technical Automators: Users needing a robust n8n trigger mechanism to start complex compute jobs.

Overview

Generating complex content often requires running jobs asynchronously, meaning the initial request immediately returns a status but the final output is delayed. This robust n8n workflow solves this by implementing a custom, non-blocking polling sequence tailored for the Replicate API.

Unlike simpler integrations, this n8n template ensures reliability by initiating the prediction, extracting the job ID, and then continuously checking the status every two seconds until the job succeeds. The use of the httpRequest n8n node, combined with data manipulation in the Code n8n node, provides a powerful and reusable framework for integrating any asynchronous service into your n8n environment. This structure is essential for building mission-critical automations using n8n.

How it Works

This powerful n8n workflow starts with a manual n8n trigger:


  1. Initialization: The workflow is manually executed via the 'On clicking 'execute'' n8n trigger. Immediately afterward, the 'Set API Key' n8n node securely injects your Replicate API key as a variable for subsequent HTTP requests.

  2. Start Prediction: The 'Create Prediction' n8n node (an HTTP Request) sends a POST request to Replicate, specifying the model version and the required input prompt. This kicks off the asynchronous AI job.

  3. Extraction: The response from the creation request contains the job ID. The 'Extract Prediction ID' Code n8n node parses this response, storing the prediction ID and the URL needed for polling.

  4. Polling Loop Entry: The execution enters the polling loop. It first hits the 'Wait' n8n node, pausing the entire n8n workflow for 2 seconds to avoid excessive API calls.

  5. Status Check: The 'Check Prediction Status' httpRequest n8n node then queries the previously extracted prediction URL to retrieve the current status of the AI job.

  6. Conditional Check: The 'Check If Complete' If n8n node evaluates the status. If the status equals 'succeeded' (True branch), the job is done, and the n8n workflow proceeds to process the result. If the status is anything else (False branch), the connection loops back to the 'Wait' n8n node, restarting the polling process until completion.

  7. Result Processing: Once successful, the 'Process Result' Code n8n node extracts the final generated content (often a URL to the asset) and formats it neatly for downstream use in your n8n workflow.

Installation Guide

To use this n8n workflow template, follow these steps:


  1. Import: Copy the provided JSON code and import it directly into your n8n instance.

  2. API Key Setup (Replicate): Locate the 'Set API Key' n8n node.

* Replace the placeholder value YOURREPLICATEAPI_KEY with your actual API key from Replicate.

  1. Configure Model Input: Locate the 'Create Prediction' n8n node. Within its JSON Body configuration, modify the prompt value and any other input parameters (like width, height, etc.) to match your desired generation requirements. Ensure the model version in the JSON body is correct.

  2. Execution: Run the n8n workflow manually using the 'Execute Workflow' button on the 'On clicking 'execute'' n8n trigger node to test the integration and polling loop.

Node Details

On clicking 'execute' (Manual Trigger): Serves as the starting n8n trigger for the entire n8n workflow, allowing manual execution for testing and configuration.
Set API Key (Set): A critical n8n node used to securely define the Replicate API key, making it accessible as an expression ($($Set API Key).item.json.replicateapikey) throughout the rest of the n8n workflow.
Create Prediction (HTTP Request): Sends the initial asynchronous POST request to the Replicate API, starting the AI generation job. It includes custom headers for authorization using the API key defined earlier.
Extract Prediction ID (Code): A custom Code n8n node crucial for reading the response from the initial prediction request. It extracts the unique predictionId and constructs the necessary polling URL for subsequent requests in the n8n workflow.
Wait (Wait): Manages flow control by pausing the n8n workflow for a set duration (2 seconds), preventing rapid-fire API requests during the polling loop.
Check Prediction Status (HTTP Request): Performs a GET request using the extracted prediction URL to check the status of the running AI job. This is the core polling step in this n8n template.
Check If Complete (If): A flow control n8n node that directs the n8n workflow logic. If the status is 'succeeded', it proceeds to the result handler; otherwise, it returns to the 'Wait' n8n node to continue polling.
Process Result (Code): The final Code n8n node that executes only upon success. It structures and extracts the finalized output, including metrics and the generated asset URL, cleaning the data for the next steps in your larger n8n workflow.

Related n8n Workflows

Free

Nodes: 7 Nodes
Updated: December 26 2025
View all
Created by

Building AI Agents and Automations | Growth Marketer | Entrepreneur | Book Author & Podcast Host If you need any help with Automations, feel free to reach out via linkedin: https://www.linkedin.com/in/yaronbeen/ And check out my Youtube channel: https://www.youtube.com/@YaronBeen/videos

Featured*