> For the complete documentation index, see [llms.txt](https://archivedocs.stackstate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://archivedocs.stackstate.com/5.1/develop/developer-guides/custom-functions/template-functions.md).

# Template functions

## Overview

Template functions are defined by a JSON template and input parameters required by the template to render elements of StackState topology - mainly components or relations. When executed template functions substitutes all handlebar parameter references with values of input parameters. Template functions must define all parameters that template body refers to.

Template functions are used in cooperation with Mapping functions to create StackState topology elements. Mapping function parse topological data of external system and prepares input parameters for Template function.

## Create template functions

### Create from an existing component or relation

An easy way to create template functions is to create them based on existing component or relation. This option is available as **+ Add as template** in the context menu or from the right panel details tab when detailed information about an element is displayed - [Component details](/5.1/use/concepts/components.md#component-details) or [Direct relation details](/5.1/use/concepts/relations.md#relation-details) depending on the element type that you selected. . After adding component or relation as template, its template function will appear in the Templates list in the Templates panel.

### Manually create a template function

Below are some examples templates to create components. Note that a template isn't limited to rendering only components and relations. It can render JSON for any StackState domain object that's supported by restapi, for example a Domain, Layer, Check or Stream. and also not only single object, but several multiple objects with one template.

* A simple template that creates a component. Its template function must define the parameters with the following names: `name`, `description`, `componentTypeId`, `layerId`, `domainId` and `environmentId`.

  ```
          [{
            "_type": "Component",
            "checks": [],
            "streams": [],
            "labels": [],
            "name": "{{ name }}",
            "description": "{{ description }}",
            "type" : {{ componentTypeId }},
            "layer": {{ layerId }},
            "domain": {{ domainId }},
            "environments": [{{ environmentId }}]
          }]
  ```
* A template to create a relation between the components `{{ sourceId }}` and `{{ targetId }}`. Its template Function must define parameters with the following names: `name`, `description`, `relationTypeId`, `sourceId` and `targetId`.

  ```
         [{
           "_type": "Relation",
           "checks": [],
           "streams": [],
           "labels": [],
           "name": "{{ name }}",
           "description": "{{ description }}",
           "type": {{ relationTypeId }},
           "source": {{ sourceId }},
           "target": {{ targetId }}
         }]
  ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://archivedocs.stackstate.com/5.1/develop/developer-guides/custom-functions/template-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
