Writing code directly inside your task.

To get started with a Script task, paste your custom script inline in your YAML workflow definition along with any other configuration.

yaml
id: api_json_to_mongodb
namespace: company.team
tasks:
  - id: extract
    type: io.kestra.plugin.scripts.python.Script
    containerImage: python:3.11-slim
    beforeCommands:
      - pip install requests kestra > /dev/null
    warningOnStdErr: false
    outputFiles:
      - "*.json"
    script: |
      import requests
      import json
      from kestra import Kestra

      response = requests.get("https://api.github.com")
      data = response.json()

      with open("output.json", "w") as output_file:
          json.dump(data, output_file)

      Kestra.outputs({"status": response.status_code})

  - id: load
    type: io.kestra.plugin.mongodb.Load
    connection:
      uri: mongodb://host.docker.internal:27017/
    database: local
    collection: github
    from: "{{ outputs.extract.outputFiles['output.json'] }}"
    description: "you can start MongoDB using: docker run -d mongo"

The example above uses a Python script added as a multiline string into the script property. The script fetches data from an API and stores it as a JSON file in Kestra's internal storage using the outputFiles property. The Kestra.outputs method allows to capture additional output variables, such as the API response status code.

The image argument of the docker property allows to optionally specify the Docker image to use for the script. If you don't specify an image, Kestra will use the default image for the language you are using. In this example, we use the python:3.11-slim image.

You can also optionally use the beforeCommands property to install libraries used in your inline script. Here, we use the command pip install requests kestra to install pip packages not available in the base image python:3.11-slim.

The warningOnStdErr property allows to specify whether to set the task run to a WARNING status if the script writes to the standard error stream. By default, this property is set to true. Keep in ming that a script that generates an error will always set the task run to a FAILED status, regardless of the value of this property.

Was this page helpful?