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
2 Likes

Happily awaiting the dynamic OAuth credentials option! I’ll use it to be able to create Outlook email drafts in the corresponding user’s account. Going to create a workflow for each user that gets triggered by the main workflow and only handles the draft creation, for now!

Loving N8N so far, it’s been an amazing experience working with the tool and seeing how it is evolving. Just wanting to revisit this conversation, I’ve been able to programmatically pass credentials into a workflow where they simply require an API secret key (Notion credentials for example).

Can I ask how I would best have the same result for Google Calendar OAuth APIs, where seemingly each user will have to the google cloud console and do a bit of setup. I would prefer to have an option where users on my application (not on N8N) can just click Connect With Google and an OAuth verification pops up, and they can simply press accept (like I see most apps are able to do). Then the credentials can be passed via backend to N8N.

Happy to share more details wherever needed, thanks in advance for the help!

l’m also interested in dynamically passing credentials to nodes, which would highly increase reusability of workflows.

@sirdavidoff could you tell me, which PR is related to this feature? Is it Add authentication to credentials by janober · Pull Request #2334 · n8n-io/n8n · GitHub?

I am reaching out to express my utmost enthusiasm for the possibility of programmatically passing credentials to N8N workflows. As an avid user of N8N, I have found it to be an exceptional platform for automating workflows, connecting various applications and services, and simplifying complex processes. The ability to programmatically pass credentials would undoubtedly elevate the functionality and versatility of this already impressive tool.

Thanks a lot. That is great to hear! I assume the following Feature Request that matches what you want to do?

If so, it would be great if you could upvote it so that we can prioritize accordingly.

I am looking for exactly this functionality as well. Do you know if it’s possible by now?

" would prefer to have an option where users on my application (not on N8N) can just click Connect With Google and an OAuth verification pops up, and they can simply press accept (like I see most apps are able to do). Then the credentials can be passed via backend to N8N."

This exactly describes what I need.