Hello everyone,
I’m developing a custom credential type for an n8n node that connects to IBM TM1 REST API. This API supports two authentication modes:
- Mode “1”: Basic Auth
- Mode “5”: CAMNamespace Auth, which requires the Authorization header to be
CAMNamespace+ base64(user:password:namespace)
I want to build the Authorization header dynamically inside the credential’s authenticate property using n8n’s expression syntax, like this:
Authorization: '={{ $credentials.security_mode === "5" ? "CAMNamespace " + $base64Encode($credentials.user + ":" + $credentials.password + ":" + $credentials.namespace) : "Basic " + $base64Encode($credentials.user + ":" + $credentials.password) }}'
The problem is that the base64 encoding behaves incorrectly or inconsistently inside this expression, causing authorization failures.
I suspect this happens because:
- The
$base64Encode()function only accepts simple string input and doesn’t handle complex concatenations well. - Some credential fields might be empty or undefined, causing malformed strings before encoding.
I’ve also tried to generate the Authorization header in the TypeScript node code itself (using Buffer.from(...).toString('base64')), and it works perfectly.
My questions:
- Is there a recommended way to do dynamic, conditional base64 encoding inside n8n credential expressions?
- Should I avoid putting this logic in the credentials and instead always generate the header inside the node’s TypeScript code?
- Are there best practices or examples for handling complex auth headers depending on credential inputs in n8n?
I’d appreciate any advice or examples!
Thanks.