# Create workflow template

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

Create a custom workflow template in the current workspace. Custom templates are only visible in the current workspace; built-in templates require administrator privileges.

```text
POST https://moi.matrixorigin.cn/newmoi/workflow-templates
```

## Preparation before calling

Prepare the workflow DSL with personal access token for target workspace access, target workspace ID, and template. Creating built-in templates also requires administrator rights.


## Request body

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>name</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>Template name. Custom templates cannot have the same name in the current workspace and current language.</dd></div>
  <div class="mo-api-field"><dt><code>description</code><span class="mo-api-field__type">string</span></dt><dd>Template description.</dd></div>
  <div class="mo-api-field"><dt><code>dsl_yaml</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>The YAML text of the workflow DSL.</dd></div>
  <div class="mo-api-field"><dt><code>runtime_fields</code><span class="mo-api-field__type">string</span></dt><dd>The JSON text of the runtime field definition; must be a valid JSON string when provided.</dd></div>
  <div class="mo-api-field"><dt><code>is_builtin</code><span class="mo-api-field__type">boolean</span></dt><dd>Whether to create a built-in template, the default is false. Setting to true requires administrator privileges and cannot use the system-managed built-in template ID.</dd></div>
  <div class="mo-api-field"><dt><code>template_key</code><span class="mo-api-field__type">string</span></dt><dd>The identifier of the built-in template. The server ignores this field when creating a custom template and returns an empty string; it cannot be repeated in the same language.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Request example</p>
```

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/workflow-templates" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "<TEMPLATE_NAME>",
    "description": "<DESCRIPTION>",
    "dsl_yaml": "<WORKFLOW_DSL_YAML>",
    "runtime_fields": "{\"fields\":[]}",
    "is_builtin": false
  }'
```

:::
::::

## Successful response

Returns `200` on success. `data` is a newly created template. Save `data.id`, this value will be needed for subsequent query, update or deletion of templates.


The response fields are as follows.

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>OK when successful.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>OK when successful.</dd></div>
  <div class="mo-api-field"><dt><code>data.id</code><span class="mo-api-field__type">integer</span></dt><dd>Template ID.</dd></div>
  <div class="mo-api-field"><dt><code>data.workspace_id</code><span class="mo-api-field__type">string</span></dt><dd>The ID of the workspace to which it belongs; the built-in template is an empty string.</dd></div>
  <div class="mo-api-field"><dt><code>data.created_by</code><span class="mo-api-field__type">string</span></dt><dd>Creator ID.</dd></div>
  <div class="mo-api-field"><dt><code>data.updated_by</code><span class="mo-api-field__type">string</span></dt><dd>Last updater ID.</dd></div>
  <div class="mo-api-field"><dt><code>data.template_key</code><span class="mo-api-field__type">string</span></dt><dd>Built-in template identifier; custom template is an empty string.</dd></div>
  <div class="mo-api-field"><dt><code>data.name</code><span class="mo-api-field__type">string</span></dt><dd>Template name.</dd></div>
  <div class="mo-api-field"><dt><code>data.description</code><span class="mo-api-field__type">string</span></dt><dd>Template description.</dd></div>
  <div class="mo-api-field"><dt><code>data.language</code><span class="mo-api-field__type">string</span></dt><dd>Template language, such as zh-CN.</dd></div>
  <div class="mo-api-field"><dt><code>data.dsl_yaml</code><span class="mo-api-field__type">string</span></dt><dd>Saved workflow DSL YAML.</dd></div>
  <div class="mo-api-field"><dt><code>data.runtime_fields</code><span class="mo-api-field__type">string</span></dt><dd>JSON text of the saved runtime field; empty string if not configured.</dd></div>
  <div class="mo-api-field"><dt><code>data.is_builtin</code><span class="mo-api-field__type">boolean</span></dt><dd>Whether it is a built-in template.</dd></div>
  <div class="mo-api-field"><dt><code>data.created_at</code><span class="mo-api-field__type">string</span></dt><dd>Creation time; may be omitted if not provided by the server.</dd></div>
  <div class="mo-api-field"><dt><code>data.updated_at</code><span class="mo-api-field__type">string</span></dt><dd>Last updated time; may be omitted if not provided by the server.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Successful response example</p>
```

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "id": 12,
    "workspace_id": "ws-001",
    "created_by": "user-001",
    "updated_by": "user-001",
    "template_key": "",
    "name": "daily-import-template",
    "description": "每日导入工作流模板",
    "language": "zh-CN",
    "dsl_yaml": "workflow: {}",
    "runtime_fields": "{\"fields\":[]}",
    "is_builtin": false,
    "created_at": "2026-08-18T10:00:00Z",
    "updated_at": "2026-08-18T10:00:00Z"
  }
}
```

:::
::::

## Error response

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>Error code.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>Readable error message.</dd></div>
  <div class="mo-api-field"><dt><code>data</code><span class="mo-api-field__type">null</span></dt><dd>`null` in an error response.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Error response example</p>
```

```json
{
  "code": "ErrParamInvalid",
  "msg": "请求参数无效",
  "data": null
}
```

:::
::::

## Follow-up operations

Use `data.id` [query workflow template details](get-workflow-template.md).
