I’m trying to get URL parameters in N8N custom nodes. The N8N workflow is embedded in an iframe in my app.
I’ve tried these approaches:
- Using
window.location.href
, but N8N doesn’t recognize the window
object.
- Tried the
url
module:
const url = require('url');
let params = new url.URLSearchParams('example=test');
console.log(params.get('example'));
This works, but I can’t figure out how to get the actual URL to pass to URLSearchParams
.
Is there a way to access URL params in N8N custom nodes? The standard browser methods don’t seem to work here. Any ideas or workarounds would be really helpful!
hey there, i’ve faced this too. n8n’s server-side nature means standard url methods often fail. try: const url = $execution.getNodeParameter(‘fullUrl’); const params = new URLSearchParams(url.split(‘?’)[1]); works if triggered by a webhook. otherwise, pass the params from an earlier node.
As someone who’s worked extensively with N8N, I can tell you that accessing URL parameters in custom nodes can be tricky. The browser methods don’t work because N8N runs server-side, not in a browser environment.
Here’s what I’ve found to be effective:
You can access the full URL of the webhook that triggered your workflow using the $execution object. In your custom node’s code, try something like:
const fullUrl = $execution.getNodeParameter(‘fullUrl’);
const urlObj = new URL(fullUrl);
const params = urlObj.searchParams;
This should give you access to the URL parameters. You can then use params.get(‘paramName’) to retrieve specific values.
Remember, this approach only works if your workflow is triggered by a webhook. If you’re using a different trigger, you might need to pass the URL parameters as input data to your custom node from earlier nodes in the workflow.
Having dealt with similar issues in N8N, I can offer an alternative approach. Instead of trying to access URL parameters directly within the custom node, consider passing them as input data to your node from earlier in the workflow.
You could use an HTTP Request node at the start of your workflow to capture the full URL, then extract the parameters there. Pass these as fields to your custom node.
In your custom node’s code, you can then access these parameters like this:
const myParam = $node.myParam;
This method is more reliable and gives you greater control over the data flow in your workflow. It also sidesteps the limitations of server-side execution in N8N.
Remember to properly sanitize and validate any parameters you’re working with to ensure security and robustness in your workflow.