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

# Render a search template


## Paths

- `POST` (also `GET`) `/_render/template/{id}`
- `POST` (also `GET`) `/_render/template`

#### Path parameters

- `id` (string) — optional, leave it out to use `/_render/template`
  The ID of the search template to render.
If no `source` is specified, this or the `id` request body parameter is required.

## Description

Render a search template as a search request body.

## Required authorization

- Index privileges: `read`

## 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/docs/deploy-manage/users-roles/cluster-or-deployment-auth/token-based-authentication-services)

## Request body

`application/json`

- `id` (string) — optional
  The ID of the search template to render.
If no `source` is specified, this or the `<template-id>` request path parameter is required.
If you specify both this parameter and the `<template-id>` parameter, the API uses only `<template-id>`.
- `file` (string) — optional
- `params` (map string to object) — optional
  Key-value pairs used to replace Mustache variables in the template.
The key is the variable name.
The value is the variable value.
- `source` (union) — optional
  An inline search template.
It supports the same parameters as the search API's request body.
These parameters also support Mustache variables.
If no `id` or `<templated-id>` is specified, this parameter is required.

## Response

`application/json`

- `template_output` (map string to object)
## Code examples

#### Console

```console
POST _render/template
{
  "id": "my-search-template",
  "params": {
    "query_string": "hello world",
    "from": 20,
    "size": 10
  }
}
```

#### JavaScript

```javascript
const response = await client.renderSearchTemplate({
  id: "my-search-template",
  params: {
    query_string: "hello world",
    from: 20,
    size: 10,
  },
});
```

#### Python

```python
resp = client.render_search_template(
    id="my-search-template",
    params={
        "query_string": "hello world",
        "from": 20,
        "size": 10
    },
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_render/template" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "my-search-template",
    "params": {
      "query_string": "hello world",
      "from": 20,
      "size": 10
    }
  }'
```

#### Java

```java
client.renderSearchTemplate(r -> r
    .id("my-search-template")
    .params(Map.of("size", JsonData.fromJson("10"),"from", JsonData.fromJson("20"),"query_string", JsonData.fromJson("\"hello world\"")))
);
```

#### C#

```c#
var response = await client
    .RenderSearchTemplateAsync(d1 => d1
        .Id("my-search-template")
        .Params(new Dictionary<string, object>()
        {
            { "query_string", "hello world" },
            { "from", 20 },
            { "size", 10 }
        })
    );
```

#### PHP

```php
$resp = $client->renderSearchTemplate([
    "body" => [
        "id" => "my-search-template",
        "params" => [
            "query_string" => "hello world",
            "from" => 20,
            "size" => 10,
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.render_search_template(
  body: {
    "id": "my-search-template",
    "params": {
      "query_string": "hello world",
      "from": 20,
      "size": 10
    }
  }
)
```

