---
type: api
title: Run a watch
description: **All methods and paths for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-watcher-execute-watch
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-watcher-execute-watch
products:
  - Elasticsearch
---

# Run a watch


## Paths

- `POST` (also `PUT`) `/_watcher/watch/{id}/_execute`
- `POST` (also `PUT`) `/_watcher/watch/_execute`

#### Path parameters

- `id` (string) — optional, leave it out to use `/_watcher/watch/_execute`
  The watch identifier.

## Description

This API can be used to force execution of the watch outside of its triggering logic or to simulate the watch execution for debugging purposes.

For testing and debugging purposes, you also have fine-grained control on how the watch runs.
You can run the watch without running all of its actions or alternatively by simulating them.
You can also force execution by ignoring the watch condition and control whether a watch record would be written to the watch history after it runs.

You can use the run watch API to run watches that are not yet registered by specifying the watch definition inline.
This serves as great tool for testing and debugging your watches prior to adding them to Watcher.

When Elasticsearch security features are enabled on your cluster, watches are run with the privileges of the user that stored the watches.
If your user is allowed to read index `a`, but not index `b`, then the exact same set of rules will apply during execution of a watch.

When using the run watch API, the authorization data of the user that called the API will be used as a base, instead of the information who stored the watch.

## Required authorization

- Cluster privileges: `manage_watcher`

## Authentication

- Api key: `Authorization: <value>`
  Elasticsearch APIs support key-based authentication.
  You must create an API key and use the encoded value in the request header.
  For example:
  
  
  ```
  curl -X GET "${ES_URL}/_cat/indices?v=true" \
    -H "Authorization: ApiKey ${API_KEY}"
  ```
  
  To get API keys, use the `/_security/api_key` APIs.
- Basic: `Authorization: Basic <credentials>`
- Bearer: `Authorization: Bearer <token>`
  Elasticsearch APIs support the use of bearer tokens in the `Authorization` HTTP header to authenticate with the API.
  For examples, refer to [Token-based authentication services](https://www.elastic.co/guide/en/elasticsearch/reference/current/token-authentication-services.html)

## Query string parameters

- `debug` (boolean · default: false) — optional
  Defines whether the watch runs in debug mode.

## Request body

`application/json`

- `action_modes` (map string to enum) — optional
  Determines how to handle the watch actions as part of the watch execution.
  Map values (enum)
    Values: `simulate`, `force_simulate`, `execute`, `force_execute`, `skip`
- `alternative_input` (map string to object) — optional
  When present, the watch uses this object as a payload instead of executing its own input.
- `ignore_condition` (boolean · default: false) — optional
  When set to `true`, the watch execution uses the always condition. This can also be specified as an HTTP parameter.
- `record_execution` (boolean · default: false) — optional
  When set to `true`, the watch record representing the watch execution result is persisted to the `.watcher-history` index for the current time.
In addition, the status of the watch is updated, possibly throttling subsequent runs.
This can also be specified as an HTTP parameter.
- `simulated_actions` (object) — optional
  - `actions` (string[]) — required
  - `all` (object) — required — recursive
    Same fields as `simulated_actions`
  - `use_all` (boolean) — required
- `trigger_data` (object) — optional
  This structure is parsed as the data of the trigger event that will be used during the watch execution.
  - `scheduled_time` (union) — required
  - `triggered_time` (union) — optional
- `watch` (object · default: null) — optional
  When present, this watch is used instead of the one specified in the request.
This watch is not persisted to the index and `record_execution` cannot be set.
  - `actions` (map string to object) — required
    Map values (object)
    - `action_type` (enum) — optional
      Values: `email`, `webhook`, `index`, `logging`, `slack`, `pagerduty`
    - `condition` (object) — optional
      - `always` (object) — optional
      - `array_compare` (map string to object · min properties: 1 · max properties: 1) — optional
        Map values (object)
        - `path` (string) — required
      - `compare` (map string to string to oneOf · min properties: 1 · max properties: 1) — optional
        Map values (map string to oneOf · min properties: 1 · max properties: 1)
      - `never` (object) — optional
      - `script` (object) — optional
        - `lang` (string · default: painless) — optional
        - `params` (map string to object) — optional
        - `source` (string) — optional
        - `id` (string) — optional
    - `foreach` (string) — optional
    - `max_iterations` (number) — optional
    - `name` (string) — optional
    - `throttle_period` (string) — optional
    - `throttle_period_in_millis` (number) — optional
    - `transform` (object) — optional
      - `chain` (object[]) — optional — recursive
        Same fields as `transform` in `actions`
      - `script` (object) — optional
        - `lang` (string · default: painless) — optional
        - `params` (map string to object) — optional
        - `source` (string) — optional
        - `id` (string) — optional
      - `search` (object) — optional
        - `request` (object) — required
          - `body` (object) — optional
            - `query` ({} Query) — required
              See [Query](https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/types/_types-query_dsl-querycontainer)
          - `indices` (string[]) — optional
          - `indices_options` (object) — optional
            - `allow_no_indices` (boolean) — optional
              If false, the request returns an error if any wildcard expression, index alias, or `_all` value targets only
missing or closed indices. This behavior applies even if the request targets other open indices. For example,
a request targeting `foo*,bar*` returns an error if an index starts with `foo` but no index starts with `bar`.
            - `expand_wildcards` (string) — optional
              Type of index that wildcard patterns can match. If the request can target data streams, this argument
determines whether wildcard expressions match hidden data streams. Supports comma-separated values,
such as `open,hidden`.

Supported values include:
  - `all`: Match any data stream or index, including hidden ones.
  - `open`: Match open, non-hidden indices. Also matches any non-hidden data stream.
  - `closed`: Match closed, non-hidden indices. Also matches any non-hidden data stream. Data streams cannot be closed.
  - `hidden`: Match hidden data streams and hidden indices. Must be combined with `open`, `closed`, or `both`.
  - `none`: Wildcard expressions are not accepted.
              Values: `all`, `open`, `closed`, `hidden`, `none`
            - `ignore_unavailable` (boolean · default: false) — optional
              If true, missing or closed indices are not included in the response.
            - `ignore_throttled` (boolean · default: true) — optional
              If true, concrete, expanded or aliased indices are ignored when frozen.
          - `search_type` (enum) — optional
            

Supported values include:
  - `query_then_fetch`: Documents are scored using local term and document frequencies for the shard. This is usually faster but less accurate.
  - `dfs_query_then_fetch`: Documents are scored using global term and document frequencies across all shards. This is usually slower but more accurate.
            Values: `query_then_fetch`, `dfs_query_then_fetch`
          - `template` (object) — optional
            - `explain` (boolean · default: false) — optional
            - `id` (string) — optional
              ID of the search template to use. If no source is specified,
this parameter is required.
            - `params` (map string to object) — optional
            - `profile` (boolean · default: false) — optional
            - `source` (string) — optional
              An inline search template. Supports the same parameters as the search API's
request body. Also supports Mustache variables. If no id is specified, this
parameter is required.
          - `rest_total_hits_as_int` (boolean) — optional
        - `timeout` (string) — required
    - `index` (object) — optional
      - `index` (string) — required
      - `doc_id` (string) — optional
      - `refresh` (enum) — optional
        Values: `true`, `false`, `wait_for`
      - `op_type` (enum · default: index) — optional
        

Supported values include:
  - `index`: Overwrite any documents that already exist.
  - `create`: Only index documents that do not already exist.
        Values: `index`, `create`
      - `timeout` (string · default: 60s) — optional
      - `execution_time_field` (string) — optional
    - `logging` (object) — optional
      - `level` (string) — optional
      - `text` (string) — required
      - `category` (string) — optional
    - `email` (object) — optional
      - `id` (string) — optional
      - `bcc` (union string | string[]) — optional
      - `body` (object) — optional
        - `html` (string) — optional
        - `text` (string) — optional
      - `cc` (union string | string[]) — optional
      - `from` (string) — optional
      - `priority` (enum) — optional
        Values: `lowest`, `low`, `normal`, `high`, `highest`
      - `reply_to` (union string | string[]) — optional
      - `sent_date` (union) — optional
      - `subject` (string) — required
      - `to` (union string | string[]) — required
      - `attachments` (map string to object) — optional
        Map values (object · min properties: 1 · max properties: 1)
        - `http` (object) — optional
          - `content_type` (string) — optional
          - `inline` (boolean) — optional
          - `request` (object) — optional
            - `auth` (object) — optional
              - `basic` (object) — required
                - `password` (string) — required
                - `username` (string) — required
            - `body` (string) — optional
            - `connection_timeout` (string) — optional
            - `headers` (map string to string) — optional
            - `host` (string) — optional
            - `method` (enum) — optional
              Values: `head`, `get`, `post`, `put`, `delete`
            - `params` (map string to string) — optional
            - `path` (string) — optional
            - `port` (number) — optional
            - `proxy` (object) — optional
              - `host` (string) — required
              - `port` (number) — required
            - `read_timeout` (string) — optional
            - `scheme` (enum) — optional
              Values: `http`, `https`
            - `url` (string) — optional
        - `reporting` (object) — optional
          - `url` (string) — required
          - `inline` (boolean) — optional
          - `retries` (number · default: 40) — optional
          - `interval` (string · default: 15s) — optional
          - `request` (object) — optional
            Same fields as `request` in `http`
        - `data` (object) — optional
          - `format` (enum) — optional
            Values: `json`, `yaml`
    - `pagerduty` (object) — optional
      - `account` (string) — optional
      - `attach_payload` (boolean) — required
      - `client` (string) — optional
      - `client_url` (string) — optional
      - `contexts` (object[]) — optional
        - `href` (string) — optional
        - `src` (string) — optional
        - `type` (enum) — required
          Values: `link`, `image`
      - `description` (string) — required
      - `event_type` (enum · default: trigger) — optional
        Values: `trigger`, `resolve`, `acknowledge`
      - `incident_key` (string) — required
      - `proxy` (object) — optional
        - `host` (string) — optional
        - `port` (number) — optional
    - `slack` (object) — optional
      - `account` (string) — optional
      - `message` (object) — required
        - `attachments` (object[]) — required
          - `author_icon` (string) — optional
          - `author_link` (string) — optional
          - `author_name` (string) — required
          - `color` (string) — optional
          - `fallback` (string) — optional
          - `fields` (object[]) — optional
            - `short` (boolean) — required
            - `title` (string) — required
            - `value` (string) — required
          - `footer` (string) — optional
          - `footer_icon` (string) — optional
          - `image_url` (string) — optional
          - `pretext` (string) — optional
          - `text` (string) — optional
          - `thumb_url` (string) — optional
          - `title` (string) — required
          - `title_link` (string) — optional
          - `ts` (number) — optional
        - `dynamic_attachments` (object) — optional
          - `attachment_template` (object) — required
            Same fields as `attachments` in `message`
          - `list_path` (string) — required
        - `from` (string) — required
        - `icon` (string) — optional
        - `text` (string) — required
        - `to` (string[]) — required
    - `webhook` (object) — optional
      - `auth` (object) — optional
        - `basic` (object) — required
          - `password` (string) — required
          - `username` (string) — required
      - `body` (string) — optional
      - `connection_timeout` (string) — optional
      - `headers` (map string to string) — optional
      - `host` (string) — optional
      - `method` (enum) — optional
        Values: `head`, `get`, `post`, `put`, `delete`
      - `params` (map string to string) — optional
      - `path` (string) — optional
      - `port` (number) — optional
      - `proxy` (object) — optional
        - `host` (string) — required
        - `port` (number) — required
      - `read_timeout` (string) — optional
      - `scheme` (enum) — optional
        Values: `http`, `https`
      - `url` (string) — optional
  - `condition` (object) — required
    Same fields as `condition` in `actions`
  - `input` (object) — required
    - `chain` (object) — optional
      - `inputs` (string to object[]) — required — recursive
        Same fields as `input` in `watch`
    - `http` (object) — optional
      - `extract` (string[]) — optional
      - `request` (object) — optional
        Same fields as `request` in `http`
      - `response_content_type` (enum) — optional
        Values: `json`, `yaml`, `text`
    - `search` (object) — optional
      - `extract` (string[]) — optional
      - `request` (object) — required
        Same fields as `request` in `search`
      - `timeout` (string) — optional
    - `simple` (map string to object) — optional
  - `metadata` (object) — optional
  - `status` (object) — optional
    - `actions` (object) — required
    - `last_checked` (union) — optional
    - `last_met_condition` (union) — optional
    - `state` (object) — required
      - `active` (boolean) — required
      - `timestamp` (union) — required
    - `version` (number) — required
    - `execution_state` (string) — optional
  - `throttle_period` (string) — optional
  - `throttle_period_in_millis` (number) — optional
  - `transform` (object) — optional
    Same fields as `transform` in `actions`
  - `trigger` (object) — required
    - `schedule` (object) — optional
      - `timezone` (string) — optional
      - `cron` (string) — optional
      - `daily` (object) — optional
        - `at` (union oneOf[]) — required
          - **string**
          - **{ hour, minute }**
            - `hour` (number[]) — required
            - `minute` (number[]) — required
      - `hourly` (object) — optional
        - `minute` (number[]) — required
      - `interval` (string) — optional
      - `monthly` (union oneOf) — optional
        `object` or `object[]`
        - `at` (string[]) — required
        - `on` (number[]) — required
      - `weekly` (union oneOf) — optional
        `object` or `object[]`
        - `at` (string[]) — required
        - `on` (enum[]) — required
          Values: `sunday`, `monday`, `tuesday`, `wednesday`, `thursday`, `friday`, `saturday`
      - `yearly` (union oneOf) — optional
        `object` or `object[]`
        - `at` (string[]) — required
        - `int` (enum[]) — required
          Values: `january`, `february`, `march`, `april`, `may`, `june`, `july`, `august`, `september`, `october`, `november`, `december`
        - `on` (number[]) — required

## Response

`application/json`

- `_id` (string)
  The watch record identifier as it would be stored in the `.watcher-history` index.
- `watch_record` (object)
  The watch record document as it would be stored in the `.watcher-history` index.
  - `condition` (object)
    Same fields as `condition` in `actions`
  - `input` (object)
    Same fields as `input` in `watch`
  - `messages` (string[])
  - `metadata` (object)
  - `node` (string)
  - `result` (object)
    - `actions` (object[])
      - `email` (object)
        - `account` (string)
        - `message` (object)
          - `id` (string)
          - `bcc` (union string | string[])
          - `body` (object)
            - `html` (string)
            - `text` (string)
          - `cc` (union string | string[])
          - `from` (string)
          - `priority` (enum)
            Values: `lowest`, `low`, `normal`, `high`, `highest`
          - `reply_to` (union string | string[])
          - `sent_date` (union)
          - `subject` (string)
          - `to` (union string | string[])
          - `attachments` (map string to object)
            Map values (object · min properties: 1 · max properties: 1)
            - `http` (object)
              Same fields as `http` in `attachments`
            - `reporting` (object)
              - `url` (string)
              - `inline` (boolean)
              - `retries` (number · default: 40)
              - `interval` (string · default: 15s)
              - `request` (object)
                Same fields as `request` in `http`
            - `data` (object)
              - `format` (enum)
                Values: `json`, `yaml`
        - `reason` (string)
      - `id` (string)
      - `index` (object)
        - `response` (object)
          - `created` (boolean)
          - `id` (string)
          - `index` (string)
          - `result` (enum)
            Values: `created`, `updated`, `deleted`, `not_found`, `noop`
          - `version` (number)
      - `logging` (object)
        - `logged_text` (string)
      - `pagerduty` (object)
        - `event` (object)
          - `account` (string)
          - `attach_payload` (boolean)
          - `client` (string)
          - `client_url` (string)
          - `contexts` (object[])
            - `href` (string)
            - `src` (string)
            - `type` (enum)
              Values: `link`, `image`
          - `description` (string)
          - `event_type` (enum · default: trigger)
            Values: `trigger`, `resolve`, `acknowledge`
          - `incident_key` (string)
          - `proxy` (object)
            - `host` (string)
            - `port` (number)
        - `reason` (string)
        - `request` (object)
          - `auth` (object)
            - `basic` (object)
              - `password` (string)
              - `username` (string)
          - `body` (string)
          - `connection_timeout` (string)
          - `headers` (map string to string)
          - `host` (string)
          - `method` (enum)
            Values: `head`, `get`, `post`, `put`, `delete`
          - `params` (map string to string)
          - `path` (string)
          - `port` (number)
          - `proxy` (object)
            - `host` (string)
            - `port` (number)
          - `read_timeout` (string)
          - `scheme` (enum)
            Values: `http`, `https`
          - `url` (string)
        - `response` (object)
          - `body` (string)
          - `headers` (object)
          - `status` (number)
      - `reason` (string)
      - `slack` (object)
        - `account` (string)
        - `message` (object)
          Same fields as `message` in `slack`
      - `status` (enum)
        Values: `success`, `failure`, `simulated`, `throttled`
      - `type` (enum)
        Values: `email`, `webhook`, `index`, `logging`, `slack`, `pagerduty`
      - `webhook` (object)
        - `request` (object)
          Same fields as `request` in `pagerduty`
        - `response` (object)
          - `body` (string)
          - `headers` (object)
          - `status` (number)
      - `error` (object)
        - `type` (string)
          The type of error
        - `reason` (union string) — nullable
          A human-readable explanation of the error, in English.
          One of: `string`
        - `stack_trace` (string)
          The server stack trace. Present only if the `error_trace=true` parameter was sent with the request.
        - `caused_by` (object) — recursive
          Same fields as `error` in `actions`
        - `root_cause` (object[]) — recursive
          Same fields as `error` in `actions`
        - `suppressed` (object[]) — recursive
          Same fields as `error` in `actions`
    - `condition` (object)
      - `met` (boolean)
      - `status` (enum)
        Values: `success`, `failure`, `simulated`, `throttled`
      - `type` (enum)
        Values: `always`, `never`, `script`, `compare`, `array_compare`
    - `execution_duration` (number)
    - `execution_time` (union)
    - `input` (object)
      - `payload` (map string to object)
      - `status` (enum)
        Values: `success`, `failure`, `simulated`, `throttled`
      - `type` (enum)
        Values: `http`, `search`, `simple`
  - `state` (enum)
    Values: `awaits_execution`, `checking`, `execution_not_needed`, `throttled`, `executed`, `failed`, `deleted_while_queued`, `not_executed_already_queued`
  - `trigger_event` (object)
    - `manual` (object)
      - `schedule` (object)
        - `scheduled_time` (union)
        - `triggered_time` (union)
    - `triggered_time` (union)
    - `type` (string)
  - `user` (string)
  - `watch_id` (string)
  - `status` (object)
    - `actions` (object)
    - `last_checked` (union)
    - `last_met_condition` (union)
    - `state` (object)
      - `active` (boolean)
      - `timestamp` (union)
    - `version` (number)
    - `execution_state` (string)
## Code examples

#### Console

```console
POST _watcher/watch/my_watch/_execute
{
  "trigger_data" : { 
    "triggered_time" : "now",
    "scheduled_time" : "now"
  },
  "alternative_input" : { 
    "foo" : "bar"
  },
  "ignore_condition" : true, 
  "action_modes" : {
    "my-action" : "force_simulate" 
  },
  "record_execution" : true 
}
```

#### JavaScript

```javascript
const response = await client.watcher.executeWatch({
  id: "my_watch",
  trigger_data: {
    triggered_time: "now",
    scheduled_time: "now",
  },
  alternative_input: {
    foo: "bar",
  },
  ignore_condition: true,
  action_modes: {
    "my-action": "force_simulate",
  },
  record_execution: true,
});
```

#### Python

```python
resp = client.watcher.execute_watch(
    id="my_watch",
    trigger_data={
        "triggered_time": "now",
        "scheduled_time": "now"
    },
    alternative_input={
        "foo": "bar"
    },
    ignore_condition=True,
    action_modes={
        "my-action": "force_simulate"
    },
    record_execution=True,
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_watcher/watch/my_watch/_execute" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "trigger_data": {
      "triggered_time": "now",
      "scheduled_time": "now"
    },
    "alternative_input": {
      "foo": "bar"
    },
    "ignore_condition": true,
    "action_modes": {
      "my-action": "force_simulate"
    },
    "record_execution": true
  }'
```

#### Java

```java
client.watcher().executeWatch(e -> e
    .actionModes("my-action", ActionExecutionMode.ForceSimulate)
    .alternativeInput("foo", JsonData.fromJson("\"bar\""))
    .id("my_watch")
    .ignoreCondition(true)
    .recordExecution(true)
    .triggerData(t -> t
        .scheduledTime(DateTime.of("now"))
        .triggeredTime(DateTime.of("now"))
    )
);
```

#### PHP

```php
$resp = $client->watcher()->executeWatch([
    "id" => "my_watch",
    "body" => [
        "trigger_data" => [
            "triggered_time" => "now",
            "scheduled_time" => "now",
        ],
        "alternative_input" => [
            "foo" => "bar",
        ],
        "ignore_condition" => true,
        "action_modes" => [
            "my-action" => "force_simulate",
        ],
        "record_execution" => true,
    ],
]);
```

#### Ruby

```ruby
response = client.watcher.execute_watch(
  id: "my_watch",
  body: {
    "trigger_data": {
      "triggered_time": "now",
      "scheduled_time": "now"
    },
    "alternative_input": {
      "foo": "bar"
    },
    "ignore_condition": true,
    "action_modes": {
      "my-action": "force_simulate"
    },
    "record_execution": true
  }
)
```

## Response examples

#### Watcher execute watch response example1

A successful response from `POST _watcher/watch/my_watch/_execute`.

```json
{
  "_id": "my_watch_0-2015-06-02T23:17:55.124Z", 
  "watch_record": { 
    "@timestamp": "2015-06-02T23:17:55.124Z",
    "watch_id": "my_watch",
    "node": "my_node",
    "messages": [],
    "trigger_event": {
      "type": "manual",
      "triggered_time": "2015-06-02T23:17:55.124Z",
      "manual": {
        "schedule": {
          "scheduled_time": "2015-06-02T23:17:55.124Z"
        }
      }
    },
    "state": "executed",
    "status": {
      "version": 1,
      "execution_state": "executed",
      "state": {
        "active": true,
        "timestamp": "2015-06-02T23:17:55.111Z"
      },
      "last_checked": "2015-06-02T23:17:55.124Z",
      "last_met_condition": "2015-06-02T23:17:55.124Z",
      "actions": {
        "test_index": {
          "ack": {
            "timestamp": "2015-06-02T23:17:55.124Z",
            "state": "ackable"
          },
          "last_execution": {
            "timestamp": "2015-06-02T23:17:55.124Z",
            "successful": true
          },
          "last_successful_execution": {
            "timestamp": "2015-06-02T23:17:55.124Z",
            "successful": true
          }
        }
      }
    },
    "input": {
      "simple": {
        "payload": {
          "send": "yes"
        }
      }
    },
    "condition": {
      "always": {}
    },
    "result": { 
      "execution_time": "2015-06-02T23:17:55.124Z",
      "execution_duration": 12608,
      "input": {
        "type": "simple",
        "payload": {
          "foo": "bar"
        },
        "status": "success"
      },
      "condition": {
        "type": "always",
        "met": true,
        "status": "success"
      },
      "actions": [
        {
          "id": "test_index",
          "index": {
            "response": {
              "index": "test",
              "version": 1,
              "created": true,
              "result": "created",
              "id": "AVSHKzPa9zx62AzUzFXY"
            }
          },
          "status": "success",
          "type": "index"
        }
      ]
    },
    "user": "test_admin" 
  }
}
```

