---
type: api
title: Create a live query
description: **Spaces method and path for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/kibana/v8/operation/operation-osquerycreatelivequery
resource: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/kibana/v8/operation/operation-osquerycreatelivequery
products:
  - Kibana
---

# Create a live query

Authentication: required

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

## Paths

- `POST` `/api/osquery/live_queries`
- `POST` `/s/{space_id}/api/osquery/live_queries`

#### Path parameters

- `space_id` (string) — optional
  An identifier for the space. If `space_id` is not provided in the URL, the default space is used. Refer to [Spaces](https://www.elastic.co/docs/deploy-manage/manage-spaces) for more information.

## 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` (map string to object) — optional
  Map osquery results columns or static values to Elastic Common Schema (ECS) fields
  Map values (object)
  - `field` (string) — optional
    The ECS field to map to.
  - `value` (union string | string[]) — optional
    The value to map to the ECS field.
- `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 you want to run, retrieve, update, or delete.
- `queries` (object[]) — optional
  An array of queries to run.
  - `ecs_mapping` (map string to object) — optional
    Map osquery results columns or static values to Elastic Common Schema (ECS) fields
    Map values (object)
    - `field` (string) — optional
      The ECS field to map to.
    - `value` (union string | string[]) — optional
      The value to map to the ECS field.
  - `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.
  - `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

OK

`application/json`

Type: `object`

## Code examples

#### Console

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

#### curl

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

## Response examples

#### 200

`application/json`

Generated from the API spec. Values are representative. Unions use the first variant; deeply nested or large bodies may omit fields.

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

