N8N Code Node With Python

N8N Code Node With Python

N8N is an opensource workflow automation tool that can be self hosted. One of the many useful features is running the code node allowing us to write custom JavaScript and Python code to manipulate the data. This is a short blog on how use custom Python in N8N code node.

Running custom JavaScript code is not complicated, however my experience with running custom Python code was kind of annoying since we have to convert the input from a JSProxy object and when returning we have to convert it back to a PyProxy object. It is converting of the input and output that was messy.

N8N uses pyodide to gain the ability to run python code in a JavaScript environment.

Processing Input

Input in the code is a json object, but initially when we first get the input in the Python node, it is of type <class 'pyodide.ffi.JsProxy'>. We can then access each individual json object

for item in _input.all():
  t=item.json
  # t=item.json.to_py() # We can optionally, use the to_py() method to convert it to a native python object
  # We can then access the key 
  print (t['key'])
For easier processing, we can add all the json object to a list and process the list.

Return Output

Once we have processed our data, we can convert add our result to a dict, we then add the dict to a list, we finally return that list.

data={"name":"jane doe"}
output=[data]
return output
We use a list since it can contain an array of objects. However, if you are returning only a single object, we can just return data

Example Workflow

In the workflow below we are using a sample datastore and then processing the data using custom Python code in the code node

Below is the workflow file.

{
  "name": "My workflow 2",
  "nodes": [
    {
      "parameters": {},
      "id": "30534fb1-501e-4dc2-a5a6-1ef58a70ca11",
      "name": "When clicking \"Test workflow\"",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        400,
        240
      ]
    },
    {
      "parameters": {
        "operation": "getAllPeople"
      },
      "id": "22190e50-7f1d-4efb-aed8-476999b05bd1",
      "name": "Customer Datastore (n8n training)",
      "type": "n8n-nodes-base.n8nTrainingCustomerDatastore",
      "typeVersion": 1,
      "position": [
        580,
        240
      ]
    },
    {
      "parameters": {
        "language": "python",
        "pythonCode": "my_list=[]\nfor item in _input.all():\n  t=item.json\n  if t['id']=='23423532':\n    my_list.append({'new_name':t['name']})\n    print(t['id'])\n\nreturn my_list"
      },
      "id": "582f7bb4-feee-401b-ac13-b1ae43d53872",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        800,
        240
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "When clicking \"Test workflow\"": {
      "main": [
        [
          {
            "node": "Customer Datastore (n8n training)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Customer Datastore (n8n training)": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "df3769da-bfd9-46b9-9bfd-17ad5c0c6b45",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "5e1619f539bf4fca477bbc4fab24e9f25d671f9e9b0589672c29f0bdbe199d84"
  },
  "id": "vKDtJPbmSy9pzlrB",
  "tags": []
}