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

# Create a saved query

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

## Paths

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

## Description

Refer to [Spaces](https://www.elastic.co/docs/deploy-manage/manage-spaces) for more information.

Create and run a saved query.

## Authentication

- Api key: `Authorization: <value>`
  These APIs use key-based authentication. You must create an API key and use the encoded value in the request header. For example: `Authorization: ApiKey base64AccessApiKey`
- Basic: `Authorization: Basic <credentials>`

## Request body

`application/json`

- `description` (string) — optional
  The saved query description.
- `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 a saved query.
- `interval` (string) — optional
  An interval, in seconds, on which to run 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.

## Response

OK

`application/json`

Type: `object`

## Code examples

#### Console

```console
POST kbn:/api/osquery/saved_queries
{
  "description": "Saved query description",
  "ecs_mapping": {
    "host.uptime": {
      "field": "total_seconds"
    }
  },
  "id": "saved_query_id",
  "interval": "60",
  "platform": "linux,darwin",
  "query": "select * from uptime;",
  "timeout": 120,
  "version": "2.8.0"
}
```

#### curl

```curl
curl -X POST "${KIBANA_URL}/api/osquery/saved_queries" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Saved query description",
    "ecs_mapping": {
      "host.uptime": {
        "field": "total_seconds"
      }
    },
    "id": "saved_query_id",
    "interval": "60",
    "platform": "linux,darwin",
    "query": "select * from uptime;",
    "timeout": 120,
    "version": "2.8.0"
  }'
```

