# Creating a RapidForm through API

> Source: https://truto.one/docs/guides/rapid-form/creating-rapid-form-api/

Follow the steps below to create a RapidForm through the Truto API.

:::skip-reading{skill="truto-link-sdk"}
:::

## Basic structure of a RapidForm

Taking the example of an Asana RapidForm which asks the user to select a workspace and its projects, it can be defined like so,

```json
{
  "type": "form",
  "config": {
    "fields": [
      {
        "name": "workspace_id",
        "type": "single_select",
        "label": "Workspace",
        "help_text": "The workspace to sync",
        "placeholder": "Select a workspace",
        "required": true,
        "data_source": {
          "type": "unified",
          "resource": "ticketing/workspaces",
          "method": "list"
        },
        "options": {
          "value": "id",
          "label": "name",
          "subText": "id"
        }
      },
      {
        "name": "collections",
        "type": "multi_select",
        "label": "Projects",
        "depends_on": [
          "workspace_id"
        ],
        "help_text": "The projects to sync",
        "required": true,
        "data_source": {
          "type": "unified",
          "resource": "ticketing/collections",
          "method": "list",
          "query": {
            "workspace_id": "{{workspace_id.value}}"
          }
        },
        "disabled_text": "Please select a workspace",
        "options": {
          "value": "id",
          "label": "name",
          "subText": "id"
        }
      }
    ]
  }
}
```

- `type` specifies that it's a `form`.
- `config.fields` is an array containing all the form fields. The order of the array items dictates the order of the form fields in the UI.

### Field reference

Each item in `config.fields` takes these keys. The last column is the matching setting in the RapidForm editor on the installed integration's **Configuration** tab; keys marked "API only" have no editor setting.

| Key | What it does | Editor label |
| --- | --- | --- |
| `name` | The variable the value is stored under in the integrated account's `context`. It's the name shown in the **Variables** section and the placeholder you use in a sync job. | **Name** |
| `type` | `single_select`, `multi_select`, `text`, `password`, `checkbox` or `hidden`. Through the API you can also use `link`, which shows `label` as a link to the field's `url` and collects no value. A single select field stores the selected option as an object with `value`, `label` and `subText`, and a multi select field stores an array of these objects. Use the `value` attribute to get the selected ID, like `{{workspace_id.value}}`. | **Type** |
| `label` | The label of the field that the end user sees. | **Label** |
| `help_text` | Help text shown below the field. | **Help text** |
| `disabled_text` | Shown in place of the help text while the field is disabled, for example while it waits on the value of another field. It works for every field type except `link`. In the example above, the Projects field shows it until a workspace is selected. | **Disabled help text** |
| `required` | The end user must fill in the field before saving. | **Required** |
| `placeholder` | Placeholder text shown in an empty text, password or single select field. | API only |
| `default` | The value the field starts with when the account has none stored. | API only |
| `format` | For a text field: `number`, `email`, `uuid` or `url`. **Save** checks the value against it. | API only |
| `url` | For a `link` field: the URL the link opens. Only an `https:` URL is shown; with any other value, the field doesn't appear. | API only |
| `depends_on` | Names of the fields this field depends on. Its data is fetched once they have values, and again when they change. In the example above, Projects depends on `workspace_id`. Multiple dependencies can be specified. | **Depends on** |
| `should_reset_on_dependency_change` | Clears the field's value when a field it depends on changes. Defaults to `true`. | **Reset value on parent dependency change** |
| `data_source.type` | `unified` for Unified API data, `proxy` for Proxy API data. | **Request type** |
| `data_source.resource` | The resource to fetch from. For a Unified API, the format is `unified_api_name/resource_name`, such as `ticketing/workspaces`. | **Resource to fetch** |
| `data_source.method` | `list`, `get` (set `data_source.id`), or a custom method of the resource, which is called with `POST`. | **Method to call** |
| `data_source.id` | The record ID for `get`. Placeholders work here. | **ID** |
| `data_source.query` | The query parameters to send. Placeholders refer to the values of other fields in the form: in the example above, the Projects query reads the selected workspace's ID with `{{workspace_id.value}}`. | **Query in JSON format** |
| `enable_auto_pagination` | Fetches every page of the list as soon as the field loads. See [Field Types](/docs/guides/rapid-form/field-types). | **Enable Auto Pagination** |
| `transform_expression` | A JSONata expression Truto runs on each page's `result`. Used only when `enable_auto_pagination` is on. | **Transformation expression (JSONata)** |
| `hide_if_expression` | A JSONata expression run against the account's `context` and `integration` when the form opens. If it returns `true`, the field is hidden. | **Show conditionally** |
| `options.value` | The attribute of each record to use as the option's value. Defaults to `value`. | **Value attribute for the Select field** |
| `options.label` | The attribute of each record to show as the option's label. Defaults to `label`. | **Label attribute for the Select field** |
| `options.subText` | The attribute of each record to show under the label, to tell apart options with the same label. Defaults to `subText`. | **Subtext attribute for the Select field** |
| `options.parent` | Multi Select: the attribute holding the value of another option in the same list, such as a parent folder's ID. The option is nested under that one. | **Parent attribute for the Select field** |
| `options.parentAttribute` | The attribute that other options' `options.parent` values are matched against. Defaults to the value attribute. | **Attribute in parent to match value** |
| `options.noLabelText` | Shown for an option whose label attribute is empty. Defaults to `No label`. | **No label text** |
| `noun` | Multi Select with search on: with `project`, the button reads "Select projects" and the count "3 projects selected". | **Noun used for the field** |
| `empty_options.title` | Multi Select: shown when the list is empty. Defaults to `No results returned`. | **No results title** |
| `searchEnabled` | Multi Select: adds a search box. | **Enable search** |
| `search_options.searchField` | The query parameter the search text is sent in. | **Attribute to use for Search** |
| `search_options.searchFieldPlaceHolder` | Placeholder text in the search box. | **Placeholder for Search input field** |
| `search_options.searchOnFrontend` | Searches the options already loaded, by `search_options.searchField`, instead of calling the API. | **Search on frontend** |
| `search_options.empty_result.title` | Shown when a search matches nothing. Defaults to `No options matched the search criteria`. | **Empty search result message** |
| `objectStore` | Multi Select: set it when the selection can be large (1,000 values or more). | **High cardinality** |

### Validation and transform

Set `config.validation_expression` and `config.transform_expression` on the form step, next to `fields`, as JSONata strings. See [Custom Validation](/docs/guides/rapid-form/custom-validation) and [Transform Expressions](/docs/guides/rapid-form/transform-expressions).

## Get the installed integration

To create the RapidForm defined above, we'll first need to get the `id` of the installed Asana integration. `integration.name` filters the installed integrations by the integration's identifier.

:::code-tabs

```bash label="cURL"
curl -G https://api.truto.one/environment-integration \
  -H "Authorization: Bearer <API token>" \
  --data-urlencode "integration.name=asana"
```

```bash label="CLI"
truto environment-integrations list --integration-name asana -o json
```

```typescript label="TypeScript"
import TrutoApi from '@truto/truto-ts-sdk'

const trutoApi = new TrutoApi({ token: process.env.TRUTO_API_TOKEN! })

const installed = await trutoApi.environmentIntegration
  .list({ 'integration.name': 'asana' })
  .toArray()
console.log(installed[0].id)
```

```python label="Python"
import asyncio
from truto_python_sdk import TrutoApi

truto_api = TrutoApi(token="<API token>")

async def main():
    installed = await truto_api.environment_integrations.list(
        **{"integration.name": "asana"}
    ).to_array()
    print(installed[0]["id"])

asyncio.run(main())
```

:::

The response:

```json
{
  "result": [
    {
      "id": "09b8d7dd-d7c1-42ba-8fae-d3c9278ade35",
      "integration_id": "91dcffb7-d9db-4c70-9ac8-91c71578f9a0",
      "is_enabled": true,
      "override": {}
    }
  ],
  "next_cursor": null,
  "limit": 500
}
```

Use `result[0].id` in the steps going forward for the installed integration ID.

## Add RapidForm to the installed integration

The form definition goes at `override.actions.post_connect_user_form.steps` on the installed integration. `PATCH /environment-integration/{id}` replaces the installed integration's whole `override`, so read it first and send it back with the form added. The response leaves out OAuth client secrets and shows `_secret` environment variables as `********`; send it back as it is and both are kept.

Save the form from [Basic structure of a RapidForm](#basic-structure-of-a-rapidform) in a file named `rapidform.json`, inside a `steps` array: `{"steps": [<the form>]}`. Then add it to the override:

:::code-tabs

```bash label="cURL"
curl -s https://api.truto.one/environment-integration/<environment_integration_id> \
  -H "Authorization: Bearer <API token>" | jq '.override // {}' > override.json
jq --slurpfile form rapidform.json '.actions.post_connect_user_form = $form[0]' override.json > new-override.json
curl -X PATCH https://api.truto.one/environment-integration/<environment_integration_id> \
  -H "Authorization: Bearer <API token>" \
  -H "Content-Type: application/json" \
  -d "{\"override\": $(cat new-override.json)}"
```

```bash label="CLI"
truto environment-integrations show-override <environment_integration_id> > override.json
jq --slurpfile form rapidform.json '.actions.post_connect_user_form = $form[0]' override.json > new-override.json
truto environment-integrations update <environment_integration_id> -b "{\"override\": $(cat new-override.json)}"
```

```typescript label="TypeScript"
import { readFile } from 'node:fs/promises'
import TrutoApi from '@truto/truto-ts-sdk'

const trutoApi = new TrutoApi({ token: process.env.TRUTO_API_TOKEN! })
const rapidForm = JSON.parse(await readFile('rapidform.json', 'utf8'))

const installed = await trutoApi.environmentIntegration.get('<environment_integration_id>')
const override = installed.override ?? {}

await trutoApi.environmentIntegration.update('<environment_integration_id>', {
  override: {
    ...override,
    actions: {
      ...(override.actions as Record<string, unknown> | undefined),
      post_connect_user_form: rapidForm,
    },
  },
})
```

```python label="Python"
import asyncio
import json
from truto_python_sdk import TrutoApi

truto_api = TrutoApi(token="<API token>")

async def main():
    with open("rapidform.json") as f:
        rapid_form = json.load(f)
    installed = await truto_api.environment_integrations.get("<environment_integration_id>")
    override = installed.get("override") or {}
    override.setdefault("actions", {})["post_connect_user_form"] = rapid_form
    await truto_api.environment_integrations.update(
        "<environment_integration_id>", {"override": override}
    )

asyncio.run(main())
```

:::

If you overwrote an override by mistake, open the installed integration's menu, choose **Version history**, and restore the earlier version. If that version had your own OAuth app, enter its client ID and secret again first: versions keep no secrets, so a restore whose client ID differs from the current one is refused.

## Accessing RapidForm values in a sync job

The values selected by the user in a RapidForm can be accessed using placeholders in a sync job like shown below. The example is of a sync job which fetches tasks in selected projects in Asana. [Learn more about sync jobs](/docs/guides/rapid-bridge/creating-rapid-bridge).

```json
{
    "integration_name": "asana",
    "args_schema": {},
    "resources": [
        {
            "resource": "ticketing/collections",
            "method": "get",
            "loop_on": "collections",
            "id": "{{collections.value}}"
        },
        {
            "resource": "ticketing/tickets",
            "method": "list",
            "depends_on": "ticketing/collections",
            "query": {
                "collection_id": "{{resources.ticketing.collections.id}}"
            }
        }
    ]
}
```

In the example above, `loop_on` is set to `collections`, the `name` attribute of the Projects field in the RapidForm defined above, so a request is made for each selected project. Inside the loop, `collections` holds one selected option, and <span v-pre>`{{collections.value}}`</span> is that project's ID.

## Next

To check the end user's answers before they're saved, add [custom validation](/docs/guides/rapid-form/custom-validation); to change them before they're saved, add a [transform expression](/docs/guides/rapid-form/transform-expressions).
