# Code

The Code piece in ZBrain Flow provides powerful scripting capabilities that allow you to execute custom code within your workflows. This component enables advanced data manipulation, complex calculations, custom algorithms, and more services that may not be covered by standard components. With the Code piece, you can write custom logic in either JavaScript/TypeScript or Python, depending on your preference and requirements.

### **How to Use the Code Piece in ZBrain Flow?** <a href="#how-to-use-the-code-piece-in-zbrain-flow" id="how-to-use-the-code-piece-in-zbrain-flow"></a>

**Step 1: Select Code as Your Connection**

* Click on the '+' button in the Flow and search for Code.
* Select Code.
* Decide on the action you need. ZBrain Flow provides two options:
  * **Custom JavaScript Code**: For Node.js & TypeScript code with npm support
  * **Custom Python Code**: For Python scripts and libraries

#### **How to Write Custom JavaScript Code?** <a href="#how-to-write-custom-javascript-code" id="how-to-write-custom-javascript-code"></a>

**Step 1: Set Up Your Inputs**

To pass data from previous steps into your code:

1. In the ‘Inputs’ section, click ‘Add Item’ for each piece of data you want to access.
2. For each input, specify:
   * A name for the input (this becomes the key to access it in your code)
   * The value from previous steps you want to pass in
3. In your code, access these inputs using `inputs.key` syntax (where "key" is the name you assigned)

**Step 2: Write Your JavaScript/TypeScript Code**

In the Code editor, write the standard JavaScript/Node.js syntax for your logic.

**Step 3: Add Dependencies (If Needed)**

If your code requires npm packages:

1. Click ‘Add package.’
2. Specify the package name you need, and the latest version will be fetched.
3. Click the ‘Add’ button.

#### **How to Write Custom Python Code?** <a href="#how-to-write-custom-python-code" id="how-to-write-custom-python-code"></a>

Once you select Custom Python Code as your action, follow the same approach as described in the JavaScript implementation for setting up inputs, writing your code, and configuring error handling options (Steps 1, 2 and 4). If you want to add Python packages, follow the below steps:

1. Click ‘Add packages.’
2. Select a requirement file from your device and press the ‘Add’ button.


---

# Agent Instructions: 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:

```
GET https://docs.zbrain.ai/zbrain-documentation/zbrain-builder/flows/flow-components/code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
