Secure Human-in-the-Loop Approvals with Database Logging and Messaging - n8n Workflow

Use this secure n8n workflow template to create HMAC-signed, time-limited approval links sent via Telegram. Logs all decisions in a Postgres database. This sophisticated n8n node setup ensures security and auditing for critical operations.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Security-Conscious Organizations: Teams requiring cryptographically verified, time-limited approval steps. Operations & Audit Teams: Users needing a full audit trail of all manual decisions (approvals, rejections, expirations) logged in a database.* Automation Specialists: Developers looking for a robust, production-ready n8n workflow example demonstrating secure webhook handling and PostgreSQL interaction.

Overview

Critical business processes often require a human manager to approve or reject an automated action. This sophisticated n8n workflow solves the inherent security risk of simple approval links by enforcing high standards: HMAC signatures and Time-to-Live (TTL) expiration checks. When a manager clicks the link provided via Telegram, the n8n trigger immediately captures the request. Crucially, the workflow verifies the signature using a hidden SECRET_KEY environment variable. If valid and not expired, the workflow proceeds to update the ticket status in PostgreSQL, logs the transaction in an audit table, and notifies the original user via Telegram. This reliable n8n template ensures that every step is secure, auditable, and logged, providing peace of mind for high-stakes automations.

How it Works

This secure n8n workflow is initiated by an external click on an approval link.


  1. Trigger Reception (01 Webhook Trigger): The process starts when a manager clicks a signed URL, activating the dedicated webhook n8n trigger (/approval).

  2. Signature Verification (02 FN: Verify Signature + TTL): This critical Python Code n8n node performs two security checks: it verifies the HMAC-SHA256 signature against the correlation ID, status, and expiry time (exp), and confirms the link has not expired. It determines the ultimate action (approve, reject, expired, or invalid).

  3. Action Routing (Actions): A Switch n8n node routes the flow based on the security verification result.

  4. Approval Path: If approved, the workflow updates the ticket status in the tickets table (04c DB: Update Ticket Status). A detailed audit record is then inserted into the ticketaudit table. Finally, the ticket owner's chat ID is retrieved, and they are notified via Telegram about the status change.

  5. Rejection Path: If rejected, the system logs the rejection action in the audit table (Execute a SQL query1) and sends a confirmation notification via Telegram.

  6. Security Failure Paths (Expired/Invalid): If the link is expired or the signature is invalid, the action is logged immediately in the Postgres audit table (04e/04i DB: Insert Audit) and an alert is sent to the administrators via Telegram, ensuring quick detection of security incidents. This detailed logging is a key feature of this reliable n8n workflow.

  7. Error Monitoring: Any execution failure within this robust n8n workflow is captured and logged into a dedicated workflowerrors table in Postgres for easy monitoring and debugging.

Installation Guide

To deploy this powerful n8n workflow template, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON and import it directly into your n8n instance using the 'Import workflow' function.

  2. Environment Variable Setup: This n8n workflow relies on a secure environment variable. Add SECRETKEY=your-long-random-secret to your n8n deployment's environment file (.env) and restart n8n.

  3. PostgreSQL Credentials: Configure a Postgres credential (named 'Postgres account' or update the references in all Postgres n8n node instances) pointing to your database.

Database Structure:* Ensure you have the required tickets, ticketaudit, and workflowerrors tables created in your Postgres instance, as detailed in the sticky notes within the workflow structure.

  1. Telegram Credentials: Set up a Telegram API credential (used by all Telegram n8n node instances) with your bot token.

  2. Update Placeholders: Review all Telegram n8n node instances. Replace fixed chatid placeholders (used for admin alerts on invalid attempts) with your actual Telegram manager IDs. The user-facing notifications use dynamic expressions like ={{ $json.chat_id }}.

  3. Activate: Save and activate the n8n workflow. The Webhook n8n trigger is now listening for secure decisions.

Node Details

This n8n template leverages several powerful n8n node types for security and data handling:

01 Webhook Trigger: Approval Decision (Webhook):
Function: Serves as the entry point, listening for incoming decisions via the URL path /approval.
Key Configuration: Response data set to a simple acknowledgment message.
02 FN: Verify Signature + TTL (Code - Python):
Function: The core security n8n node. It verifies the HMAC signature and checks the Time-To-Live (TTL) of the link against the current time. It relies on the SECRETKEY environment variable.
Key Configuration: Uses standard Python libraries (hashlib, hmac, time, os.getenv) to perform cryptographic checks.
04c DB: Update Ticket Status (Postgres):
Function: Executes an UPDATE SQL command to change the status of the ticket based on the approval action.
Key Configuration: Uses expressions to dynamically inject the correlationid and the newstatus into the SQL query.
04c2 DB: Insert Audit Row / 04e DB: Insert Audit Expired / 04i DB: Insert Audit Invalid (Postgres):
Function: Dedicated Postgres n8n node instances responsible for logging every decision, rejection, expiry, or invalid attempt into the ticketaudit table, ensuring a complete audit trail for this n8n workflow.
05c Telegram: Update Confirmation (Telegram):
Function: Sends notifications to the relevant Telegram chat ID, confirming the action taken (e.g., status updated or rejection noted).
Key Configuration: Uses HTML Parse Mode for formatting the status updates.

Related n8n Workflows

Free

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

Automation consultant specializing in n8n workflows that save businesses time and reduce costs. Experienced in designing ticketing systems, email parsers, and integrations with Postgres, Telegram, and APIs. I publish workflows to help teams streamline operations and build internal efficiency.

Featured*