Programatically passing credentials into a Workflow

Continuing the discussion from Integrations which my clients can use:

Sometimes you may want to run the same (or similar) workflows for different user accounts. For example, when embedding n8n, you may want to capture credentials for each user on your own site and automatically set up workflows to run for them.

You have two options for doing this:

  1. Create a new workflow for each user
  2. Have a single workflow and call it with different credentials for different users

The advantage of (2) is that you only have a single workflow, so making changes is easy and things stay neat. However, you would have to trigger the workflow yourself (e.g. you wouldn’t be able to use our built-in triggers for individual services)

With (1), you have no limitations on how the workflow is triggered — you create it once and it runs like any other n8n workflow. However, you will need to manage each workflow that you create.

1. Creating a new workflow for each user

Steps:

  1. Obtain the user’s credentials
    • You may wish to do this by asking them to enter an API key on a webpage, or launching an OAuth flow for them
    • You could also capture parameters for the workflow at this point if you wanted
  2. Create a credential in n8n for the user
    • You can use n8n’s frontend API for this
    • This call returns the ID of the created credential, which you will need in the next step
  3. Create the workflow in n8n for the user
    • The best way here is to have a ‘template’ workflow that you copy and customise
    • Fetch the JSON for the template workflow (could be via API)
    • Insert the ID of your newly-created credentials in the JSON (and any other user-specific settings you might need)
    • Create the new workflow (via API)
    • Activate it (via API)

NB: You may want to make your user-specific workflows call a single, more general workflow for the non user-specific parts. That way you reduce the chances that you’d need to changes to many workflows.

2. Calling a single workflow with different credentials

NB: For OAuth, this requires being able to set a credential using an expression, which is currently under development

Steps:

  1. Obtain the user’s credentials
    • You may wish to do this by asking them to enter an API key on a webpage, or launching an OAuth flow for them
    • You could also capture parameters for the workflow at this point if you wanted
  2. Create a credential in n8n for the user
    • You can use n8n’s frontend API for this
    • This call returns the ID of the created credential, which you will need in the next step
  3. Set up a workflow
    • The workflow needs to be triggered via a webhook, and the credentials should be sent with the webhook call (plus and user settings you need)
    • For the nodes that need the user’s credential, use an expression on the credentials field to read the credential ID from the webhook node
  4. Activate the workflow (using the n8n UI)
  5. Call the workflow
    • Call the webhook whenever you want, passing the data outlined in the previous step
1 Like