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

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.