Zalo OA OAuth Token Auto-Refresh and Secure Caching - n8n Workflow

Use this comprehensive n8n workflow to automatically manage and refresh Zalo OA OAuth tokens. Securely cache tokens using n8n static data for reliable, persistent API access.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Users integrating Zalo Official Accounts API services.

  • Developers requiring persistent, non-expiring authentication tokens.

  • Teams looking for robust n8n templates for critical token management.

  • n8n experts seeking advanced examples of using Workflow Static Data and custom Code nodes.

Overview

This is a critical n8n workflow designed to maintain valid API connectivity for Zalo Official Accounts (OA). Handling OAuth tokens can be challenging due to their short lifespan. This automated n8n solution ensures that a fresh access token is always available by utilizing n8n's Workflow Static Data feature for persistent caching. This specific n8n workflow supports both a regular, scheduled refresh cycle (triggered every 12 hours) and provides dedicated webhooks for immediate token retrieval or forcing a manual cache reset. Implementing this n8n automation means your downstream services will never fail due to an expired token.

How it Works

The core of this automation is initiated by the Schedule Trigger n8n node, set to run every 12 hours. This process ensures the token is refreshed well before expiry.


  1. Initialization: The Set Refresh Token and App ID n8n node injects the necessary credentials (initial refresh token, App ID, and Secret Key) into the flow.

  2. Cache Check: The Load to Static Data Code n8n node retrieves the currently cached token from the global Workflow Static Data. It determines if a refresh is needed based on the expiration time, applying a 90-second buffer to trigger the refresh early.

  3. Token Refresh: The Refresh Token (Zalo v4) HTTP Request n8n node contacts the Zalo OAuth endpoint, using the current refresh token to acquire a brand-new access token and an updated refresh token.

  4. Persistence: The Store to SD & Pass token Code n8n node is crucial. It overwrites the global Workflow Static Data with the new access token, refresh token, and updated expiry timestamps, completing the refresh cycle.

Additionally, an Execute_Node Webhook allows users to manually trigger a reset via the Clean Zalo Static Data Code n8n node, flushing the cache for re-seeding. A separate Webhook n8n trigger is available for external services to easily retrieve the current, valid access token.

Installation Guide

To install this n8n workflow:


  1. Import: Copy the provided JSON and import it into your n8n instance as a new workflow.

  2. Credentials Setup: Locate the Set Refresh Token and App ID n8n node.

Replace the placeholder values for refreshtoken, appid, and secretkey with your actual Zalo OA credentials.
Security Note: For production use, it is highly recommended to switch the assignment type to 'Expression' and reference environment variables (e.g., {{ $env.ZALO
REFRESHTOKEN }}) instead of hardcoding sensitive data.

  1. Webhook Configuration: Note the paths for the two Webhook n8n trigger nodes (ExecuteNode for manual reset and Webhook for retrieval). Ensure your n8n instance is accessible externally if you intend to use these integration points.

  2. Activate: Save and activate the n8n workflow.

Node Details

Schedule Trigger (n8n trigger): Runs the token refresh logic automatically every 12 hours to maintain token validity.
Set Refresh Token and App ID (Set n8n node): Serves as the configuration point, injecting the initial static Zalo OA credentials (refresh token, app ID, secret key) into the data stream.
Load to Static Data (Code n8n node): Checks the global workflow cache. This custom n8n node determines if the existing access token is still valid or if a new refresh cycle is required.
Refresh Token (Zalo v4) (HTTP Request n8n node): Performs the core OAuth action, calling the Zalo API to exchange the expiring refresh token for a new pair of access and refresh tokens. Uses form-urlencoded body type.
Store to SD & Pass token (Code n8n node): Persists the newly acquired tokens and their updated expiration times into the global Workflow Static Data, ensuring the cache is up-to-date.
Clean Zalo Static Data (Code n8n node): Executes on the manual reset path, deleting the cached tokens from the workflow static data, forcing the next run to use the initial seed token.


  • Webhook (n8n trigger, zalo-intergration-v1): Provides an external API endpoint for other n8n workflows or services to retrieve the currently active access token.

Related n8n Workflows

Free

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

Salesforce Architect with 10+ years of experience in CRM, integrations, and automation. Skilled in Apex, LWC, REST APIs, and full-stack dev (JavaScript, .NET). I build secure, scalable workflows in n8n—connecting Salesforce, Stripe, and more. Passionate about lead scoring, data sync, and secure field masking. Certified Application Architect with deep expertise in platform, integration, and data architecture.

Featured*