# Create an app trigger

> Watch tables of a connection and run a workflow's sync trigger step or a genie's Trigger interface on each matching change, from now on (past records are not replayed). The trigger is switched on and saved on the workflow's draft step or the genie's interface; a draft saved meanwhile is refused with 409 and nothing is created. Send an Idempotency-Key to retry safely.

Source: https://docs.stacksync.com/api-reference/app-triggers/create_app_trigger

`POST https://api.stacksync.com/v1/workspaces/{workspace_id}/app-triggers`

- Operation: `create_app_trigger`
- Resource: App triggers
- 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 |
| --- | --- | --- | --- |
| `target` | object (AppTriggerTargetToCreate) | Yes |  |
| `target.type` | string (workflow \| genie) | Yes |  |
| `target.id` | string | Yes | The workflow or the genie |
| `target.step_id` | string \| null | No | For a workflow: the sync trigger step that runs on each change (its workflow_module_instance_id) |
| `target.interface_id` | string \| null | No | For a genie: its Trigger interface (type app_trigger) |
| `connection_id` | string | Yes | A connection of the app the workflow step or the genie watches |
| `tables` | array of object (AppTriggerTable), at least 1 item, at most 100 items | Yes |  |
| `tables[].table_name` | string, at least 1 characters | Yes | A table of the connection's app, as the app's tables are listed for that connection (GET app schema; MCP: connections_tables) |
| `tables[].operations` | array of string (created \| updated \| deleted), at least 1 item | Yes | The record changes that run the target |
| `tables[].filters` | object (TriggerFilterGroup) | No | The conditions a changed record must meet, on the table's column names, as on a sync trigger. An empty `and` group matches every change |
| `tables[].filters.type` | string, default "group" | No | `group` |
| `tables[].filters.operator` | string (and \| or) | Yes | How the conditions combine |
| `tables[].filters.conditions` | array of object (TriggerFilterGroup) \| object (TriggerFilterCondition) | No | A list (never a single object): each entry is a condition or a nested group |
| `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 |
| --- | --- | --- | --- |
| `app_trigger_id` | string | Yes |  |
| `target` | object (AppTriggerTarget) | Yes | What runs on each change |
| `target.type` | string (workflow \| genie) | Yes |  |
| `target.id` | string | Yes | The workflow or the genie |
| `target.step_id` | string \| null | No | For a workflow: the sync trigger step that runs on each change (its workflow_module_instance_id) |
| `target.interface_id` | string \| null | No | For a genie: its Trigger interface (type app_trigger) |
| `connection_id` | string | Yes | The connection whose app is watched |
| `tables` | array of object (AppTriggerTable) | Yes | One entry per trigger: the table, its record changes, filters |
| `tables[].table_name` | string, at least 1 characters | Yes | A table of the connection's app, as the app's tables are listed for that connection (GET app schema; MCP: connections_tables) |
| `tables[].operations` | array of string (created \| updated \| deleted), at least 1 item | Yes | The record changes that run the target |
| `tables[].filters` | object (TriggerFilterGroup) | No | The conditions a changed record must meet, on the table's column names, as on a sync trigger. An empty `and` group matches every change |
| `tables[].filters.type` | string, default "group" | No | `group` |
| `tables[].filters.operator` | string (and \| or) | Yes | How the conditions combine |
| `tables[].filters.conditions` | array of object (TriggerFilterGroup) \| object (TriggerFilterCondition) | No | A list (never a single object): each entry is a condition or a nested group |
| `status` | string (on \| paused) | Yes |  |
| `region` | string (besg \| usnv) | Yes |  |

### 204 No Content

No body.

## Example request

```bash
curl -X POST "https://api.stacksync.com/v1/workspaces/{workspace_id}/app-triggers?region=besg" \
  -H "Authorization: Bearer $STACKSYNC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "target": {
    "type": "workflow",
    "id": "<id>"
  },
  "connection_id": "<connection_id>",
  "tables": [
    {
      "table_name": "<table_name>",
      "operations": [
        "created"
      ]
    }
  ]
}'
```

## Example response (201)

```json
{
  "app_trigger_id": "<app_trigger_id>",
  "target": {
    "type": "workflow",
    "id": "<id>",
    "step_id": "<step_id>",
    "interface_id": "<interface_id>"
  },
  "connection_id": "<connection_id>",
  "tables": [
    {
      "table_name": "<table_name>",
      "operations": [
        "created"
      ],
      "filters": {
        "type": "group",
        "operator": "and"
      }
    }
  ],
  "status": "on",
  "region": "besg"
}
```

## 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
- [Get an app trigger](https://docs.stacksync.com/api-reference/app-triggers/get_app_trigger)
- [Update an app trigger](https://docs.stacksync.com/api-reference/app-triggers/update_app_trigger)
- [Delete an app trigger](https://docs.stacksync.com/api-reference/app-triggers/delete_app_trigger)