# Update sync job run

> Source: https://truto.one/docs/api-reference/admin/sync-job-runs/update/

`PATCH /sync-job-run/{id}`

Resource: **Sync job runs**

Updates a sync job run's `meta`, `error_handling`, `delay_between_requests_ms` and `resource_stats`. Sends a `sync_job_run:updated` webhook event.

Truto sets `status`, `started_at`, `finished_at` and `retry_attempt` as the run runs. A request that sends any of them, even as `null` or as the stored value, is a `400` and changes nothing: its `truto_error_code` is `sync_job_run_read_only_field`, and `fields` lists the fields it sent. To stop a run, use `DELETE /sync-job-run/{id}`. To run the sync job again, create a new run with `POST /sync-job-run`.

One exception keeps older scripts working: `"status": "stopped"`, sent without `started_at`, `finished_at` or `retry_attempt`, stops the run as `DELETE /sync-job-run/{id}` does, and the other fields sent with it are updated.

## Path parameters

- **`id`** _(string, required)_
  The ID of the sync job run to update.

## Request body

- **`error_handling`** _(string)_
  Allowed: `fail_fast`, `ignore`, `batch`
- **`meta`** _(object)_
  Replaces the stored `meta`.
- **`resource_stats`** _(object)_
  Replaces the stored `resource_stats`.
- **`delay_between_requests_ms`** _(number)_

## Response body

- **`id`** _(string)_
  The ID of the sync job run.
- **`args`** _(object)_
  The arguments passed to the sync job run.
- **`resources`** _(array<object>)_
  The resources to sync as part of the sync job.
  - **`name`** _(string)_
    The name of the node. Required on runtime version 4, where `depends_on` and destinations refer to nodes by name. Keep names unique within a sync job.
  - **`type`** _(string)_
    The node type. Required on runtime version 4. Each type reads its own fields; `resource`, `method`, `id`, `query` and `loop_on` apply to `request` nodes.
    Allowed: `request`, `transform`, `spool`, `add_context`, `destination`, `event`, `get_state`, `update_state`, `delete_state`
  - **`run_if`** _(string)_
    A JSONata expression. On runtime version 4 the node runs only when it evaluates to a truthy value.
  - **`resource`** _(string)_
    The name of the resource to sync. Required for `request` nodes and on runtime versions 1 to 3. For Unified APIs, it should be in the format `unified_api_name/resource_name`. For Proxy API, it can just be `resource_name`.
  - **`method`** _(string)_
    The method to call on the resource.
  - **`id`** _(string)_
    The ID of the resource to sync. This is optional and can be used to sync a single resource. It also supports placeholders.
  - **`query`** _(object | string)_
    The query parameters to pass to the resource method. An object whose values may use placeholders, or a JSONata string that returns the object.
  - **`body`** _(object | string)_
    `request` nodes: the request body. An object whose values may use placeholders, or a JSONata string that returns the object.
  - **`integrated_account_id`** _(string)_
    `request` nodes, runtime version 4: the integrated account to call. Required there, usually `{{args.integrated_account_id}}`. Versions 1 to 3 refuse it and call the run's own `integrated_account_id`. On version 4, `add_context` and `get_state` nodes also take it.
  - **`recurse`** _(object)_
    `request` nodes: call the same resource again for each returned record for which `if` is truthy, with `config.id` and `config.query` for the next call. Use it for nested data such as child pages.
    - **`if`** _(string | object)_
      A placeholder or JSONata condition, or `{ expression }` with a JSONata expression.
      _One of:_
      - **object**
        - **`expression`** _(string)_
    - **`config`** _(object)_
      - **`id`** _(string)_
      - **`query`** _(object | string)_
  - **`paginate_if`** _(string)_
    `request` nodes, runtime version 4: a JSONata expression. Truto fetches the next page only while it is truthy.
  - **`delay_after_ms`** _(number)_
    `request` nodes, runtime version 4: a pause in milliseconds after each call.
  - **`delete_tracking`** _(string)_
    `request` nodes, for runs that store Unified API records in Super Query: what happens to the stored records of this job and account that the run did not write. `hard` deletes them; `soft` marks them `truto_deleted`. Not used by runs that send to a webhook.
    Allowed: `hard`, `soft`
  - **`delete_tracking_query`** _(object)_
    With `delete_tracking`, a filter (placeholders allowed) that narrows which stored records are checked.
  - **`config`** _(object)_
    The settings of a non-`request` node. Its shape depends on `type`: for example `expression` on `transform` and `add_context` nodes, `key` on the state nodes, `name` on `event` nodes and `id` on `destination` nodes.
  - **`destination_type`** _(string)_
    `destination` nodes, runtime version 4: where the records go. `config.id` names the webhook or datastore.
    Allowed: `webhook`, `datastore`
  - **`resources_to_persist`** _(array<string>)_
    `destination` nodes, runtime version 4: the names of the nodes whose records this destination sends.
  - **`use_args`** _(boolean)_
    `transform` nodes, runtime version 4: set `true` when the parent is not a `request` node, such as a transform that builds records from `args`.
  - **`debug`** _(boolean)_
    Runtime version 4, any node: `true` logs a `transform` node's input and output.
  - **`loop_on`** _(string)_
    When a particular placeholder argument is an array and you want to repeat the request for each element in that array, you can set this parameter. For example, if you accept an argument called `user_ids` which is an array of strings, and you want to fetch each user's details, you can set this parameter to `args.user_ids` and the request will be repeated for each element in the `user_ids` array.
  - **`depends_on`** _(string)_
    The `name` of the parent node (on runtime versions 1 to 3, its `name` or `resource`). The node runs only after its parent, and each object synced for the parent is available as the resources object in placeholders, e.g. `{{resources.crm.contacts.id}}` when the parent is the `crm/contacts` resource.
  - **`persist`** _(boolean)_
    Runtime versions 1 to 3 only; refused on version 4. Whether to persist the resource in the database or not in case of a Daemon sync job run or send as a payload in case of RapidBridge sync job run. The Proxy API resources are by default NOT persisted and this parameter can be set to `true` to persist them. Unified API resources are always persisted.
- **`sync_job_id`** _(string)_
  The ID of the sync job that this sync job run belongs to.
- **`integrated_account_id`** _(string)_
  The ID of the integrated account that this sync job run belongs to. On runtime version 4 this is `00000000-0000-0000-0000-000000000000`; the accounts the run calls are in `args`. Filter version 4 runs by `sync_job_id`.
- **`status`** _(string)_
  The status of the sync job run. `created` - waiting to run, `running` - running, `rate_limited` - temporarily paused after an upstream rate-limit response and will resume automatically after backoff, `completed` - completed successfully, `failed` - failed. The run doesn't store why: the reason is in the `error` of the `sync_job_run:failed` webhook event. On runtime versions 3 and 4, a run whose integrated accounts all need reauthorization fails before its first request with `truto_error_code: integrated_account_needs_reauth`. A scheduled run whose accounts are all stale is created already `failed` and sends only `sync_job_run:failed`. See [Sync job runs](https://truto.one/docs/guides/sync-jobs/overview#sync-job-runs). `stopped` - stopped by the user.
  Allowed: `created`, `running`, `rate_limited`, `completed`, `failed`, `stopped`
- **`daemon_id`** _(string)_
  The daemon_id attribute of a daemon that is running this sync job.
- **`daemon_group_key`** _(string)_
  The name of the daemon group that this sync job run should be assigned to. Used while creating a sync job run. Truto finds a daemon in the group that is available to run the sync job. On runtime versions 1 to 3, specify exactly one of `daemon_group_key` (Daemon), `webhook_id` (RapidBridge) or `super_query` while creating a Sync Job Run.
- **`webhook_id`** _(string)_
  The ID of the webhook where the sync job records need to be sent. On runtime versions 1 to 3, specify exactly one of `daemon_group_key` (Daemon), `webhook_id` (RapidBridge) or `super_query` while creating a Sync Job Run.
- **`error_handling`** _(string)_
  How a run on runtime versions 1 to 3 handles a failed request. `fail_fast` (version 1 only) stops the run at the first error. `ignore` carries on and sends a `sync_job_run:record_error` event for each error. `batch` (versions 2 and 3, runs with a `webhook_id`) carries on and sends `sync_job_run:record_error` events with the errors batched in `errors`. Versions 2 and 3 treat `fail_fast` as `ignore`. Version 4 ignores this field.
  Allowed: `fail_fast`, `ignore`, `batch`
- **`ignore_previous_run`** _(boolean)_
  Specifies if the sync job run was started ignoring the previous run date. Runtime versions 1 to 3 only.
- **`version`** _(integer)_
  The runtime version of the sync job run. Defaults to the sync job's `default_runtime_version`.
  Allowed: `1`, `2`, `3`, `4`
- **`force`** _(boolean)_
  Whether the run was started even though another run of the same sync job, integrated account and args was still active.
- **`super_query`** _(string)_
  The SuperQuery region the run's records are stored in, when the run uses SuperQuery as its destination.
  Allowed: `apac`, `wnam`
- **`datastore_id`** _(string)_
  The datastore a run created before run-level datastores were removed was started with. A new run always has `null`. Use a datastore destination on a runtime version 4 sync job instead.
- **`events_to_send`** _(array<string>)_
  When set, only these `sync_job_run:*` and `record:*` events are sent for the run. When not set, every event is sent.
- **`mutex_key`** _(string)_
  The sync job's `mutex_key` with the run's args filled in.
- **`state_key`** _(string)_
  The sync job's `state_key` with the run's args filled in. Names the namespace the run's state nodes use.
- **`meta`** _(object)_
  Any JSON object stored with the run, as sent when the run was created or updated.
- **`individual_request_batch_size`** _(number)_
  On runtime versions 2 to 4, how many queued requests the run takes at a time. Defaults to 1.
- **`delay_between_requests_ms`** _(number)_
  An optional delay, in milliseconds, between requests made by the run on runtime version 4.
- **`retry_attempt`** _(number)_
  How many times the run has been retried.
- **`resource_stats`** _(object)_
  The statistics of the resources synced in the sync job run. This will have information around how many records were fetched and persisted by the sync job run -- grouped by the resource and method. The data is updated every 10 seconds while the sync job run is running.
- **`created_at`** _(string)_
  The date and time when the sync job run was created.
- **`updated_at`** _(string)_
  The date and time when the sync job run was last updated.
- **`started_at`** _(string)_
  The date and time when the sync job run was started.
- **`finished_at`** _(string)_
  The date and time when the sync job run was finished.
- **`sync_job`** _(object)_
  - **`id`** _(string)_
    The ID of the sync job.
  - **`integration_name`** _(string)_
    The name of the integration that this sync job interacts with.
  - **`label`** _(string)_
    A human-readable name for the sync job.
  - **`args_validation`** _(string)_
    An optional JSONata expression evaluated against the run's `args` when a sync job run is created. Return an object with a `message` field to reject the args with a 400.
  - **`default_runtime_version`** _(integer)_
    The runtime version that sync job runs use by default.
    Allowed: `1`, `2`, `3`, `4`
  - **`mutex_key`** _(string)_
    An optional key, with `{{args.*}}` placeholders, that stops a new run from starting while a run with the same resolved key is still active. Applies on runtime version 4.
  - **`state_key`** _(string)_
    An optional key, with `{{args.*}}` placeholders, that names the namespace the sync job's `get_state`, `update_state` and `delete_state` nodes read and write. Required for a sync job that uses those nodes.
  - **`delay_between_requests_ms`** _(integer)_
    An optional delay, in milliseconds, between requests made by the sync job's runs on runtime version 4.
  - **`args_schema`** _(object)_
    The arguments a run takes, keyed by argument name. Each value describes one argument: `type`, `description`, `format` and `required`. Truto checks only arguments whose `format` is `integrated_account_id`, `webhook_id` or `datastore_id` (the ID must exist in the sync job's environment) or `super_query` (`apac` or `wnam`). For those, `required: true` makes a run without the argument a `400`. Other arguments aren't checked; use `args_validation` for them. On runtime version 4 the accounts a run calls are read from the arguments with `format: integrated_account_id`.
  - **`resources`** _(array<object>)_
    The resources to sync as part of the sync job.
    - **`name`** _(string)_
      The name of the node. Required on runtime version 4, where `depends_on` and destinations refer to nodes by name. Keep names unique within a sync job.
    - **`type`** _(string)_
      The node type. Required on runtime version 4. Each type reads its own fields; `resource`, `method`, `id`, `query` and `loop_on` apply to `request` nodes.
      Allowed: `request`, `transform`, `spool`, `add_context`, `destination`, `event`, `get_state`, `update_state`, `delete_state`
    - **`run_if`** _(string)_
      A JSONata expression. On runtime version 4 the node runs only when it evaluates to a truthy value.
    - **`resource`** _(string)_
      The name of the resource to sync. Required for `request` nodes and on runtime versions 1 to 3. For Unified APIs, it should be in the format `unified_api_name/resource_name`. For Proxy API, it can just be `resource_name`.
    - **`method`** _(string)_
      The method to call on the resource.
    - **`id`** _(string)_
      The ID of the resource to sync. This is optional and can be used to sync a single resource. It also supports placeholders.
    - **`query`** _(object | string)_
      The query parameters to pass to the resource method. An object whose values may use placeholders, or a JSONata string that returns the object.
    - **`body`** _(object | string)_
      `request` nodes: the request body. An object whose values may use placeholders, or a JSONata string that returns the object.
    - **`integrated_account_id`** _(string)_
      `request` nodes, runtime version 4: the integrated account to call. Required there, usually `{{args.integrated_account_id}}`. Versions 1 to 3 refuse it and call the run's own `integrated_account_id`. On version 4, `add_context` and `get_state` nodes also take it.
    - **`recurse`** _(object)_
      `request` nodes: call the same resource again for each returned record for which `if` is truthy, with `config.id` and `config.query` for the next call. Use it for nested data such as child pages.
      - **`if`** _(string | object)_
        A placeholder or JSONata condition, or `{ expression }` with a JSONata expression.
      - **`config`** _(object)_
    - **`paginate_if`** _(string)_
      `request` nodes, runtime version 4: a JSONata expression. Truto fetches the next page only while it is truthy.
    - **`delay_after_ms`** _(number)_
      `request` nodes, runtime version 4: a pause in milliseconds after each call.
    - **`delete_tracking`** _(string)_
      `request` nodes, for runs that store Unified API records in Super Query: what happens to the stored records of this job and account that the run did not write. `hard` deletes them; `soft` marks them `truto_deleted`. Not used by runs that send to a webhook.
      Allowed: `hard`, `soft`
    - **`delete_tracking_query`** _(object)_
      With `delete_tracking`, a filter (placeholders allowed) that narrows which stored records are checked.
    - **`config`** _(object)_
      The settings of a non-`request` node. Its shape depends on `type`: for example `expression` on `transform` and `add_context` nodes, `key` on the state nodes, `name` on `event` nodes and `id` on `destination` nodes.
    - **`destination_type`** _(string)_
      `destination` nodes, runtime version 4: where the records go. `config.id` names the webhook or datastore.
      Allowed: `webhook`, `datastore`
    - **`resources_to_persist`** _(array<string>)_
      `destination` nodes, runtime version 4: the names of the nodes whose records this destination sends.
    - **`use_args`** _(boolean)_
      `transform` nodes, runtime version 4: set `true` when the parent is not a `request` node, such as a transform that builds records from `args`.
    - **`debug`** _(boolean)_
      Runtime version 4, any node: `true` logs a `transform` node's input and output.
    - **`loop_on`** _(string)_
      When a particular placeholder argument is an array and you want to repeat the request for each element in that array, you can set this parameter. For example, if you accept an argument called `user_ids` which is an array of strings, and you want to fetch each user's details, you can set this parameter to `args.user_ids` and the request will be repeated for each element in the `user_ids` array.
    - **`depends_on`** _(string)_
      The `name` of the parent node (on runtime versions 1 to 3, its `name` or `resource`). The node runs only after its parent, and each object synced for the parent is available as the resources object in placeholders, e.g. `{{resources.crm.contacts.id}}` when the parent is the `crm/contacts` resource.
    - **`persist`** _(boolean)_
      Runtime versions 1 to 3 only; refused on version 4. Whether to persist the resource in the database or not in case of a Daemon sync job run or send as a payload in case of RapidBridge sync job run. The Proxy API resources are by default NOT persisted and this parameter can be set to `true` to persist them. Unified API resources are always persisted.
  - **`environment_id`** _(string)_
    The ID of the environment that this sync job belongs to.
  - **`created_at`** _(string)_
    The date and time when the sync job was created.
  - **`updated_at`** _(string)_
    The date and time when the sync job was last updated.

## Errors

| Status | Description |
| :----- | :---------- |
| `400` | The body is not a JSON object, a field is not valid, or the body sends `status` (other than `stopped`), `started_at`, `finished_at` or `retry_attempt` (`truto_error_code` `sync_job_run_read_only_field`, with the fields sent in `fields`). Nothing is changed. |
| `404` | Sync job run not found |

## Code examples

### curl

```bash
curl -X PATCH 'https://api.truto.one/sync-job-run/{id}' \
  -H 'Authorization: Bearer <your_api_token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "error_handling": "fail_fast",
  "meta": {},
  "resource_stats": {},
  "delay_between_requests_ms": 0
}'
```

### JavaScript

```javascript
const body = {
  "error_handling": "fail_fast",
  "meta": {},
  "resource_stats": {},
  "delay_between_requests_ms": 0
};

const response = await fetch('https://api.truto.one/sync-job-run/{id}', {
  method: 'PATCH',
  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/sync-job-run/{id}"
headers = {
    "Authorization": "Bearer <your_api_token>",
    "Content-Type": "application/json",
}
params = {
}
payload = {
    "error_handling": "fail_fast",
    "meta": {},
    "resource_stats": {},
    "delay_between_requests_ms": 0
}

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