I am currently working on a custom node for n8n, and I want to know if there’s any way I can automatically save an API response as a credentials.
The custom node that I’m working on need to send an API request to get access token and that access token is needed to send an API request to its core API functions and that said access token expires after a certain amount of time.
So I was hoping if there’s a way for me to save the access token(after sending the API request to generate access token) as well as the expiry date and save it as a credential automatically?
Not sure if there is a generic approach using which this can work tbh. This might be one for our resident node building specialist @marcus.
Or are your credentials OAuth2 credentials by any chance? In this case you could simply extend n8n’s existing logic on storing the tokens and refresh them as needed, similar to what other nodes using OAuth2 are doing (Raindrop for example).
There you will see a hidden, expirable sessionToken property
You will also see a preAuthentication() method that will be called if the sessionToken is empty. You can make http requests here and whatever you return will be applied to the credentials object.
In case you need a more complex authentication logic, you can also use a function for authenticate, e. g. see the CalendlyApi.credentials.ts implementation.