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

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

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) }}