Zalo OA Refresh Token icon

Zalo OA Refresh Token

Refresh Zalo OA access token and store it in database for reuse

Overview

This node manages Zalo Official Account (OA) access tokens by either refreshing the token or checking its current status. It is useful in workflows that integrate with Zalo OA APIs, ensuring that valid access tokens are available for API calls without manual intervention.

  • Refresh Token Operation: Automatically refreshes the Zalo OA access token using a stored refresh token and updates the token storage (database, environment variables, and workflow static data).
  • Check Token Status Operation: Checks if the current access token is still valid, how long until it expires, and whether it should be refreshed soon.

Practical examples:

  • Automatically refreshing tokens before making API requests to avoid authentication failures.
  • Monitoring token validity in webhook workflows to decide when to trigger a refresh.

Properties

Name Meaning
Operation Choose between "Refresh Token" (refresh and store new tokens) or "Check Token Status" (verify current token validity).
Return Access Token (Boolean, only for "Check Token Status") Whether to include the actual access token in the output (useful for webhook endpoints).

Output

The node outputs an array of JSON objects, one per input item, with the following structure depending on the operation:

  • Refresh Token:

    {
      "success": true,
      "message": "Token refreshed successfully and saved to database",
      "access_token": "<new_access_token>",
      "refresh_token": "<new_refresh_token>",
      "expires_in": <seconds_until_expiry>,
      "expires_at": "<ISO_timestamp_of_expiry>",
      "db_save_success": true,
      "refresh_executed_at": "<ISO_timestamp_of_refresh>"
    }
    

    This confirms the token was refreshed and saved successfully.

  • Check Token Status:
    If Return Access Token is false (default):

    {
      "is_valid": true,
      "expires_at": "<ISO_timestamp_of_expiry>",
      "hours_until_expiry": <number_of_hours>,
      "should_refresh": <boolean_if_less_than_2_hours_left>,
      "check_executed_at": "<ISO_timestamp_of_check>",
      "current_access_token_preview": "***<last_10_chars_of_token>" // or "Not set"
    }
    

    If Return Access Token is true:

    {
      "is_valid": true,
      "expires_at": "<ISO_timestamp_of_expiry>",
      "hours_until_expiry": <number_of_hours>,
      "should_refresh": <boolean_if_less_than_2_hours_left>,
      "check_executed_at": "<ISO_timestamp_of_check>",
      "access_token": "<full_access_token_or_null>"
    }
    

No binary data output is produced by this node.

Dependencies

  • Requires an API key credential for authenticating with the Zalo OA API.
  • Uses Axios HTTP client for API requests.
  • Stores tokens in multiple places for redundancy:
    • Workflow static data
    • Environment variables (if accessible)
    • A database table named variables (requires a database connection configured in n8n)
  • The node expects a database connection service to be available or falls back to credentials/environment variables.

Troubleshooting

  • Common issues:

    • Failure to connect to the database will prevent token retrieval or saving; ensure the database connection is properly configured.
    • Missing or invalid API credentials will cause authentication errors when refreshing tokens.
    • Environment variables may not be writable depending on the runtime environment, so token saving there might fail silently.
  • Error messages:

    • "Failed to refresh token: ..." indicates the API call to refresh the token did not return expected tokens. Check API credentials and network connectivity.
    • Database save failures are logged but do not stop execution; verify database permissions and schema.
    • If the node throws "Failed to execute operation '...' : ...", check the error message for details and ensure all required inputs and credentials are correct.
  • Recommendations:

    • Enable "Continue On Fail" in the node settings to handle errors gracefully in workflows.
    • Monitor logs for warnings about failed token fetch/save attempts to diagnose storage issues.

Links and References

Discussion