How can I save an API response as a credential?

Hello Everyone!

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?

I hope to hear from you everyone soon.

Thank you!

Hi @clarence_bernacer, welcome to the community!

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).

1 Like

Hey @clarence_bernacer,
in case you aren’t trying to do OAuth2 authentication as @MutedJam mentioned, you could take a look at our MetabaseApi.credentials.ts.

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.

You can than use your sessionToken in authenticate like this

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.


Oh that’s nice, I didn’t know we already do this! Sweet :slight_smile:

1 Like

Thanks for the reply, I will try this out.