# Create workflow

> Create a workflow with only its Input step, as the builder's New workflow does: its other steps are saved in its draft once it exists (PATCH /draft), which sets up their triggers. A document refused leaves no workflow behind.

Source: https://docs.stacksync.com/api-reference/workflows/create_workflow

`POST https://api.stacksync.com/v1/workspaces/{workspace_id}/workflows`

- Operation: `create_workflow`
- Resource: Workflows
- Access: writes data

## Authentication

- `Authorization: Bearer <key>`: A workspace API key (workspace_…, created by a workspace owner under Workspace settings > Stacksync API) acts with editor rights on that workspace only; a user API key (user_…, created under Profile > Stacksync API) acts as that user in every workspace they belong to. POST, PUT and PATCH requests must send Content-Type: application/json. Every authentication failure answers 403.
- `Content-Type: application/json` on every request with a body.

## Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `workspace_id` | integer | Yes | The workspace. List the ones your key can reach with GET /v1/workspaces. |

## Query parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `region` | string (besg \| usnv) | Yes | Region of the resource: besg (Europe) or usnv (United States), as GET /v1/regions lists them |

## Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string \| null | No | Makes the create retry-safe for 24 hours: a retry with the same key answers the resource the first call created, with the Idempotent-Replayed: true header, and creates nothing. 1 to 255 visible ASCII characters. The same key with a different request answers 422; while the first call still runs, 409. |

## Request body

`application/json`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `workflow_name` | string, 1 to 200 characters | Yes |  |
| `workflow_resource_id` | string \| null, pattern `^[a-z0-9_]{1,60}$` | No | A human-readable id, unique in the workspace (lowercase letters, digits and _, at most 60); derived from the name when absent |
| `folder_id` | string \| null | No | The folder to create it in; the root when absent |
| `is_shared_with_workspace` | boolean \| null | No |  |
| `document` | object (WorkflowSchemaModel) \| null | No | The first draft's document, holding only the Input step; an empty draft when absent |
| `document.workflow_execution_timeout` | integer \| null, default 300 | No |  |
| `document.workflow_module_execution_timeout` | integer \| null, default 300 | No |  |
| `document.return_workflow_module_instance_ids` | array of string \| null | No |  |
| `document.modules` | array of object (WorkflowSchemaModuleModel) | Yes |  |
| `document.modules[].name` | string | Yes |  |
| `document.modules[].workflow_module_instance_id` | string | Yes |  |
| `document.modules[].module_id` | string | Yes |  |
| `document.modules[].description` | string \| null | No |  |
| `document.modules[].properties` | object | Yes |  |
| `document.modules[].connections` | array of object (WorkflowModuleConnectionManagedModelByResourceId) \| object (WorkflowModuleConnectionManagedModelByName) \| object (WorkflowModuleConnectionUserDataModel), default [] | No |  |
| `document.modules[].module_options` | object (WorkflowSchemaModuleOptionsModel) \| null | No |  |
| `document.modules[].module_options.patches` | object \| null | No |  |
| `document.modules[].module_options.layout` | object (WorkflowSchemaModuleOptionsLayoutModel) \| null | No |  |
| `document.modules[].module_options.layout.group_module_instance_id` | string \| null | No |  |
| `document.modules[].module_options.enable_error_handling` | boolean \| null | No |  |
| `document.modules[].module_options.workflow_module_execution_timeout` | integer \| null | No |  |
| `document.modules[].error_handling_strategy` | object (WorkflowModuleErrorStrategyModel) \| null | No |  |
| `document.modules[].error_handling_strategy.conditions` | array of object (WorkflowModuleErrorStrategyConditionModel), default [] | No |  |
| `document.modules[].error_handling_strategy.conditions[].condition` | object (WorkflowModuleConditionGroupModel) \| object (WorkflowModuleConditionValueModel) \| object (WorkflowModuleConditionComparatorModel) | Yes |  |
| `document.modules[].error_handling_strategy.conditions[].retry_policy` | object (WorkflowModuleErrorStrategyConditionActionModel) | Yes |  |
| `document.modules[].error_handling_strategy.conditions[].retry_policy.retry_count` | integer, default 0 | No |  |
| `document.modules[].error_handling_strategy.conditions[].retry_policy.retry_interval` | integer, default 0 | No |  |
| `document.modules[].error_handling_strategy.else_condition` | object (WorkflowModuleErrorStrategyConditionActionModel) \| null | No |  |
| `document.modules[].error_handling_strategy.else_condition.retry_count` | integer, default 0 | No |  |
| `document.modules[].error_handling_strategy.else_condition.retry_interval` | integer, default 0 | No |  |
| `document.edges` | array of object (WorkflowSchemaEdgeModel) | Yes |  |
| `document.edges[].source` | object (WorkflowSchemaEdgeHandleModel) | Yes |  |
| `document.edges[].source.handle_id` | string \| null | No |  |
| `document.edges[].source.workflow_module_instance_id` | string | Yes |  |
| `document.edges[].target` | object (WorkflowSchemaEdgeHandleModel) | Yes |  |
| `document.edges[].target.handle_id` | string \| null | No |  |
| `document.edges[].target.workflow_module_instance_id` | string | Yes |  |
| `document.edges[].edge_id` | string | Yes |  |
| `validate_only` | boolean, default false | No | Run every check of the write and answer 204 without writing: a refused write answers the error the write would |

## Returns

### 201 Created

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `workflow_id` | string | Yes |  |
| `workflow_name` | string | Yes |  |
| `region` | string (besg \| usnv) | Yes |  |
| `version` | object (WorkflowVersionSummary) | Yes | A version without its document; step_ids lists its steps (workflow_module_instance_id) |
| `version.workflow_version` | string | Yes |  |
| `version.is_draft` | boolean | Yes |  |
| `version.is_deployed` | boolean | Yes |  |
| `version.last_modified_time` | string | Yes |  |
| `version.step_ids` | array of string | Yes |  |
| `triggers` | array of object (StepTrigger) | Yes |  |
| `triggers[].step_id` | string | Yes |  |
| `triggers[].status` | string (succeeded \| failed) | Yes | Whether what a step runs alongside the workflow (schedule, email route, Gmail watch, queue consumer...) is set up |
| `triggers[].msg` | string \| null | No |  |

### 204 No Content

No body.

## Example request

```bash
curl -X POST "https://api.stacksync.com/v1/workspaces/{workspace_id}/workflows?region=besg" \
  -H "Authorization: Bearer $STACKSYNC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "workflow_name": "<workflow_name>"
}'
```

## Example response (201)

```json
{
  "workflow_id": "<workflow_id>",
  "workflow_name": "<workflow_name>",
  "region": "besg",
  "version": {
    "workflow_version": "<workflow_version>",
    "is_draft": false,
    "is_deployed": false,
    "last_modified_time": "<last_modified_time>",
    "step_ids": [
      "<step_ids>"
    ]
  },
  "triggers": [
    {
      "step_id": "<step_id>",
      "status": "succeeded",
      "msg": "<msg>"
    }
  ]
}
```

## Errors

| Status | Meaning | Body |
| --- | --- | --- |
| 400 | Bad Request | `{ "error": string, "msg": string, "field": string \| null }` |
| 403 | Forbidden | `{ "error": string, "msg": string, "field": string \| null }` |
| 409 | Conflict | `{ "error": string, "msg": string, "field": string \| null }` |
| 422 | Unprocessable Content | `{ "error": string, "msg": string, "field": string \| null }` |

## Related

- [API Reference overview](https://docs.stacksync.com/api-reference): base URLs, regions, pagination, idempotency and errors
- [Authentication](https://docs.stacksync.com/api-reference/authentication): API keys and the 403 answers
- OpenAPI document: https://docs.stacksync.com/openapi.json
- [List workflows](https://docs.stacksync.com/api-reference/workflows/list_workflows)
- [List workflow versions](https://docs.stacksync.com/api-reference/workflows/list_workflow_versions)
- [Get workflow](https://docs.stacksync.com/api-reference/workflows/get_workflow)
- [Get workflow info](https://docs.stacksync.com/api-reference/workflows/get_workflow_info)
- [Create workflow draft](https://docs.stacksync.com/api-reference/workflows/create_workflow_draft)
- [Update workflow](https://docs.stacksync.com/api-reference/workflows/update_workflow)
- [Update workflow options](https://docs.stacksync.com/api-reference/workflows/update_workflow_options)
- [Delete workflow](https://docs.stacksync.com/api-reference/workflows/delete_workflow)
- [Run workflow](https://docs.stacksync.com/api-reference/workflows/run_workflow)
- [Deploy workflow](https://docs.stacksync.com/api-reference/workflows/deploy_workflow)
- [Duplicate workflow](https://docs.stacksync.com/api-reference/workflows/duplicate_workflow)
- [Save workflow draft](https://docs.stacksync.com/api-reference/workflows/save_workflow_draft)
- [Check a workflow email alias](https://docs.stacksync.com/api-reference/workflows/check_workflow_email_alias)