---
type: api
title: Create or update a script or search template
description: **All methods and paths for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-put-script
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-put-script
products:
  - Elasticsearch
---

# Create or update a script or search template


## Paths

- `POST` (also `PUT`) `/_scripts/{id}/{context}`
- `POST` (also `PUT`) `/_scripts/{id}`

#### Path parameters

- `id` (string) — required
  The identifier for the stored script or search template.
It must be unique within the cluster.
- `context` (string) — optional, leave it out to use `/_scripts/{id}`
  The context in which the script or search template should run.
To prevent errors, the API immediately compiles the script or template in this context.

## Description

Creates or updates a stored script or search template.

[Read the reference documentation](https://www.elastic.co/guide/en/elasticsearch/reference/8.19/search-template.html)

## Required authorization

- Cluster privileges: `manage`

## Authentication

- Api key: `Authorization: <value>`
  Elasticsearch APIs support key-based authentication.
  You must create an API key and use the encoded value in the request header.
  For example:
  
  
  ```
  curl -X GET "${ES_URL}/_cat/indices?v=true" \
    -H "Authorization: ApiKey ${API_KEY}"
  ```
  
  To get API keys, use the `/_security/api_key` APIs.
- Basic: `Authorization: Basic <credentials>`
- Bearer: `Authorization: Bearer <token>`
  Elasticsearch APIs support the use of bearer tokens in the `Authorization` HTTP header to authenticate with the API.
  For examples, refer to [Token-based authentication services](https://www.elastic.co/guide/en/elasticsearch/reference/current/token-authentication-services.html)

## Query string parameters

- `context` (string) — optional
  The context in which the script or search template should run.
To prevent errors, the API immediately compiles the script or template in this context.
If you specify both this and the `<context>` path parameter, the API uses the request path parameter.
- `master_timeout` (string · default: 30s) — optional
  The period to wait for a connection to the master node.
If no response is received before the timeout expires, the request fails and returns an error.
It can also be set to `-1` to indicate that the request should never timeout.
- `timeout` (string · default: 30s) — optional
  The period to wait for a response.
If no response is received before the timeout expires, the request fails and returns an error.
It can also be set to `-1` to indicate that the request should never timeout.

## Request body

`application/json`

- `script` (object) — required
  The script or search template, its parameters, and its language.
  - `lang` (union) — required
    The language the script is written in.
For serach templates, use `mustache`.

Supported values include:
  - `painless`: Painless scripting language, purpose-built for Elasticsearch.
  - `expression`: Lucene’s expressions language, compiles a JavaScript expression to bytecode.
  - `mustache`: Mustache templated, used for templates.
  - `java`: Expert Java API
    Values: `painless`, `expression`, `mustache`, `java`
  - `options` (map string to string) — optional
  - `source` (string) — required
    The script source.
For search templates, an object containing the search template.

## Response

`application/json`

- `acknowledged` (boolean)
  For a successful response, this value is always true. On failure, an exception is returned instead.
## Code examples

#### Console

```console
POST _scripts/my-search-template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "message": "{{query_string}}"
        }
      },
      "from": "{{from}}",
      "size": "{{size}}"
    }
  }
}
```

#### JavaScript

```javascript
const response = await client.putScript({
  id: "my-search-template",
  script: {
    lang: "mustache",
    source: {
      query: {
        match: {
          message: "{{query_string}}",
        },
      },
      from: "{{from}}",
      size: "{{size}}",
    },
  },
});
```

#### Python

```python
resp = client.put_script(
    id="my-search-template",
    script={
        "lang": "mustache",
        "source": {
            "query": {
                "match": {
                    "message": "{{query_string}}"
                }
            },
            "from": "{{from}}",
            "size": "{{size}}"
        }
    },
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_scripts/my-search-template" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "script": {
      "lang": "mustache",
      "source": {
        "query": {
          "match": {
            "message": "{{query_string}}"
          }
        },
        "from": "{{from}}",
        "size": "{{size}}"
      }
    }
  }'
```

#### PHP

```php
$resp = $client->putScript([
    "id" => "my-search-template",
    "body" => [
        "script" => [
            "lang" => "mustache",
            "source" => [
                "query" => [
                    "match" => [
                        "message" => "{{query_string}}",
                    ],
                ],
                "from" => "{{from}}",
                "size" => "{{size}}",
            ],
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.put_script(
  id: "my-search-template",
  body: {
    "script": {
      "lang": "mustache",
      "source": {
        "query": {
          "match": {
            "message": "{{query_string}}"
          }
        },
        "from": "{{from}}",
        "size": "{{size}}"
      }
    }
  }
)
```

