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

# Create or update a runtime field

Authentication: required

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

## Paths

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

#### 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.
- `viewId` (string) — required
  The ID of the data view fields you want to update.

## Description

Create or update a runtime field for a data view. If the runtime field already exists, it is replaced with the new definition.

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

## Responses

#### `200`

Indicates a successful call.

`application/json`

- `data_view` (object)
- `fields` (object[])
#### `400`

Bad request

`application/json`

- `error` (string)
- `message` (string)
- `statusCode` (number)
## Code examples

#### Console

```console
PUT 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 PUT "${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.

`application/json`

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

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

#### A bad request response.

`application/json`

The request was rejected because the payload or query parameters are missing required fields or contain invalid values.

```json
{
  "error": "Bad Request",
  "message": "[request body.data_view.title]: expected value of type [string] but got [undefined]",
  "statusCode": 400
}
```

