Accessing URL parameters in N8N custom nodes: Challenges with standard JavaScript methods

I’m trying to get URL parameters in N8N custom nodes. The N8N workflow is embedded in an app using an iframe. I want to use these parameters for automation tasks.

Here’s what I’ve tried:

  1. window.location.href doesn’t work. N8N doesn’t recognize the window object.

  2. I attempted using the url module:

const url = require('url');
let params = new url.URLSearchParams('token=example');
console.log(params.get('token'));

This partly works, but I can’t figure out how to get the actual URL to pass to URLSearchParams.

Does anyone know how to grab URL params in N8N custom nodes? Regular browser-side JavaScript methods don’t seem to work here. Any tips or workarounds would be super helpful!

I’ve faced similar challenges with N8N custom nodes and URL parameters. From my experience, the standard browser methods don’t work because N8N runs server-side. One approach that worked for me was using the ‘n8n-workflow’ module. You can access the incoming request data, including query parameters, through the $node object.

Try something like this in your custom node:

const incomingData = $node.getInputData();
const queryParams = incomingData[0].json.query;
const token = queryParams.token;

This assumes your N8N workflow is triggered by an HTTP request. You might need to adjust based on your specific setup. Also, consider using environment variables for sensitive data like tokens. Hope this helps point you in the right direction!

hey ryan, i’ve run into this too. tricky stuff! have u tried using the $node object? it’s got some useful methods. maybe something like:

const params = $node.getWorkflowStaticData(‘global’).urlParams;
const token = params.token;

not sure if itll work 100%, but worth a shot. lemme know how it goes!

I’ve been working with N8N for a while now, and I can tell you that accessing URL parameters in custom nodes can be tricky. One method I’ve found effective is using the ‘n8n-workflow’ package, specifically the NodeExecuteFunctions interface.

Here’s what you can try:

In your custom node, import the necessary types:

const { IExecuteFunctions, INodeExecutionData } = require(‘n8n-workflow’);

Then, in your execute function, you can access the workflow data like this:

const workflowData = this.getWorkflowStaticData(‘global’);
const urlParams = workflowData.urlParams;

If you’ve set up your workflow correctly to pass URL parameters, they should be available in the urlParams object.

Remember, this approach requires you to set up your workflow to capture and pass along the URL parameters from the initial HTTP trigger node. It’s not a direct browser-like access, but it’s a reliable way to get those parameters into your custom nodes.

Let me know if you need any clarification on this approach!