New to retool and JavaScript – Best practices for replicating a python script to fetch and process API data

Goal: Hi, I’m new to Retool and JavaScript! I want to replicate a Python script in Retool and need guidance on how to handle multiple API calls, data manipulation, and transformations using JavaScript.

Steps: I’m trying to build a workflow in Retool where I can fetch sales orders from an API, extract product details for each order, and store the results in a structured format (like a table). Here’s the Python code I’ve been using:

url = "https://%%/api/sales/order?limit=1000"

token = "%%%"
headers = {
    "Authorization": f"Bearer {token}",
    "Content-Type": "application/json"
}

response = requests.get(url, headers=headers)
orders = response.json()

ids = [x['id'] for x in orders]

all_products = []
for id in ids:
    url = "https://%%%/api/sales/order/" + id

    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }

    response = requests.get(url, headers=headers).json()
    all_products.extend([
        {
            "id": product["id"],
            "extra_id": product["extra_id"],
            "name": product["name"],
            "quantity": product["quantity"],
            "uom": product["uom"]
        }
        for product in response['products']
    ])

df = pd.DataFrame(all_products)
print(df)

How can I achieve the same result in Retool using JavaScript? Any best practices for structuring this workflow in Retool would be greatly appreciated!

Hello @mavimassa!

Welcome to Retool :grin: so great news, our Retool Workflows allow for the use of Python code!

You can create a block to fetch your data using a query, then use a loop block to iterate over the data and extract out the order details you need.

Then pass that to another block to format the data as needed and then use another query to 'POST' that data in a structured format to a DB table.

We also have a fun little AI tool which is the purple robot in the bottom corner of a query block that you can click on and prompt with text input to have it build a query to your liking.

This could also work with you passing the python code you are using to it as well and could be used in a Retool app to convert over your python code to Retool/JS code if you want to use an App instead of a workflow.

Hope this helps!