How to create dynamic workflows via API?

Describe the problem/error/question

I’m trying to create a workflow to create other workflows using the n8n API. When the nodes of the workflow that I want to create by the API doesn’t have expressions everything works fine, but when I add an expression to a node parameter it will [ERROR: invalid syntax]

I want to be able to use dynamic values so I need to use expressions. How do I add expressions within a workflow object?

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

[ERROR: invalid syntax]

Information on your n8n setup

  • n8n version: 2.0.3
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: Ubuntu 24.04 LTS

Hey there

You can use expressions when creating workflows via the n8n API, but they must be sent as plain strings inside the workflow JSON, correctly escaped. n8n only evaluates expressions after the workflow is saved and executed. Most “ERROR: invalid syntax” issues come from incorrect JSON escaping or mixing JavaScript syntax with JSON.

What characters need escaping & what to do when it does contain javascript code. The workflow works when I’m just exporting & importing but it doesn’t work when it’s created via API. I don’t want to go the export & import route since I can’t use dynamic values

When creating workflows via the n8n API, the workflow must be valid JSON.

Exports/imports work because n8n handles escaping for you. The API does not.

Like this

“value”: “={{ $json[“name”] }}”

If the parameter contains JavaScript code the n8n expression are still strings not raw JS yet

“value”: “={{ $items(“Node”)[0].json[“id”] }}”

You can try and set the json in the set node and then stringify it in the Create a Workflow node like this;

{{ JSON.stringify($json.workflowBody) }}
2 Likes

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