---
type: api
title: Create a runtime field
description: **Spaces method and path for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/operation/operation-createruntimefielddefault
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/operation/operation-createruntimefielddefault
products:
  - Kibana
---

# Create a runtime field

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

## Paths

- `POST` `/api/data_views/data_view/{viewId}/runtime_field`
- `POST` `/s/{space_id}/api/data_views/data_view/{viewId}/runtime_field`

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

#### Path Parameters

- `viewId` (string)
  An identifier for the data view.

## Description

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

Create a runtime field for a data view. Runtime fields are computed at query time using a [Painless script](https://www.elastic.co/docs/explore-analyze/scripting/modules-scripting-painless) and do not require reindexing. If no `script` is provided, the runtime field returns the corresponding value from the document `_source`.

## Request Body

`application/json`

- `name` (string) — required
  The name for a runtime field.
- `runtimeField` (object) — required
  The runtime field definition object.

## Response

Indicates a successful call.

`application/json`

Type: `object`

## Code Examples

#### Console

```console
POST kbn://api/data_views/data_view/{viewId}/runtime_field
{"name":"hour_of_day","runtimeField":{"type":"long","script":{"source":"emit(doc['timestamp'].value.getHour())"}}}
```

#### curl

```curl
curl \
  -X POST "${KIBANA_URL}/api/data_views/data_view/${DATA_VIEW_ID}/runtime_field" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "kbn-xsrf: true" \
  -H "Content-Type: application/json" \
  -d '{"name":"hour_of_day","runtimeField":{"type":"long","script":{"source":"emit(doc['"'"'timestamp'"'"'].value.getHour())"}}}'
```

## Response Examples

#### The API returns created runtime field object array and updated data view object.

The response includes the newly created runtime field as an array and the full updated data view object.

```json
{
  "data_view": {
    "...": "null"
  },
  "fields": [
    "..."
  ]
}
```

