# Code Patterns

Code Pattern is a special type of [component](https://help.keboola.com/components/) that

- generates code based on [parameters](https://help.keboola.com/transformations/code-patterns/#parameters-form), and
- can be used in the user interface of [New Transformations](https://help.keboola.com/transformations/).



**Before you start**

You need:

- **A transformation type that a code pattern supports.** The public SCD code pattern works with Snowflake SQL and Synapse transformations. A pattern shows up in **Use predefined code pattern** only for the transformation types it lists.
- **A project role that can create configurations.** The **ReadOnly** role cannot. In projects with Governed Change Management, Developers and Reviewers work in a development branch and can't edit the production branch. See user roles (/management/project/users/#user-roles).

What each tab needs:

- **Prompt tab:** Kai, the **Kai Agent** button in the top bar, on supported stacks. A new Free Plan project has it from the start; elsewhere an organization admin turns it on once: Get started with Kai (/kai/getting-started/).
- **UI tab:** a browser.
- **CLI / API tab:** kbagent (/cli/getting-started/), connected to your project. The checks only read.


## List of Code Patterns

All components published with **Code Pattern** can be found in the Keboola 
[Developer Portal](https://components.keboola.com/components). To find out which transformation types a component can be used for, read its description.

![Screenshot -- Transformations page](https://help.keboola.com/transformations/code-patterns/overview-0-dev-portal.png)

## New Transformation with Code Pattern

This section describes how to create a new transformation with a code pattern.

First, click **Transformations** on the project menu.

Then click **New Transformation** to create a new transformation.

![Screenshot -- Transformations page](https://help.keboola.com/transformations/code-patterns/overview-1-intro.png)

In the modal, click the selected **type of the transformation**.

![Screenshot -- Add new transformation modal](https://help.keboola.com/transformations/code-patterns/overview-2-modal.png)

Fill in the **name** and, optionally, a **description**. 

Then select a code pattern from the **Use predefined code pattern** select box.

If a code pattern is missing in the list, please check whether it supports the selected transformation type 
and it is published.

Click the **Create Transformation** button.

![Screenshot -- Select box for code pattern](https://help.keboola.com/transformations/code-patterns/overview-3-new.png)

A new transformation has been created and you can see its configuration.

You can now fill in the [Parameters Form](https://help.keboola.com/transformations/code-patterns/#parameters-form) and click the **Generate Code** button.

Follow the instructions at the top of the configuration page.

## Configuration

![Screenshot -- Code pattern user interface](https://help.keboola.com/transformations/code-patterns/overview-4-ui.png)

The user interface of the configuration consists of the following:
1. Instructions on how to set up the code pattern correctly
2. [Table Input Mapping](https://help.keboola.com/transformations/mappings/input/#table-input-mapping)
3. [Table Output Mapping](https://help.keboola.com/transformations/mappings/output/#table-output-mapping)
4. [Parameters Form](https://help.keboola.com/transformations/code-patterns/#parameters-form) of the code pattern
5. **Generate Code** button
6. Read-only [generated code](https://help.keboola.com/transformations/code-patterns/#generated-code)

### Input and Output Mapping

Table [Input](https://help.keboola.com/transformations/mappings/input/#table-input-mapping) 
and [Output](https://help.keboola.com/transformations/mappings/output/#table-output-mapping) Mapping can be created either
- **manually** as in ordinary transformations, or
- **automatically**, generated together with the code if the component supports it.

Follow the instructions at the top of the configuration page.

Below you can see an example mapping. It was generated by the `Snapshot - SCD` code pattern.

![Screenshot -- Input / Output mapping](https://help.keboola.com/transformations/code-patterns/overview-7-mapping.png)

### Parameters Form

The parameters determine the content of the generated code.
The form is generated dynamically based on the component specification in the Keboola 
[Developer Portal](https://components.keboola.com/components). Remember to save your changes.

![Screenshot -- Parameters](https://help.keboola.com/transformations/code-patterns/overview-5-parameters.png)

### Generated Code

Generated code is read only, it cannot be adjusted manually. 
It is (re)generated by clicking the **Regenerate Code** button. 
This calls the [Generate Action](https://developers.keboola.com/extend/component/code-patterns/interface#generate-action) 
on the code pattern component with the actual parameters. The result is then saved and displayed.

![Screenshot -- Generated Code](https://help.keboola.com/transformations/code-patterns/overview-6-code.png)

After the code has been generated, you can [run the job](https://help.keboola.com/management/jobs/#running-jobs) the standard way.

**Check:**


#### Prompt


```text title="Prompt" wrap
Does the transformation my-transformation have generated code, and did its latest job succeed?
```

Kai only reads here, so it asks for no approval, and its answer links to what it found in your project.


#### UI


The generated code shows up read-only in the transformation, next to a **Regenerate Code** button,
and the job detail of the run shows the status **Success**.


#### CLI / API


```bash
kbagent job list --component-id keboola.snowflake-transformation --config-id 12345 --limit 1
```

Use your transformation's component and configuration IDs; the job's **Status** is `success`.


---
