# Create workflow

> Source: https://truto.one/docs/api-reference/admin/workflows/create/

`POST /workflow`

Resource: **Workflows**

Creates a workflow configuration that listens to a trigger and executes one or more steps when the trigger occurs.
The workflow configuration supports JSONata expressions for: - Conditional execution (`run_if`) - Dynamic step configuration (`config`)
Cron expressions can be provided at the step level to schedule execution.

## Request body

- **`id`** _(string)_
  Optional workflow ID. If not provided, it will be generated.
- **`environment_id`** _(string)_
  The environment in which this workflow is created. Ignored for API tokens, which create in their own environment. A dashboard session should send it.
- **`trigger_name`** _(string)_
  The event that runs the workflow. `integrated_account:active` runs when an integrated account becomes active (for example right after it is connected); `unified_webhook:batch` runs when a batch of unified webhook events arrives. A `unified_webhook:batch` run is delivered at least once. After a failed hand-off it can be sent again, or carry the events of several deliveries, so dedupe on an id each event carries, such as its `event_id` where present or the provider's own id in `raw_payload`.
  Allowed: `integrated_account:active`, `unified_webhook:batch`
- **`config`** _(object, required)_
  Workflow execution configuration.
  - **`run_if`** _(string)_
    JSONata condition evaluated before executing workflow steps.
  - **`steps`** _(array<object>)_
    - **`type`** _(string)_
      Step execution type.
      Allowed: `run`
    - **`action`** _(string)_
      Action executed by the step. `run_sync_job` is the only action.
      Allowed: `run_sync_job`
    - **`run_if`** _(string)_
      JSONata condition; the step is skipped when it is falsy.
    - **`cron_expression`** _(string)_
      Optional cron expression for scheduled execution, in UTC. When set, the step creates a sync job cron trigger instead of starting a run, so the trigger's check applies: the step fails if the expression doesn't parse or its next two runs are less than 5 minutes apart.
    - **`config`** _(string | object)_
      A JSONata expression that returns the step's payload, or an object whose values may use `{{placeholders}}`. For `run_sync_job` it must give a `sync_job_id`.

## Response body

- **`id`** _(string)_
  The ID of the workflow.
- **`environment_id`** _(string)_
  The environment the workflow belongs to.
- **`trigger_name`** _(string)_
  The event that runs the workflow. `integrated_account:active` runs when an integrated account becomes active (for example right after it is connected); `unified_webhook:batch` runs when a batch of unified webhook events arrives. A `unified_webhook:batch` run is delivered at least once. After a failed hand-off it can be sent again, or carry the events of several deliveries, so dedupe on an id each event carries, such as its `event_id` where present or the provider's own id in `raw_payload`.
  Allowed: `integrated_account:active`, `unified_webhook:batch`
- **`config`** _(object, required)_
  Workflow execution configuration.
  - **`run_if`** _(string)_
    JSONata condition evaluated before executing workflow steps.
  - **`steps`** _(array<object>)_
    - **`type`** _(string)_
      Step execution type.
      Allowed: `run`
    - **`action`** _(string)_
      Action executed by the step. `run_sync_job` is the only action.
      Allowed: `run_sync_job`
    - **`run_if`** _(string)_
      JSONata condition; the step is skipped when it is falsy.
    - **`cron_expression`** _(string)_
      Optional cron expression for scheduled execution, in UTC. When set, the step creates a sync job cron trigger instead of starting a run, so the trigger's check applies: the step fails if the expression doesn't parse or its next two runs are less than 5 minutes apart.
    - **`config`** _(string | object)_
      A JSONata expression that returns the step's payload, or an object whose values may use `{{placeholders}}`. For `run_sync_job` it must give a `sync_job_id`.
- **`created_at`** _(string)_
  When the workflow was created.
- **`updated_at`** _(string)_
  When the workflow was last updated.

## Errors

| Status | Description |
| :----- | :---------- |
| `400` | Invalid workflow configuration |

## Code examples

### Truto CLI

```bash
truto workflows create \
  -b '{
  "environment_id": "05daecaf-4365-42e8-8370-8127de5dd717",
  "trigger_name": "integrated_account:active",
  "config": {
    "run_if": "integration.name = 'calcom'",
    "steps": [
      {
        "type": "run",
        "action": "run_sync_job",
        "config": "(\n  $sync_job_id := $mapValues(integration.name,{\n    \"calcom\": \"51b79ace-a7b4-4077-93ba-8048a06ece2b\"\n  });\n  {\n    \"sync_job_id\": $sync_job_id,\n    \"integrated_account_id\": integrated_account_id,\n    \"webhook_id\": \"9fda519d-148f-4cc5-bbac-4e373b16e5d4\"\n  }\n)\n"
      }
    ]
  }
}' \
  -o json
```

### Truto TS SDK

```typescript
import Truto from '@truto/truto-ts-sdk';

const truto = new Truto({
  token: '<your_api_token>',
});

const body = {
  "environment_id": "05daecaf-4365-42e8-8370-8127de5dd717",
  "trigger_name": "integrated_account:active",
  "config": {
    "run_if": "integration.name = 'calcom'",
    "steps": [
      {
        "type": "run",
        "action": "run_sync_job",
        "config": "(\n  $sync_job_id := $mapValues(integration.name,{\n    \"calcom\": \"51b79ace-a7b4-4077-93ba-8048a06ece2b\"\n  });\n  {\n    \"sync_job_id\": $sync_job_id,\n    \"integrated_account_id\": integrated_account_id,\n    \"webhook_id\": \"9fda519d-148f-4cc5-bbac-4e373b16e5d4\"\n  }\n)\n"
      }
    ]
  }
};

const result = await truto.workflow.create(body);

console.log(result);
```

### Truto Python SDK

```python
import asyncio
from truto_python_sdk import TrutoApi

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

async def main():
    body = {
        "environment_id": "05daecaf-4365-42e8-8370-8127de5dd717",
        "trigger_name": "integrated_account:active",
        "config": {
            "run_if": "integration.name = 'calcom'",
            "steps": [
                {
                    "type": "run",
                    "action": "run_sync_job",
                    "config": "(\n  $sync_job_id := $mapValues(integration.name,{\n    \"calcom\": \"51b79ace-a7b4-4077-93ba-8048a06ece2b\"\n  });\n  {\n    \"sync_job_id\": $sync_job_id,\n    \"integrated_account_id\": integrated_account_id,\n    \"webhook_id\": \"9fda519d-148f-4cc5-bbac-4e373b16e5d4\"\n  }\n)\n"
                }
            ]
        }
    }

    result = await truto_api.workflows.create(body)
    print(result)

asyncio.run(main())
```

### curl

```bash
curl -X POST 'https://api.truto.one/workflow' \
  -H 'Authorization: Bearer <your_api_token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "environment_id": "05daecaf-4365-42e8-8370-8127de5dd717",
  "trigger_name": "integrated_account:active",
  "config": {
    "run_if": "integration.name = 'calcom'",
    "steps": [
      {
        "type": "run",
        "action": "run_sync_job",
        "config": "(\n  $sync_job_id := $mapValues(integration.name,{\n    \"calcom\": \"51b79ace-a7b4-4077-93ba-8048a06ece2b\"\n  });\n  {\n    \"sync_job_id\": $sync_job_id,\n    \"integrated_account_id\": integrated_account_id,\n    \"webhook_id\": \"9fda519d-148f-4cc5-bbac-4e373b16e5d4\"\n  }\n)\n"
      }
    ]
  }
}'
```

### JavaScript

```javascript
const body = {
  "environment_id": "05daecaf-4365-42e8-8370-8127de5dd717",
  "trigger_name": "integrated_account:active",
  "config": {
    "run_if": "integration.name = 'calcom'",
    "steps": [
      {
        "type": "run",
        "action": "run_sync_job",
        "config": "(\n  $sync_job_id := $mapValues(integration.name,{\n    \"calcom\": \"51b79ace-a7b4-4077-93ba-8048a06ece2b\"\n  });\n  {\n    \"sync_job_id\": $sync_job_id,\n    \"integrated_account_id\": integrated_account_id,\n    \"webhook_id\": \"9fda519d-148f-4cc5-bbac-4e373b16e5d4\"\n  }\n)\n"
      }
    ]
  }
};

const response = await fetch('https://api.truto.one/workflow', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <your_api_token>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(body),
});

const data = await response.json();
console.log(data);
```

### Python

```python
import requests

url = "https://api.truto.one/workflow"
headers = {
    "Authorization": "Bearer <your_api_token>",
    "Content-Type": "application/json",
}
params = {
}
payload = {
    "environment_id": "05daecaf-4365-42e8-8370-8127de5dd717",
    "trigger_name": "integrated_account:active",
    "config": {
        "run_if": "integration.name = 'calcom'",
        "steps": [
            {
                "type": "run",
                "action": "run_sync_job",
                "config": "(\n  $sync_job_id := $mapValues(integration.name,{\n    \"calcom\": \"51b79ace-a7b4-4077-93ba-8048a06ece2b\"\n  });\n  {\n    \"sync_job_id\": $sync_job_id,\n    \"integrated_account_id\": integrated_account_id,\n    \"webhook_id\": \"9fda519d-148f-4cc5-bbac-4e373b16e5d4\"\n  }\n)\n"
            }
        ]
    }
}

response = requests.post(url, headers=headers, params=params, json=payload)
print(response.json())
```
