{
  "openapi": "3.0.1",
  "info": {
    "title": "Python Code Runner – pandas, DuckDB & SQL on Apify Datasets",
    "description": "Run your own Python or DuckDB SQL on rows from an Apify Dataset or a JSON array in one API call. pandas, DuckDB, pyarrow and openpyxl preinstalled, extra pip packages on request. Rows back in the Dataset; errors come back as data. About $3 per 1,000 runs.",
    "version": "0.1",
    "x-build-id": "Do08733o7ZXXU81L2"
  },
  "servers": [
    {
      "url": "https://api.apify.com/v2"
    }
  ],
  "paths": {
    "/acts/data_daemon~python-code-runner/run-sync-get-dataset-items": {
      "post": {
        "operationId": "run-sync-get-dataset-items-data_daemon-python-code-runner",
        "x-openai-isConsequential": false,
        "summary": "Executes an Actor, waits for its completion, and returns Actor's dataset items in response.",
        "tags": [
          "Run Actor"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/inputSchema"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "token",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Enter your Apify token here"
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/acts/data_daemon~python-code-runner/runs": {
      "post": {
        "operationId": "runs-sync-data_daemon-python-code-runner",
        "x-openai-isConsequential": false,
        "summary": "Executes an Actor and returns information about the initiated run in response.",
        "tags": [
          "Run Actor"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/inputSchema"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "token",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Enter your Apify token here"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/runsResponseSchema"
                }
              }
            }
          }
        }
      }
    },
    "/acts/data_daemon~python-code-runner/run-sync": {
      "post": {
        "operationId": "run-sync-data_daemon-python-code-runner",
        "x-openai-isConsequential": false,
        "summary": "Executes an Actor, waits for completion, and returns the OUTPUT from Key-value store in response.",
        "tags": [
          "Run Actor"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/inputSchema"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "token",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Enter your Apify token here"
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "inputSchema": {
        "type": "object",
        "properties": {
          "code": {
            "title": "Python code",
            "maxLength": 200000,
            "type": "string",
            "description": "Python 3.12. Define main(rows, ctx) and return a list of dicts, one dict, a pandas DataFrame or None; or write a plain script that sets a variable named result. rows is the list of input rows. ctx.df() gives them as a DataFrame, ctx.sql('SELECT ... FROM rows') runs DuckDB SQL on them, ctx.input holds the values of the Parameters field, ctx.save_file(name, data) saves a file. Preinstalled: pandas, numpy, duckdb, pyarrow, openpyxl, requests, httpx, beautifulsoup4, lxml. Leave empty to run only the SQL."
          },
          "sql": {
            "title": "SQL (DuckDB), optional",
            "maxLength": 100000,
            "type": "string",
            "description": "A DuckDB query over the input rows, which are the table named rows. Example: SELECT category, sum(price * qty) AS total FROM rows GROUP BY category ORDER BY total DESC. With SQL only, the query result is the output. With SQL and Python, the SQL runs first and its result is what main receives as rows."
          },
          "rows": {
            "title": "Input rows",
            "type": "array",
            "description": "JSON objects passed to your code, up to 50,000, for example the items of the previous n8n, Make or Zapier step. Ignored when a Dataset ID is set."
          },
          "datasetId": {
            "title": "Dataset ID",
            "type": "string",
            "description": "An Apify Dataset whose items are the input rows, for example the Dataset of another Actor's run. The run gets read access to this Dataset only. In an integration use {{resource.defaultDatasetId}}."
          },
          "maxRows": {
            "title": "Max Dataset rows",
            "minimum": 1,
            "maximum": 200000,
            "type": "integer",
            "description": "How many Dataset items to read in this run (1 to 200,000). The rows are held in memory: give the run more memory for large Datasets.",
            "default": 10000
          },
          "datasetOffset": {
            "title": "Dataset offset",
            "minimum": 0,
            "type": "integer",
            "description": "First Dataset item to read (0-based). Use it with Max Dataset rows to work through a large Dataset in several runs.",
            "default": 0
          },
          "fields": {
            "title": "Dataset fields to read",
            "maxItems": 100,
            "type": "array",
            "description": "Optional top-level field names to read from each Dataset item. Leave empty for whole items; fewer fields use less memory.",
            "items": {
              "type": "string"
            }
          },
          "input": {
            "title": "Parameters",
            "type": "object",
            "description": "Any JSON object. Your code reads it as ctx.input, for example ctx.input[\"minPrice\"]. Lets you save one piece of code as several Tasks with different settings."
          },
          "packages": {
            "title": "pip packages",
            "maxItems": 15,
            "type": "array",
            "description": "Extra packages to install before the code runs, up to 15, for example polars or rapidfuzz==3.9.0. Plain names with an optional version; URLs, paths and pip options are not accepted. Installation time counts as compute time.",
            "items": {
              "type": "string"
            }
          },
          "env": {
            "title": "Environment variables",
            "type": "object",
            "description": "Stored encrypted. Your code reads them with os.environ[\"NAME\"]: the place for API keys and passwords your script needs. Keep them out of the code."
          },
          "timeoutSecs": {
            "title": "Time limit for the code (seconds)",
            "minimum": 5,
            "maximum": 900,
            "type": "integer",
            "description": "The code is stopped after this many seconds (5 to 900). Default 60.",
            "default": 60
          },
          "failRunOnError": {
            "title": "Fail the run when the code fails",
            "type": "boolean",
            "description": "Off (default): the run succeeds and the Dataset gets one row with _status, error and traceback, so a workflow can branch on it. On: the run ends as FAILED, which stops an n8n, Make or Zapier workflow.",
            "default": false
          }
        }
      },
      "runsResponseSchema": {
        "type": "object",
        "properties": {
          "data": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              },
              "actId": {
                "type": "string"
              },
              "userId": {
                "type": "string"
              },
              "startedAt": {
                "type": "string",
                "format": "date-time",
                "example": "2025-01-08T00:00:00.000Z"
              },
              "finishedAt": {
                "type": "string",
                "format": "date-time",
                "example": "2025-01-08T00:00:00.000Z"
              },
              "status": {
                "type": "string",
                "example": "READY"
              },
              "meta": {
                "type": "object",
                "properties": {
                  "origin": {
                    "type": "string",
                    "example": "API"
                  },
                  "userAgent": {
                    "type": "string"
                  }
                }
              },
              "stats": {
                "type": "object",
                "properties": {
                  "inputBodyLen": {
                    "type": "integer",
                    "example": 2000
                  },
                  "rebootCount": {
                    "type": "integer",
                    "example": 0
                  },
                  "restartCount": {
                    "type": "integer",
                    "example": 0
                  },
                  "resurrectCount": {
                    "type": "integer",
                    "example": 0
                  },
                  "computeUnits": {
                    "type": "integer",
                    "example": 0
                  }
                }
              },
              "options": {
                "type": "object",
                "properties": {
                  "build": {
                    "type": "string",
                    "example": "latest"
                  },
                  "timeoutSecs": {
                    "type": "integer",
                    "example": 300
                  },
                  "memoryMbytes": {
                    "type": "integer",
                    "example": 1024
                  },
                  "diskMbytes": {
                    "type": "integer",
                    "example": 2048
                  }
                }
              },
              "buildId": {
                "type": "string"
              },
              "defaultKeyValueStoreId": {
                "type": "string"
              },
              "defaultDatasetId": {
                "type": "string"
              },
              "defaultRequestQueueId": {
                "type": "string"
              },
              "buildNumber": {
                "type": "string",
                "example": "1.0.0"
              },
              "containerUrl": {
                "type": "string"
              },
              "usage": {
                "type": "object",
                "properties": {
                  "ACTOR_COMPUTE_UNITS": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATASET_READS": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATASET_WRITES": {
                    "type": "integer",
                    "example": 0
                  },
                  "KEY_VALUE_STORE_READS": {
                    "type": "integer",
                    "example": 0
                  },
                  "KEY_VALUE_STORE_WRITES": {
                    "type": "integer",
                    "example": 1
                  },
                  "KEY_VALUE_STORE_LISTS": {
                    "type": "integer",
                    "example": 0
                  },
                  "REQUEST_QUEUE_READS": {
                    "type": "integer",
                    "example": 0
                  },
                  "REQUEST_QUEUE_WRITES": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATA_TRANSFER_INTERNAL_GBYTES": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATA_TRANSFER_EXTERNAL_GBYTES": {
                    "type": "integer",
                    "example": 0
                  },
                  "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                    "type": "integer",
                    "example": 0
                  },
                  "PROXY_SERPS": {
                    "type": "integer",
                    "example": 0
                  },
                  "PROXY_UNBLOCKER_UNITS": {
                    "type": "integer",
                    "example": 0
                  }
                }
              },
              "usageTotalUsd": {
                "type": "number",
                "example": 0.00005
              },
              "usageUsd": {
                "type": "object",
                "properties": {
                  "ACTOR_COMPUTE_UNITS": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATASET_READS": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATASET_WRITES": {
                    "type": "integer",
                    "example": 0
                  },
                  "KEY_VALUE_STORE_READS": {
                    "type": "integer",
                    "example": 0
                  },
                  "KEY_VALUE_STORE_WRITES": {
                    "type": "number",
                    "example": 0.00005
                  },
                  "KEY_VALUE_STORE_LISTS": {
                    "type": "integer",
                    "example": 0
                  },
                  "REQUEST_QUEUE_READS": {
                    "type": "integer",
                    "example": 0
                  },
                  "REQUEST_QUEUE_WRITES": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATA_TRANSFER_INTERNAL_GBYTES": {
                    "type": "integer",
                    "example": 0
                  },
                  "DATA_TRANSFER_EXTERNAL_GBYTES": {
                    "type": "integer",
                    "example": 0
                  },
                  "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                    "type": "integer",
                    "example": 0
                  },
                  "PROXY_SERPS": {
                    "type": "integer",
                    "example": 0
                  },
                  "PROXY_UNBLOCKER_UNITS": {
                    "type": "integer",
                    "example": 0
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}