---
type: api
title: Create a runtime field
description: 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`.
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/kibana/v0/operation/operation-createruntimefielddefault
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/kibana/v0/operation/operation-createruntimefielddefault
products:
  - Kibana
---

# Create a runtime field

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

## Paths

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

#### Path parameters

- `viewId` (string) — required
  An identifier for the data view.
  Example: `ff959d40-b880-11e8-a6d9-e546fe2bba5f`

## Description

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`.

## 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>`

## Header parameters

- `kbn-xsrf` (string) — required
  Cross-site request forgery protection

## 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": [
    "..."
  ]
}
```

