Optional Credentials with Community Nodes

Describe the problem/error/question

I’m developing a node for an API that does not require authentication, but may include it in a request if utilizing/posting private resources. I’m using the programmatic approach utilizing the execute method from the INodeType. When I use the getCredentials method, it throws an error (Node does not require credentials) and stops the execution of the node when no credentials are selected. I’m having issues finding a way to check for set credentials before running the method or an option within the method to allow the node to continue when there are no credentials present or defined within the node. I have attempted to replicate the HttpRequest node’s credential selection, but I’ve been unable to get the credentials to lock down to only my custom node rather than allowing selection of other credential types. What would be the best way to handle this so that the node can continue through execution if no creds are present and will be able to access the creds if they are? Would it be best to avoid using the n8n credential convention and build a new string field within the node for the bearer token authentication?

Information on your n8n setup

  • n8n version: 1.39.1
  • Database (default: SQLite): SQLite
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: Windows

hello @jyln

have you tried to use the getCredentials with the try/catch approach?

E.g.

let myCreds: object
try {
	myCreds = this.getCredentials("123")
} catch {
	myCreds = {}
}
1 Like

@barn4k Thank you for the reply! I don’t know why I didn’t think of the try/catch approach but this was exactly what I needed

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.