Vector Centroid Calculation API Endpoint - n8n Workflow

Build a high-performance mathematical service using n8n. This n8n workflow calculates the centroid of a set of vectors via a webhook n8n trigger, ensuring data validation and efficient computation using a custom n8n node.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Machine learning engineers needing to calculate centroids for clustering or anomaly detection outside of main models.

  • Data scientists looking for lightweight microservices hosted on n8n.

  • Developers needing robust mathematical API endpoints without standing up traditional backend servers.

  • Users searching for practical n8n templates involving mathematical logic and custom scripting.

Overview

When building complex applications or integrating machine learning components, quickly calculating vector centroids is often necessary. This specialized n8n workflow solves the complexity of setting up a dedicated API service by leveraging the power of n8n.

This particular n8n workflow acts as a serverless function, accepting a set of vectors, validating their dimensions, calculating the exact centroid, and returning the result instantly. It demonstrates how powerful the combination of the Webhook n8n trigger and a Code n8n node can be for creating custom utilities. This approach saves significant development time compared to traditional backend setup, making it one of the most useful n8n templates for technical users.

How it Works

The operation of this n8n workflow is sequential and highly focused on validation and computation.


  1. Initiation via n8n Trigger: The process begins when a client sends a GET request to the Webhook path /centroid. This webhook n8n trigger captures the incoming query parameters, specifically looking for the array of vectors.

  2. Data Extraction: The subsequent n8n node (Set) extracts the vectors array from the incoming query data ($json.query.vectors), preparing it for computational analysis.

  3. Validation and Calculation: The core logic resides within the custom Code n8n node. This node first validates the input, ensuring that the vectors are a valid array and that all vectors within the array share the exact same number of dimensions. If validation fails, it returns a descriptive error message. If successful, it performs the required mathematical computation to calculate the centroid, which is the average of all vectors.

  4. Final Response: Finally, the Respond to Webhook n8n node takes the output (either the calculated centroid or the error object) and sends it back immediately as the response to the initial request. This completes the loop of the n8n workflow.

Installation Guide

To deploy this calculation service using this n8n workflow, follow these steps:


  1. Import: Copy the provided JSON data and import it directly into your n8n instance via the 'New' menu -> 'Import from JSON'. This provides you with the full n8n workflow structure.

  2. Activate Webhook: Locate the 'Receive Vectors' n8n trigger node. Upon saving the workflow, n8n will provide a unique URL for the webhook. Note this URL.

  3. Setup Credentials: This specific n8n workflow does not require external credentials (like API keys for OpenAI or Google Sheets) as the core logic is handled internally by the Code n8n node.

  4. Test the n8n template: Execute the n8n workflow manually or by calling the generated webhook URL with an example set of vectors in the query parameters (e.g., .../centroid?vectors=[[2,3],[4,5]]).

Node Details

Receive Vectors (Webhook): This is the entry point and primary n8n trigger. It is configured to listen on the path centroid. It captures the vector data sent in the query parameters of the inbound request.
Extract & Parse Vectors (Set): This data manipulation n8n node ensures that the array of vectors is cleanly extracted from the raw webhook query data and assigned to a variable named vectors for the next processing step.
Validate & Compute Centroid (Code): This critical n8n node runs custom JavaScript code. It handles the validation of vector dimensions and performs the averaging calculation to find the centroid. It ensures the reliability of the n8n workflow by handling error conditions like invalid or inconsistent input.
Return Centroid Response (Respond to Webhook): The final n8n node sends the result back to the client. This is essential for turning the entire n8n workflow into a functional, synchronous API endpoint.

Related n8n Workflows

Free

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

Automation consultant with over 10 years of experience specializing in AI, no-code, and workflow optimization. I’ve delivered tailored AI and NLP solutions across real estate, healthcare, and more, enhancing efficiency and customer experiences. Proficient in tools like Make, Airtable, and Zapier, I also integrate GPT models to create scalable, innovative automations. Contact me to discuss custom n8n workflows or advanced automations to streamline your processes.

Featured*