/execute-python

This node is used to execute Python code. It takes the Python code as input, runs it and returns the output of the execution.

Method: POST

Note: The method POST is appropriate here as we are sending data (Python code) to be processed by the server. No change in the method is needed.

URL

https://node.nodetrigger.com/execute-python

JSON Body Parameters

  • code (string): The Python code to be executed. This is a required parameter.

Formatting the JSON and Python Code

The JSON body must contain a single key code with a string value that holds the Python code to be executed. The Python code should be properly formatted and indented. To ensure the output is captured, you need to explicitly define the output in the code by assigning it to a variable named output.

For example, to return the sum of a list, you would write:

{
    "code": "output = sum([1, 2, 3, 4, 5])"
}

In the Python code, make sure to:

  • Use valid Python syntax.
  • Define an output variable for the result you want to be returned.
  • Escape any newline characters (\n) and double quotes (\") in the JSON string.

How to Make a Call

To use this endpoint, you need to send a POST request with a JSON body containing the Python code you want to execute. Below are the detailed steps and examples.

Run Python Code in Make

In the context of executing Python code through our node, it is crucial to assign the result of your python code to a designated output variable. This practice is essential for the successful retrieval and display of results from your executed code.

When you assign your results to the output variable, the server can specifically capture this value and return it as part of the response payload. Without this assignment, the server would not have a standardized way of determining what specific piece of data to return, potentially leading to a lack of response or an error.

/execute-python

Example Request

Example 1: Sum of a List

Request:

curl -X POST https://node.nodetrigger.com/execute-python \
     -H "Content-Type: application/json" \
     -d '{"code": "output = sum([1, 2, 3, 4, 5])"}'

Description: This example calculates the sum of the list [1, 2, 3, 4, 5] and returns the result.

Example 2: Factorial Calculation

Request:

curl -X POST https://node.nodetrigger.com/execute-python \
     -H "Content-Type: application/json" \
     -d '{"code": "def factorial(n):\\n    if n == 0:\\n        return 1\\n    else:\\n        return n * factorial(n-1)\\noutput = factorial(5)"}'

Description: This example calculates the factorial of 5 using a recursive function and returns the result.

Response

The response will be a JSON object containing the output of the executed code.

Successful Response:

{
    "output": 15
}

Error Response:

{
    "error": "No Python code provided"
}

or

{
    "error": "Python execution error",
    "message": "Error message with details"
}

Summary

  • Endpoint: https://node.nodetrigger.com/execute-python
  • Method: POST
  • JSON Body Parameters:
  • code (string): The Python code to execute
  • Usage: Send a POST request with the Python code in the JSON body
  • Response: Returns the output of the executed Python code or an error message