Help with python code. "NameError: name 'input_data' is not defined"

I am in the process of moving everything from Zapier and I am getting most of it, but I am stuck at the most simple thing. the error I am getting is

ERROR: NameError: name 'input_data' is not defined

Error: NameError: name 'input_data' is not defined

    at PythonSandbox.getPrettyError (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:69:20)
    at PythonSandbox.runCodeInPython (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:56:24)
    at PythonSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:22:33)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:113:25)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:658:19)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:631:5

and I have a feeling it is because I am not telling it what the input is from the previous node, the ZPID in my case.
But I have read the docs up and down and cannot find where it specifically says how to do this. it is probably my lack of experience with python that is my roadblock

What is wrong with my last node?

n8n cloud

Hi @djjace, n8n would reference input data in a slightly different way. Assuming the data you are interested in lives in the zpid field, you’d need to use _input.item.json.zpid instead of input_data.get('zpid').

Let me know if you run into any trouble with this!

So, I did that and I am still coming up with the ```
Error: NameError: name ‘zpid’ is not defined

The code

def get_property_details(zpid):
    zpid = extract_zpid_from_url(zpid)

    url = "https://zillow-com1.p.rapidapi.com/property"

    querystring = {"zpid": zpid}

    headers = {
        "X-RapidAPI-Key": "REDACTED",
        "X-RapidAPI-Host": "zillow-com1.p.rapidapi.com"
    }

    response = requests.get(url, headers=headers, params=querystring)

    return {'property_details': response.json()}

# Example usage
_input.item.json.zpid
output = get_property_details(zpid)

the error

Error: NameError: name 'zpid' is not defined

    at PythonSandbox.getPrettyError (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:69:20)
    at PythonSandbox.runCodeInPython (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:56:24)
    at PythonSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:22:33)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:113:25)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:658:19)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:631:53

I’m so sorry @djjace. Can you share the JSON data structure you are passing on to your final code node so I can reproduce (and hopefully fix) the problem? You can of course redact anything confidential, just make sure to keep the structure itself.

It looks like you’re simply referencing a non-existent field here. As for the actual logic you probably want to use the existing HTTP Request node for your request rather than write custom code. But let’s take a look at your data structure first and then worry about the exact implementation :slight_smile:

Also, I’ll removed your own API key from your previous post. To be on the safe side you would still want to rotate it.

zpid not defined, the _input.item.json.zpid is right.
you can try

thanks for that, totally slipped my mind to remove it

here is the json structure from the last node

[
  {
    "headers": {
      "host": "xxxxxxx",
      "x-request-id": "xxxxxxxxxxxxx",
      "x-real-ip": "5xxx83",
      "x-forwarded-for": "5xxxxxx.83",
      "x-forwarded-host": "xxxxx.n8n.cloud",
      "x-forwarded-port": "443",
      "x-forwarded-proto": "https",
      "x-forwarded-scheme": "https",
      "x-scheme": "https",
      "content-length": "131",
      "accept": "application/json",
      "content-type": "text/plain",
      "user-agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
    },
    "params": {},
    "query": {},
    "body": "Hello, this is new message, https://www.zillow.com/homedetails/112-Earl-Jones-Rd-Hodgenville-KY-42748/115357515_zpid/?view=public \n",
    "zpid": "115357515"
  }
]

thanks so much for your help with this.

I am very much a beginner here, lol, would an http request be better?

1 Like

Yes, much better. The requests module used in your code is unfortunately not available in n8n’s Python implementation, so even once the syntax problems are sorted this wouldn’t work using the snippet provided.

On the plus side, the HTTP Request node is much easier to use and won’t require code. What your code does is this:

  • Send a GET request to https://zillow-com1.p.rapidapi.com/property
  • Use a query string including the zpid value from your previous code snippet
  • Add two custom headers

In a workflow using the HTTP Request node this would look like so:

This example uses credentials to store your API key. You’d need to configure them once as shown below (name = X-RapidAPI-Key, value = your actual API key) and can then re-use them in every HTTP Request node you might be using in any of your workflows, without having to add your API key in clear text to your nodes:

You can also see that I am using an expression in my HTTP Request node to read the incoming zpid value. You wouldn’t have to write this manually, simply drag and drop the value you want to use in the target field:

Hope this helps!

1 Like

It works perfectly now, thanks you all!

1 Like

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