<commit_before>[[create-stored-script-api]]
=== Create or update stored script API
++++
<titleabbrev>Create or update stored script</titleabbrev>
++++

Creates or updates a <<script-stored-scripts,stored script>> or
<<search-template,search template>>.

[source,console]
----
PUT _scripts/my-stored-script
{
  "script": {
    "lang": "painless",
    "source": """
      TimestampHour date =  doc['@timestamp'].value; 
      return date.getHour()
    """
  }
}
----

[[create-stored-script-api-request]]
==== {api-request-title}

`PUT _search/<script-id>`

`POST _scripts/<script-id>`

`PUT _search/<script-id>/<context>`

`POST _scripts/<script-id>/<context>`

[[create-stored-script-api-prereqs]]
==== {api-prereq-title}

* If the {es} {security-features} are enabled, you must have the `manage`
<<privileges-list-cluster,cluster privilege>> to use this API.

[[create-stored-script-api-path-params]]
==== {api-path-parms-title}

`<script-id>`::
(Required, string)
Name or identifier for the stored script or search template. Must be unique
within the cluster.

`<context>`::
(Optional, string)
Context in which the script or search template should run. To prevent errors,
the API immediately compiles the script or template in this context.

[[create-stored-script-api-query-params]]
==== {api-query-parms-title}
<commit_msg>[DOCS] Fix created stored script API request paths<commit_after>[[create-stored-script-api]]
=== Create or update stored script API
++++
<titleabbrev>Create or update stored script</titleabbrev>
++++

Creates or updates a <<script-stored-scripts,stored script>> or
<<search-template,search template>>.

[source,console]
----
PUT _scripts/my-stored-script
{
  "script": {
    "lang": "painless",
    "source": """
      TimestampHour date =  doc['@timestamp'].value; 
      return date.getHour()
    """
  }
}
----

[[create-stored-script-api-request]]
==== {api-request-title}

`PUT _scripts/<script-id>`

`POST _scripts/<script-id>`

`PUT _scripts/<script-id>/<context>`

`POST _scripts/<script-id>/<context>`

[[create-stored-script-api-prereqs]]
==== {api-prereq-title}

* If the {es} {security-features} are enabled, you must have the `manage`
<<privileges-list-cluster,cluster privilege>> to use this API.

[[create-stored-script-api-path-params]]
==== {api-path-parms-title}

`<script-id>`::
(Required, string)
Name or identifier for the stored script or search template. Must be unique
within the cluster.

`<context>`::
(Optional, string)
Context in which the script or search template should run. To prevent errors,
the API immediately compiles the script or template in this context.

[[create-stored-script-api-query-params]]
==== {api-query-parms-title}

{'fraction_non_alphanumeric': 0.16858522094318604, 'fraction_numerical': 0.0, 'mean_word_length': 4.520491803278689, 'pattern_counts': {'":': 6, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1087224', 'n_tokens_mistral': 903, 'n_tokens_neox': 873, 'n_words': 231}