---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://docs.sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://docs.sendcorex.com/llms.txt"
---

Source: https://docs.sendcorex.com/ja/api-reference/templates/routes/run-commands

テンプレート：ルート

# Run Commands

テンプレートへの唯一の入り口です。1リクエストで1〜10個のコマンドを送り、コマンドごとに1つの結果を受け取ります。

POST `https://g.sendcorex.com/v1/template`

Domains、Senders、Contactsと同じエンベロープです。この下のページで各コマンドを説明しています。

エラーはコマンドごとに報告されます。1つのコマンドが失敗してもほかは止まらず、HTTPステータスは`200`のままです。

## ボディパラメーター

- `commands` object[] 必須
1〜10個のコマンドを順番に実行します。ボディ全体で最大1 MBです。
- `commands[].command` string 必須
`list_templates`、`get_template`、`render_template`、`create_template`、`update_template`、`delete_template`、`list_template_versions`、`restore_template_version`のいずれか。
- `commands[].args` object 任意
コマンドの引数。どの値も、前のコマンドの結果への`$ref`にできます。
- `commands[].id` string 任意
結果につける独自の識別子。デフォルトはインデックス（`"0"`、`"1"`…）。`$ref`を使う場合は必須。

## クイックスタート

```
import axios from 'axios';

const response = await axios.post(
  'https://g.sendcorex.com/v1/template',
  {
    commands: [
      {
        command: "list_templates",
        id: "all"
      },
      {
        command: "render_template",
        id: "preview",
        args: {
          template_id: "html_5f2c8a10-3d4e-4b6f-9a71-2c8e0d4b7a93",
          variables: {
            first_name: "Ana",
            plan: "Pro"
          }
        }
      }
    ]
  },
  { headers: { Authorization: process.env.SENDCOREX_API_KEY } },
);

for (const result of response.data.results) {
  if (result.type !== 'SUCCESS') console.error(result.id, result.code, result.message);
  else console.log(result.id, result.data);
}
```

## レスポンス

```
{
  "type": "SUCCESS",
  "results": [
    {
      "command": "list_templates",
      "id": "all",
      "type": "SUCCESS",
      "took_ms": 6,
      "data": {
        "templates": [
          {
            "template_id": "html_5f2c8a10-3d4e-4b6f-9a71-2c8e0d4b7a93",
            "type": "html",
            "name": "Welcome email",
            "reference": "K7Q2M9XR4T",
            "preview": null,
            "has_html": true,
            "html_size": 248,
            "editable": true,
            "created_at": "2026-10-21T04:02:11.310Z",
            "updated_at": "2026-10-21T04:15:40.902Z"
          },
          {
            "template_id": "unlayer_1284",
            "type": "unlayer",
            "name": "October newsletter",
            "reference": "B3VN8QK1ZT",
            "preview": "https://res.cloudinary.com/dmg04ffuy/image/upload/v1760000000/previews/oct_newsletter.png",
            "has_html": true,
            "html_size": 48211,
            "editable": false,
            "created_at": "2026-10-01T09:12:00.000Z",
            "updated_at": "2026-10-18T16:40:22.118Z"
          }
        ]
      }
    },
    {
      "command": "render_template",
      "id": "preview",
      "type": "SUCCESS",
      "took_ms": 3,
      "data": {
        "template_id": "html_5f2c8a10-3d4e-4b6f-9a71-2c8e0d4b7a93",
        "subject": "Welcome to Acme, Ana",
        "html": "<!doctype html><html><body style=\"font-family:Arial,sans-serif\"><h1>Welcome, Ana!</h1><p>Your Pro plan is ready.</p><p><a href=\"https://acme.com/start\">Get started</a></p></body></html>",
        "text": "Welcome, Ana! Your Pro plan is ready. Get started: https://acme.com/start",
        "missing_variables": []
      }
    }
  ]
}
```

### レスポンスの項目

- `type`
リクエスト自体が受け付けられた場合は常に`SUCCESS`。
- `results[].command`
この結果を生成したコマンド。
- `results[].id`
送った`id`、またはコマンドのインデックス。
- `results[].type`
`SUCCESS`または`ERROR`。
- `results[].took_ms`
サーバー内でコマンドにかかった時間。
- `results[].data`
コマンドの出力。`type`が`SUCCESS`の場合のみ。
- `results[].code / message`
エラー。`type`が`ERROR`の場合のみ。

## 認証

ダッシュボードのDevelopers → API Keysで作成したAPI keyを、コマンドに必要なスコープ付きで使います。

- `Authorization`
`Authorization: <api key>`または`Authorization: Bearer <api key>`を送ります。
- `template:read`
`list_templates`、`get_template`、`render_template`、`list_template_versions`。
- `template:create`
`create_template`。
- `template:update`
`update_template`と`restore_template_version`。
- `template:delete`
`delete_template`。
- `メールボックスのトークン`
Webmailのトークンはここでは使えず、`WRONG_CREDENTIAL_TYPE`で拒否されます。
- `許可するIP`
キーにIP許可リストがある場合、ほかのIPからのリクエストには`403 IP_NOT_ALLOWED`が返ります。

## $refでつなぐ

テンプレートを作成し、同じリクエストでレンダリングして結果を確認します。

create_template → render_template

```
{
  "commands": [
    {
      "command": "create_template",
      "id": "new",
      "args": {
        "name": "Welcome email",
        "subject": "Welcome to Acme, {{first_name}}",
        "html": "<!doctype html><html><body style=\"font-family:Arial,sans-serif\"><h1>Welcome, {{first_name}}!</h1><p>Your {{plan}} plan is ready.</p><p><a href=\"https://acme.com/start\">Get started</a></p></body></html>"
      }
    },
    {
      "command": "render_template",
      "args": {
        "template_id": {
          "$ref": "new",
          "path": "template_id"
        },
        "variables": {
          "first_name": "Ana",
          "plan": "Pro"
        }
      }
    }
  ]
}
```

## レート制限とキャッシュ

- `毎秒15リクエスト`
IP + API keyごと。超えるとHTTP 429と`Retry-After`ヘッダー。
- `同時15件`
IP + API keyごとに、同時に処理されるリクエストは最大15件。
- `コマンドごと、アカウントごと`
`create_template` 30回/分 · `update_template` 60回/分 · `restore_template_version` 20回/分 · `delete_template` 30回/分。超えるとそのコマンドは`RATE_LIMITED`を返します。
- `読み取りキャッシュ：1.5秒`
1.5秒以内の同じ読み取りは同じ結果を返します。このルートで書き込みがあるとリセットされます。

## エラー

HTTPレベルのエラーは`type`、`code`、`message`を返します。CMDと表示されたエラーは、HTTP 200でコマンドの結果内に返ります。

- 400`INVALID_JSON`
ボディが{"commands":[...]}ではないか、1 MBを超えています。
- 400`INVALID_ARGS`
リクエストにコマンドがないか、10個を超えています。
- 401`MISSING_AUTH_HEADER`
Authorizationヘッダーが送られていません。
- 401`INVALID_API_KEY`
API keyが存在しないか、取り消されたか、ダッシュボードで無効化されています。
- 401`API_KEY_EXPIRED`
API keyがダッシュボードで設定した有効期限を過ぎています。
- 401`WRONG_CREDENTIAL_TYPE`
メールボックスのtokenが送られました。Templates APIにはダッシュボードのAPI keyが必要です。
- 403`IP_NOT_ALLOWED`
キーの許可IPにないIPからのリクエストです。
- 403`ACCOUNT_BLOCKED`
アカウントがブロックされています。
- 403`MISSING_SCOPE`
キーに`template:*`のscopeがないため、どのコマンドも実行できません。
- 429`RATE_LIMITED`
同じIP + API keyで毎秒15リクエストまたは同時15件を超えました。Retry-Afterの秒数だけ待ってください。
- 503`AUTH_UNAVAILABLE`
認証が一時的に利用できません。もう一度お試しください。
- CMD`UNKNOWN_COMMAND`
そのコマンド名は存在しません。
- CMD`INTERNAL`
サーバーエラー。詳細はサーバーのログにのみ記録されます。
