> For the complete documentation index, see [llms.txt](https://docs.dckapintegrator.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dckapintegrator.com/tutorials/workflow-builder-tutorial/2.-hello-workflow/2.8-lets-write-code.md).

# 2.8 👨‍💻 Let's Write Code

Sometimes the built-in tools may not be enough to handle your specific data transformation needs. That is where Code Runner comes in — it allows you to write your own custom Python code inside the workflow, making use of previous step results and returning transformed data.

⚠️ **Warning: Always try to use the available built-in tools as much as possible before reaching for Code Runner. Using Code Runner unnecessarily may affect workflow performance.**&#x20;

{% stepper %}
{% step %}

### Initialize Variable

Create a new batch workflow. Initialize a variable called orders as JSON with the following value:

```
[
  {"order_id": 1, "amount": 500},
  {"order_id": 2, "amount": 1200},
  {"order_id": 3, "amount": 300},
  {"order_id": 4, "amount": 850}
]
```

<figure><img src="/files/77Ddru0VhSUaSvQRc3sB" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Add Code Runner

Add a Code Runner tool. In the Inputs section, use the initialized variable:

| Name   | Value                  |
| ------ | ---------------------- |
| orders | `{{variables.orders}}` |

In the coding section, write the following Python code to sort the orders by amount:

```
sorted_orders = sorted(inputs["orders"], key=lambda x: x["amount"])
response = sorted_orders
```

<figure><img src="/files/E59sxcCNWeh4kVrGteqD" alt=""><figcaption></figcaption></figure>

Give title and enable Show Output in Console Log
{% endstep %}

{% step %}

### Save and Sync

Enable Console Logs for the workflow, save the workflow and click Sync Now. Check the logs.

**Expected Output:**

```
[
  {"order_id": 3, "amount": 300},
  {"order_id": 1, "amount": 500},
  {"order_id": 4, "amount": 850},
  {"order_id": 2, "amount": 1200}
]
```

#### &#x20;🎉 You have successfully written custom Python code inside your workflow to sort data!

📌 **Things to Keep in Mind:**

**Allowed Packages:** `json, datetime, math, re, time, uuid, base64, hashlib`

**Allowed Built-in Methods:** `abs, all, any, bool, dict, enumerate, filter, float, format, int, isinstance, len, list, map, max, min, range, round, set, sorted, sum, str, tuple, type, zip and all exception classes`

⚠️ **Note:** Any packages or built-in functions outside the above list cannot be used or imported. **The maximum execution time is 30 seconds.**

\ <br>
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.dckapintegrator.com/tutorials/workflow-builder-tutorial/2.-hello-workflow/2.8-lets-write-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
