How to implement custom n8n node authentication with API key that needs auto-refresh

Describe the problem/error/question

Hi,

I’m building a custom n8n node for an API whose authorization flow is a bit different from standard OAuth2.
The process is:

  1. Send a request to a dedicated endpoint to obtain an API key.

  2. The endpoint responds with JSON like:

    {
      "response": {
        "api_key": "<string>",
        "expires_in": "<timestamp>"
      },
      "status": {
        "code": 200
      }
    }
    
    
  3. The api_key is valid for 10 days and must be used to access all API endpoints.

I could simply request a new key every time before calling the API resources, but that seems inefficient.
What is the recommended way to implement authentication in a custom n8n node so that:

  • The API key and its expiry timestamp are stored,

  • The node checks whether the key is still valid on each execution,

  • and, only if the key has expired, it requests a new one?

Any best practices or example patterns for handling this “long-lived key with auto-refresh” inside an n8n custom node would be appreciated.

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey! You can persist the key in workflow static data. It’ should be used for small chunks of data only and persists across executions. If you use it in workflows that don’t need it at the end of the execution (not like the current one), always make sure to clear it as it can crash your instance.

Feel free to mark as Solution if it helps.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.