Skip to main content

Create a secure MongoDB data retrieval API with input validation and HTTP responses

Workflow preview

Workflow preview
100%
Create a secure MongoDB data retrieval API with input validation and HTTP responses preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

Data Extraction from MongoDB Overview This workflow exposes a public HTTP GET endpoint to read all documents from a MongoDB collection, with: Strict validation of the collection name Error handling...

Best for

  • Engineering automation workflows
  • Multimodal AI automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.webhook, n8n-nodes-base.mongodb, n8n-nodes-base.respondtowebhook, n8n-nodes-base.stickynote, n8n-nodes-base.if, n8n-nodes-base.code

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Samuel Heredia.

Original n8n.io source

1.1 Workflow description

Title
Create a secure MongoDB data retrieval API with input validation and HTTP responses
Workflow name
Create a secure MongoDB data retrieval API with input validation and HTTP responses

Data Extraction from MongoDB

Overview This workflow exposes a public HTTP GET endpoint to read all documents from a MongoDB collection, with:

  • Strict validation of the collection name

  • Error handling with proper 4xx codes

  • Response formatting (e.g., _id → id) and a consistent 2XX JSON envelope

Workflow Steps

  1. Webhook Trigger: A public GET endpoint receives requests with the collection name as a parameter.

The workflow begins with a webhook that listens for incoming HTTP GET requests.

The endpoint follows this pattern:

https://{{your-n8n-instance}}/webhook-test/{{uuid&gt}}/:nameCollection

The :nameCollection parameter is passed directly in the URL and specifies the MongoDB collection to be queried.

Example:

https://yourdomain.com/webhook-test/abcd1234/orders

would attempt to fetch all documents from the orders collection.

  1. Validation: The collection name is checked against a set of rules to prevent invalid or unsafe queries.

Before querying the database, the collection name undergoes validation using a regular expression:

^(?!system\.)[a-zA-Z0-9._]{1,120}$

Purpose of validation:

Blocks access to MongoDB’s reserved system.* collections.

Prevents injection attacks by ensuring only alphanumeric characters, underscores, and dots are allowed.

Enforces MongoDB’s length restrictions (max 120 characters).

This step ensures the workflow cannot be exploited with malicious input.

  1. Conditional Check: If the validation fails, the workflow stops and returns an error message. If it succeeds, it continues.

The workflow checks if the collection name passes validation.

If valid ✅: proceeds to query MongoDB.

If invalid ❌: immediately returns a structured HTTP 400 response, adhering to RESTful standards:

{
  "code": 400,
  "message": "{{ $json.message }}"
}
  1. MongoDB Query: The workflow connects to MongoDB and retrieves all documents from the specified collection.

To use the MongoDB node, a proper database connection must be configured in n8n. This is done through MongoDB Credentials in the node settings:

Create MongoDB Credentials in n8n

Go to n8n → Credentials → New.

Select MongoDB and Fill in the following fields:

Host: The MongoDB server hostname or IP (e.g., cluster0.mongodb.net).

Port: Default is 27017 for local deployments.

Database: Name of the database (e.g., myDatabase).

User: MongoDB username with read permissions.

Password: Corresponding password.

Connection Type: Standard for most cases, or Connection String if using a full URI.

Replica Set / SRV Record: Enable if using MongoDB Atlas or a replica cluster.

Using a Connection String (recommended for MongoDB Atlas)

Example URI:

mongodb+srv://<username>:<password>@cluster0.mongodb.net/myDatabase?retryWrites=true&w=majority

Paste this into the Connection String field when selecting "Connection String" as the type.

Verify the Connection

After saving, test the credentials to confirm n8n can connect successfully to your MongoDB instance.

Configure the MongoDB Node in the Workflow

Operation: Find (to fetch documents).

Collection: Dynamic value passed from the workflow (e.g., {{$json["nameCollection"]}}).

Query: Leave empty to fetch all documents, or define filters if needed.

Result: The MongoDB node will retrieve all documents from the specified collection and pass the dataset as JSON to the next node for processing.

  1. Data Formatting: The retrieved documents are processed to adjust field names.

By default, MongoDB returns its unique identifier as _id.

To align with common API conventions, this step renames _id → id.

This small transformation simplifies downstream usage, making responses more intuitive for client applications.

  1. Response: The cleaned dataset is returned as a structured JSON response to the original request.

The processed dataset is returned as the response to the original HTTP request.

Clients receive a clean JSON payload with the expected format and renamed identifiers.

Example response:

[
  {
    "id": "64f13c1e2f1a5e34d9b3e7f0",
    "name": "John Doe",
    "email": "[email protected]"
  },
  {
    "id": "64f13c1e2f1a5e34d9b3e7f1",
    "name": "Jane Smith",
    "email": "[email protected]"
  }
]

Workflow Summary

Webhook (GET) → Code (Validation) → IF (Validation Check) → MongoDB (Query) → Code (Transform IDs) → Respond to Webhook

Key Benefits ✅ Security-first design: prevents unauthorized access or injection attacks.

✅ Standards compliance: uses HTTP status codes (400) for invalid requests.

✅ Clean API response: transforms MongoDB’s native _id into a more user-friendly id.

✅ Scalability: ready for integration with any frontend, third-party service, or analytics pipeline.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Webhook

Type / Role
n8n-nodes-base.webhook - webhook
Config choices
Version 2

Block 2 - MongoDB

Type / Role
n8n-nodes-base.mongoDb - mongoDb
Config choices
Version 1.2

Block 3 - Respond to Webhook

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.4

Block 4 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 5 - If

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 6 - Respond code 400

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.4

Block 7 - IDS format

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 8 - Validate Pattern

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 9 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 10 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 11 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 12 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 13 - Sticky Note5

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 14 - Sticky Note6

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 15 - Sticky Note7

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 16 - Sticky Note8

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 17 - Sticky Note9

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 18 - Sticky Note10

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 19 - Sticky Note11

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 20 - Sticky Note12

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - Sticky Note13

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Sticky Note14

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

3. Summary Table

Workflow Create a secure MongoDB data retrieval API with input validation and HTTP responses
Complexity advanced
Nodes 22
Categories Engineering, Multimodal AI
Author Samuel Heredia
Published 21 Aug 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/7674/7674.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Create a secure MongoDB data retrieval API with input validation and HTTP responses do?

Data Extraction from MongoDB Overview This workflow exposes a public HTTP GET endpoint to read all documents from a MongoDB collection, with: Strict validation of the collection name Error handling...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, Multimodal AI use case.