---
type: api
title: Create a live query
description: Create and run a live query.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/v0/operation/operation-osquerycreatelivequery
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/v0/operation/operation-osquerycreatelivequery
products:
  - Kibana
---

# Create a live query

- Server: `https://{kibana_url}`

## Paths

- `POST` `/api/osquery/live_queries`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/kibana/v0/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/kibana/v0/authentication#basicauth)

## Description

Create and run a live query.

## Request Body

`application/json`

- `agent_all` (boolean) — optional
  When `true`, the query runs on all agents.
- `agent_ids` ([] string) — optional
  A list of agent IDs to run the query on.
- `agent_platforms` ([] string) — optional
  A list of agent platforms to run the query on.
- `agent_policy_ids` ([] string) — optional
  A list of agent policy IDs to run the query on.
- `alert_ids` ([] string) — optional
  A list of alert IDs associated with the live query.
- `case_ids` ([] string) — optional
  A list of case IDs associated with the live query.
- `ecs_mapping` (object) — optional
  Map osquery results columns or static values to Elastic Common Schema (ECS) fields
- `event_ids` ([] string) — optional
  A list of event IDs associated with the live query.
- `metadata` (object) — optional — nullable
  Custom metadata object associated with the live query.
- `pack_id` (string) — optional
  The ID of the pack.
- `queries` ([] object) — optional
  An array of queries to run.
  - `ecs_mapping` (object) — optional
    Map osquery results columns or static values to Elastic Common Schema (ECS) fields
  - `enabled` (boolean) — optional
    Whether this query is enabled. When false, the query is omitted from the Fleet policy. Default is true.
  - `id` (string) — optional
    The ID of the query.
  - `platform` (string) — optional
    Restricts the query to a specified platform. The default is all platforms. To specify multiple platforms, use commas. For example, `linux,darwin`.
  - `query` (string) — optional
    The SQL query you want to run.
  - `removed` (boolean) — optional
    Indicates whether the query is removed.
  - `result_type` (enum) — optional
    Controls the result document type emitted by osquerybeat for this pack or query.
- `snapshot`: Full table snapshot on every scheduled run (default).
- `differential`: Rows added or removed since the previous run.
- `differential_added_only`: Only rows added since the previous run (no removals).
    Values: `snapshot`, `differential`, `differential_added_only`
  - `snapshot` (boolean) — optional
    Indicates whether the query is a snapshot.
  - `version` (string) — optional
    Uses the Osquery versions greater than or equal to the specified version string.
- `query` (string) — optional
  The SQL query you want to run.
- `saved_query_id` (string) — optional
  The ID of a saved query.

## Response

Indicates a successful call.

`application/json`

- `data` (object)
  - `@timestamp` (string)
    The timestamp when the action was created.
  - `action_id` (string)
    The ID of the action.
  - `agent_all` (boolean)
    Whether the query targets all agents.
  - `agent_ids` ([] string)
    The agent IDs targeted by the action.
  - `agent_platforms` ([] string)
    The agent platforms targeted.
  - `agent_policy_ids` ([] string)
    The agent policy IDs targeted.
  - `agents` ([] string)
    The resolved list of agent IDs.
  - `expiration` (string)
    The expiration date of the action.
  - `input_type` (string)
    The input type.
  - `metadata` (object)
    Custom metadata associated with the action.
  - `pack_id` (string)
    The pack ID if the query was run from a pack.
  - `queries` ([] object)
    The queries in this action.
    - `action_id` (string)
    - `agents` ([] string)
    - `ecs_mapping` (object)
      Map osquery results columns or static values to Elastic Common Schema (ECS) fields
    - `id` (string)
    - `platform` (string)
    - `query` (string)
    - `saved_query_id` (string)
    - `timeout` (integer)
    - `version` (string)
  - `type` (string)
    The action type.
  - `user_id` (string)
    The user who created the action.
## Code Examples

#### Console

```console
POST /api/osquery/live_queries
{
  "agent_all": true,
  "ecs_mapping": {
    "host.uptime": {
      "field": "total_seconds"
    }
  },
  "query": "select * from uptime;"
}
```

#### curl

```curl
curl -X POST "https://{kibana_url}/api/osquery/live_queries" \
  -H "Content-Type: application/json" \
  -d '{
  "agent_all": true,
  "ecs_mapping": {
    "host.uptime": {
      "field": "total_seconds"
    }
  },
  "query": "select * from uptime;"
}'
```

## Response Examples

#### Live query created

```json
{
  "data": {
    "@timestamp": "2022-07-26T09:59:32.220Z",
    "action_id": "3c42c847-eb30-4452-80e0-728584042334",
    "agent_all": true,
    "agents": [
      "16d7caf5-efd2-4212-9b62-73dafc91fa13"
    ],
    "expiration": "2022-07-26T10:04:32.220Z",
    "input_type": "osquery",
    "queries": [
      {
        "action_id": "609c4c66-ba3d-43fa-afdd-53e244577aa0",
        "agents": [
          "16d7caf5-efd2-4212-9b62-73dafc91fa13"
        ],
        "id": "6724a474-cbba-41ef-a1aa-66aebf0879e2",
        "query": "select * from uptime;",
        "timeout": 120
      }
    ],
    "type": "INPUT_ACTION",
    "user_id": "elastic"
  }
}
```

