HTTP Request Node - Body Auth - How to save Credentials and Token

Dear n8n Community,

I have a endpoint which needs to authenticate at the body level and then generates a access token.

My problem is that I want to create and save a credential set to save that nodes credentials encrypted inside n8n, which I did not manage.

Also that token generated should be saved inside the credentials till it expires and then auto-revoke a new token.

The node looks like this:

Maybe you have some ideas for that case?
I also tried and searched to create a expression to enter the credentials from a saved credential set, which was not possible.

Thank you

Hi @prononext, is the server you’re connecting to using OAuth2 by any chance?

If so, n8n can usually manage the token renewal for you. It also supports sending the credentials in the body if you select Body in the Authentication dropdown of your OAuth2 credentials:

image

Sadly it is not OAuth2, but maybe there is another way for this case?

I think in this case your best bet might be to save your secret in an external data store (such as a database) and read it in your workflows whenever needed.

You could also consider using an environment variable which you can read inside n8n through an expression such as {{ $env["MY_VARIABLE_NAME"] }}.

I have the exact same problem. And even though I think it should be easy to solve, there is something I am missing.

I am using n8n cloud solution.

I don`t know how to save environment variable - should i use ssh node? how do i authenticate?

I have also tried to save the credential to a file, but I got a permission denied when I`ve tried that. What path should i use that i have permission to save a file?

@MutedJam I`ve found no documentation about the n8n cloud installation. Could you point me to a solution please?

Hi @jgggr, welcome to the community!

You can’t access environment variables on n8n cloud I am afraid, so you would need to store the required values directly in your workflow.

Thank you for your fast reply.

I need to “save” the credential so i can use it until expires. If i just “set” the value it will be lost in the next run, right?

Let me be clear. I have to make an initial authentication sending my username and password in the body of an http request, just like the initial message in this thread. The server respond to me with a bearer token authentication. I will be using this token until expires, than i have to ask for another.

I have to save this token I’ve received to be used for many runs of the workflow.

This is my workflow.

1 Like

Hello buddy,

Did you find a solution?
I have the exact same scenario in that I have to authenticate at the body with a user/password in order to get a Bearer Token that expires after 5mins. My main concern is that I don’t want to store the password in plain text.
Are there any options for us n8n self-hosted? or must we upgrade to paid plans that offer ‘Global Variables’ (Pro Plan) or ‘External Secret Integration’ (Enterprise Plan).

Thanks in advance.

Hello,

I had a similar problem and been able to solve it with a ‘Custom Authentication’ type and providing the headers in my case. In your case you might need to provide the body instead.
Take a look here for a possible solution: HTTP Request Node - Header Auth - How to securely save Credentials - #7 by CristianG