How to update credentials from nodes package or core package

Hello n8n community,

I need some help regarding codebase, There is a case where I need to update a credentials from a node.

My Case: I have access token in my database (credentials entity) and it can expire after few days but I can programmatically generate a new token (with the help of some other parameters) I want to save new token in credentials entity.

This is what I tried:
Just like getCredentials I tried adding a method updateCredentials in NodeExecuteFunctions.ts, but seems like we don’t have DB access in core package.

Can anyone please guide me, how to do this ? Any code pointers I can follow ? I just need to update credentials from my node

1 Like

Can’t you renew the token within the same workflow? If it’s an HTTP Request, it would probably return 401 when the token is expired. If so, then you refresh it and then use the new refreshed token to continue the workflow.

Also, perhaps you can reference the token in the credentials using expressions? You can also access env variables from the credentials.

Just a couple of thoughts

Thanks for your reply Ricardo,

I can renew token during node/workflow execution but I want to persist the new token in credentials,
so next run of same workflow could use new token.

Can’t use expressions in this case, tokens are not coming from any preceding node, they were save 1 time in database as CredentialsEntity.

Can we update DB objects from nodes-base or core package code ?

Ahh, it has been a long time since I did something in the core, so to be honest, I do not remember. I can remember that you can access a function to update the credentials in the server.ts file.

@jan or @krynble might be better suited to answer this.

Sorry, there is currently no officially supported way to change credentials via a node and you are also correct that core does not know anything about a DB.

Is a long time that I wrote all of that code but the only simple way I can think of right now to make that possible is the very hacky and ugly way of doing it via the n8n API the frontend uses. So an HTTP request in the node.

1 Like

Thanks @jan, this is exactly what I did as a workaround but like you mentioned it seems hacky…
I am going to keep that piece for now. If you see any better way of doing this, please let me know.

Really appreciate your help, thanks again.

Hello @jan @Mudassir_Ali ! I encountered the same issue for a custom node. There is a limit of the possible active tokens and they expire after 3 hours.

I’m trying to have a field in the credentials with this token and to update it automatically from the N8N API. I can create / delete credentials but I cannot update them:
What request to the N8N API can I use and can you explain this solution?

I also tried to use the static global data to store it. It saves a lot of the tokens and for automatic executions it seems to be working well. But it is not shared between manual executions. For each of them a new token is requested.

Hey @valya,

This might be better off as it’s own thread but it might be worth taking a look at the Metabase Nodes credentials.

In there we have a sessionToken that is hidden and marked as expirable you can then use the preAuthentication method that will refresh the token if it has expired.

I am not sure if this would solve the problem you are seeing but it could help.


Hey @Jon , thank you for pointing me to this! Works like charm!

1 Like

Hi @Mudassir_Ali ,

I am in the exact same situation where I would like to update my credentials from a node… Did you find another solution since your post ?
Otherwise, what is the HTTP endpoint you take about in your workaround ?

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