Expression in credentials - only uses first item

Describe the problem/error/question

I’ve created a credential containing an expression which references a node that pulls data from an Excel spreadsheet. When I run the workflow it loops the correct amount of times for the number of rows in the spreadsheet, but it’s using the data from the first item every time. Previous topics have mentioned that this is correct and to get around it by using the Split In Batches node, which I’ve now done (using the newer Loop Over Items version), but it still does this.

What is the error message (if any)?

None

Please share your workflow

Note: I’ve changed the Excel node to a code one with dummy data for sharing, but the same happens regardless.

Share the output returned by the last node

Can’t share the output, but I can say that the output is correct for the first item, then duplicated for each subsequent item.

Information on your n8n setup

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

Actually, I’ve just noticed that after the first run, the credential itself gets changed to fixed (rather than expression) with the value from the first item of the first run. So all subsequent runs use the fixed value instead of the list of values returned.

In case it helps, I’m using credential type of OAuth2 API, and the field with the expression is Access Token URL. The expression I’m using is:

https://login.microsoftonline.com/{{ $('Get Tenants').item.json["Tenant ID"] }}/oauth2/v2.0/token

Hey @mtxkev,

We fixed an issue with not iterating over expressions properly in credentials in 1.27.0 I would recommend starting with an upgrade to see if that fixes the issue for you.

The change itself can be found here: fix(core): Properly iterate over credentials with expressions by krynble · Pull Request #8502 · n8n-io/n8n · GitHub

Thanks @Jon, I’ll update and report back!

1 Like

Hi @Jon I’ve just tried this after updating to 1.31.2, unfortunately I’m getting the same behaviour - the first time the workflow runs, the credential changes to a fixed one with the value from the first item, and the loop just uses the same credentials each time.

Hey @mtxkev,

OAuth might be a bit different as a changing access token url would likely break the credential being used on refresh as the client id, client secret and account used to generate would likely be different. I missed the bit previously where you mentioned using OAuth credentials.

@Jon ah OK, in that case do you think there would be any other way we might be able to achieve this with OAuth? I need to do the same thing 85 times, so right now I have 85 lots of credentials and workflows, which is unscalable!

Hey @mtxkev,

The best I can think of would be have the multiple credenitals although it really depends on what the end goal actually is. In the future we will allow for credenitals to be selected by an expression which would help but if you are managing multiple M365 tenants maybe there is a special tenant admin option that lets you impersonate / access an instance.

Thanks @Jon, the end goal is basically to pull a bunch of info (user counts etc) for each of our customers’ M365 tenants, we do have partner access but unfortunately most of the Graph API is only accessible in the context of the tenant itself (not via partner access), and the auth token URL has to contain the specific tenant ID. I was hoping to add that via an expression in the credential but looks like that’s not going to be possible right now, so instead I will continue with a workflow and associated credential for each individual customer.

1 Like