How to Validate TOTP Codes with n8n Using Python Code Node

Learn how to validate 6-digit TOTP codes using a Python Code node in n8n without creating credentials. This workflow solves the need to verify 2FA codes efficiently and securely in an automated process.
manualTrigger
code
if
+2
Workflow Identifier: 2478
NODES in Use: Manual Trigger, Set, Code, IF, Sticky Note
Validate TOTP codes with n8n and Python

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What This Workflow Does

This workflow checks if a 6-digit TOTP code matches the secret key using Python, inside n8n.
It solves the problem of manual, slow, possibly unsafe TOTP code validation.
The result is a clear status showing if the code is correct or not.


Who Should Use This Workflow

This is for users who want to verify TOTP 2FA codes securely and quickly without outside tools.
It is good for anyone managing user authentication, especially where manual checking wastes time or causes errors.


Tools and Services Used

  • n8n Automation Platform: Runs the workflow and nodes.
  • Python Code Node: Runs Python script to decode secrets and check TOTP.
  • Manual Trigger Node: Starts the workflow by user action.
  • Set Node: Holds example TOTP secret and code.
  • IF Node: Decides if the code is valid or not.

Inputs, Processing Steps, and Outputs

Inputs

The workflow starts with user trigger.
Example inputs are set: a sample 6-digit code and a base32-encoded TOTP secret key.

Processing Steps

The Python Code node decodes the secret from base32.
Then it calculates the current TOTP code using HMAC-SHA1 and a 30-second time window.
It compares the generated code to the user input.
The node returns a status: 1 if codes match, 0 if not.
The IF node checks this status to branch the flow.

Outputs

Outputs split into two possibilities: valid or invalid TOTP code.
The valid branch can proceed to allow authentication.
The invalid branch can block access or notify users.


Beginner Step-by-Step: How to Use in n8n

Step 1: Import the Workflow

  1. Download the workflow file from this page.
  2. Open the n8n editor where the workflow will run.
  3. Use the Import from File option to add the downloaded workflow.

Step 2: Configure the Workflow

  1. Check if any credentials or API Keys are needed and add them.
  2. If the workflow connects to other services, update emails, IDs, or channels accordingly.
  3. Make sure the example TOTP secret and code fields match expected format or replace with your own test data.

Step 3: Test and Activate

  1. Run the workflow by clicking Execute Workflow.
  2. Watch the result to confirm if status returns 1 for valid code or 0 for invalid code.
  3. Once tests succeed, activate the workflow to use in production.
  4. For ongoing use, replace the manual trigger with real input sources like webhooks or database queries.

Use self-host n8n if hosting n8n on your own server.


Common Edge Cases and Failures

Incorrect system time can cause valid codes to fail validation.
Make sure the server clock is accurate and synced with internet time.

Secrets must be base32 encoded correctly or decoding will fail.
Check secret strings for typos or missing characters.

Python environment in n8n must support required modules like hmac, hashlib, base64.
Errors in the code node usually mean environment setup issues.


Customization Ideas

  • Replace the Set node’s example secret and code with data from a database or form submission.
  • Add more checks in the IF node for different status codes or error messages.
  • Connect true or false branches to notification nodes like Email or Slack to alert admin or users.

Summary

✓ Validate TOTP codes inside n8n without external apps.
✓ Automate code checking using a Python script.
✓ Avoid manual errors and save time on user authentication.
✓ Control workflow branches based on valid or invalid status.
→ Ready to use with manual triggers or connect to live user data.


Validate TOTP codes with n8n and Python

Visit through Desktop to Interact with the Workflow.

Author
Written By
Vikash Kumar
Building AI agents, n8n workflows and end-to-end automation for 30+ Brands across India, the US, Europe, Dubai & Australia. 7+ years of Experience saving founders real hours every week - no code required.
Author
Written By
Vikash Kumar
Building AI agents, n8n workflows and end-to-end automation for 30+ Brands across India, the US, Europe, Dubai & Australia. 7+ years of Experience saving founders real hours every week - no code required.

Frequently Asked Questions

Yes. Replace the Set node example values with user secrets and codes from a database or form input.
No. The Python code runs locally inside n8n to calculate and verify TOTP codes.
Errors happen if the Python environment lacks necessary modules or if the secret is not valid base32.
Ensure the n8n instance has enough resources and use proper concurrency settings for handling multiple requests.

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.