---
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/4371/api/doc/kibana/v9/operation/operation-createupdateruntimefielddefault
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/v9/operation/operation-createupdateruntimefielddefault
products:
  - Kibana
---

# Create or update a runtime field

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

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

#### Path Parameters

- `viewId` (string)
  The ID of the data view fields you want to update.

## Description

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

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

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

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.

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

