I need to dynamically change the values defined in my openAI credentials according to the client’s API when the workflow is executed. In the credentials I have the option to use Expressions, but literally NOTHING works there. I put correct references of where the nodes are but I simply can’t use it that way. I can’t use an HTTP node because I’m in the Agent AI > model node, and I have several agents in my application that always use the same credential. I’d like to understand how expressions work in credentials since I can’t reference anything there. Another thing: I saw that some people use references with $env, but the scenario is dynamic and this comes from the webhook message of the client, just like the information I’m trying to pass in the credentials…
@seiflow You need a node if your going to be using json to chage it, you can use a set node and an IF statement node to set it up. It says there is an error because there isnt a node that has the info. If you have the nodes and it isnt working, do you have any additional info or errors, or could we see more of your workflow without the sensitive api info?
Hi @seiflow, welcome!
Although it says that there is no path back to the node and throws an error below, if you actually try to map the variable exactly like shown in the below workflow example, it will work perfectly. A drawback is that the model list would be fetching automatically, but you can fix it via also passing a model name as a JSON the name should be exactly like the one displayed in the original model list, as it is case-sensitive and sometimes would not work if the model name is different or invalid. See the below workflow:
You have to exactly mention {{ $json.apiKey }} in the API key section of the OpenAI node credential and it would defintely work, I have tested it and it works perfectly.
For the model list here it is:
@seiflow Welcome to the n8n community!
Why are you using workflow data inside the credential?
hi @Anshul_Namdev, I did the test as you mentioned and it worked yes, not exactly the same variable name.. but apparently it only works if the reference is to a node that MUST come before the agent. I confess that I don’t see the point and it really seems like 2 errors, the first for having the expression option in all credential fields but when making a reference (even the correct one) it doesn’t show any value, and for requiring mandatory reference in the previous node. I understand it might be because “credentials” is in a different scope but if the option is there, it should work. Anyway, thank you for the solution!
Hi @tamy.santos, my flow is multi-tenant and the credentials information comes from the webhook at runtime, and I need to pass this information dynamically because I separate costs by project (tenant) within OpenAI, each with its own apiKey or projectId, which I need to pass within the credentials in each call to the llm agent. @Anshul_Namdev provided a solution that solved the problem, although I believe it’s a bug in n8n.


