Run a Python function with an input from a previous node... also import requests?

Hello -

I am using N8N and have access to the Python node.

I have a simple 3 node workflow that runs a SQL statement, gets a list of Amazon ASINs back (pretty much IDs) and now I would like to pass those into the Python function below.

I am running into a couple errors…

  1. I am getting an error: ERROR: ModuleNotFoundError: No module named ‘requests’
  2. I can see the next error is going to be around passing ASIN from the previous node into the print statement to return the result.


Thank you!

import requests

def generate_affiliate_url(asin, affiliate_tag):
    base_url = ""
    params = {
        "asin": asin,
        "tag": affiliate_tag
    response = requests.get(base_url, params=params)
    if response.status_code == 200:
        return response.json()['short_url']
        return None

# Example usage:
# print(generate_affiliate_url("B07H9YWT9C", "your-affiliate-tag-20"))

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

As n8n does use Pyodide underneath the hood, which does not support the request module, does sadly n8n neither. So, I suggest using the HTTP Request Node to make HTTP requests.

Regarding your second question, assuming you have Mode: Run Once for Each Item set, you should actually be able to see the answer in the default code of a Code node.

The default code is:

# Add a new field called 'myNewField' to the JSON of the item
_input.item.json.myNewField = 1
return _input.item

Meaning assuming the “ASIN” is saved as asin on the item, you can access it via _input.item.json.asin.


Thank you! I appreciate the detailed response, this puts me on the right track!

1 Like

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