Asynchronous AI Generation Polling Loop with Replicate - n8n Workflow

Use this robust n8n workflow to handle asynchronous AI tasks (like Replicate Moss-TTSD) by implementing a crucial polling loop. This n8n template ensures you only proceed once the job succeeds.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • AI developers or content creators who use Replicate models for long-running generation tasks.

  • Users looking for a reliable n8n workflow template to handle asynchronous API calls.

  • Automation specialists needing to implement robust core logic flow control, including polling loops, within n8n.

Overview

Handling long-running tasks in automation, such as complex AI model generations, requires asynchronous logic. This specific n8n workflow solves the common challenge of waiting for an external service (Replicate) without freezing the entire execution. By using an n8n node loop, this n8n template initiates the Dessix Moss-TTSD model (or similar) generation and then enters a polling sequence. This methodology, integral to professional n8n implementations, ensures that resources are not wasted, and the workflow only continues once the external process returns a successful status. This core logic flow control is a powerful feature of the n8n platform, making this n8n workflow highly reusable for any API that relies on background processing.

How it Works

This automation starts with the 'On clicking 'execute'' n8n trigger, designed for manual testing or scheduled execution.


  1. Initialization: The workflow first uses an n8n node to securely set the Replicate API key as a reusable variable.

  2. Prediction Creation: An HTTP Request n8n node sends a POST request to the Replicate API to initiate the AI generation job (using the specific model version).

  3. ID Extraction: A Code n8n node, 'Extract Prediction ID,' parses the initial API response to retrieve the unique predictionId and constructs the URL required for polling.

  4. Polling Start & Wait: The workflow enters the asynchronous loop via the 'Wait' n8n node, pausing the execution for 2 seconds to prevent excessive API calls.

  5. Status Check: Another HTTP Request n8n node, 'Check Prediction Status,' retrieves the current job status using the extracted prediction URL and the API key.

  6. Conditional Check (Core Logic Flow Control): An If n8n node determines if the job status is 'succeeded'. If it is not successful, the workflow follows the FALSE path and loops back to the 'Wait' n8n node, restarting the polling cycle. This loop is the heart of the asynchronous n8n workflow.

  7. Result Processing: If the job succeeds, the workflow follows the TRUE path to the 'Process Result' Code n8n node, which cleans and structures the final output, typically providing the resulting asset URL (e.g., speech file or text output).

Installation Guide

To utilize this highly efficient n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON code and import it directly into your n8n instance via the 'Workflows' section using the 'New' button and selecting 'Import from JSON'.

  2. Set Credentials: This n8n template uses a generic HTTP Header authentication strategy, but the API key itself is set within the 'Set API Key' n8n node.

  3. Configure API Key: Open the 'Set API Key' n8n node and replace the placeholder YOURREPLICATEAPIKEY with your actual Replicate API key. Ensure this key is prefixed with 'Bearer ' within the HTTP Header definitions of the subsequent HTTP Request nodes, as defined in the expression: ={{ 'Bearer ' + $('Set API Key').item.json.replicateapi_key }}.

  4. Test: Execute the n8n workflow manually using the 'Execute Workflow' button to verify the polling logic and API connection.

Node Details

On clicking 'execute' (n8n trigger): The starting point of the n8n workflow. It is a manual trigger used to initiate the generation process for demonstration and testing purposes.
Set API Key (n8n node): Essential for security. It defines the replicateapikey variable, making the API key easily accessible to subsequent HTTP Request nodes via expressions.
Create Prediction (HTTP Request n8n node): Initiates the long-running task by sending a POST request. It uses the Replicate API and includes the required model version and input parameters.
Extract Prediction ID (Code n8n node): Parses the initial JSON response to isolate the crucial id of the prediction, which is required for monitoring the status. This custom n8n node prepares the data structure for the polling loop.
Wait (n8n node): Implements a necessary delay (2 seconds) in the polling loop, preventing rate-limiting issues with the external API while waiting for the AI generation to complete. This is key to a stable n8n workflow.
Check Prediction Status (HTTP Request n8n node): Repeatedly called within the polling loop using the prediction URL derived in the previous steps. It retrieves the current status of the AI job.


  • Check If Complete (If n8n node): The core logic control gate. It checks if $json.status is equal to 'succeeded'. If not, the flow loops back to the 'Wait' n8n node. If true, the n8n workflow proceeds to process the final output.

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*