---
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/vi/api-reference/templates/routes/run-commands

Mẫu: Route

# Run Commands

Điểm truy cập duy nhất cho mẫu. Gửi 1 đến 10 lệnh trong một yêu cầu và nhận một kết quả cho mỗi lệnh.

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

Cùng phong bì như Domains, Senders và Contacts. Các trang bên dưới mô tả từng lệnh.

Lỗi được báo theo từng lệnh. Một lệnh lỗi không dừng các lệnh khác, và trạng thái HTTP vẫn là `200`.

## Tham số body

- `commands` object[] bắt buộc
1 đến 10 lệnh, chạy theo thứ tự. Toàn bộ body có thể tối đa 1 MB.
- `commands[].command` string bắt buộc
`list_templates`, `get_template`, `render_template`, `create_template`, `update_template`, `delete_template`, `list_template_versions` hoặc `restore_template_version`.
- `commands[].args` object không bắt buộc
Tham số của lệnh. Bất kỳ giá trị nào cũng có thể là `$ref` tới kết quả của lệnh trước.
- `commands[].id` string không bắt buộc
Mã định danh riêng cho kết quả. Mặc định là chỉ số (`"0"`, `"1"`…). Bắt buộc nếu muốn dùng `$ref`.

## Bắt đầu nhanh

```
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);
}
```

## Phản hồi

```
{
  "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": []
      }
    }
  ]
}
```

### Trường phản hồi

- `type`
Luôn là `SUCCESS` khi bản thân yêu cầu được chấp nhận.
- `results[].command`
Lệnh tạo ra kết quả này.
- `results[].id`
`id` bạn đã gửi, hoặc chỉ số của lệnh.
- `results[].type`
`SUCCESS` hoặc `ERROR`.
- `results[].took_ms`
Thời gian lệnh chạy trong máy chủ.
- `results[].data`
Đầu ra của lệnh. Chỉ có khi `type` là `SUCCESS`.
- `results[].code / message`
Lỗi. Chỉ có khi `type` là `ERROR`.

## Xác thực

Dùng API key bạn tạo trong dashboard, mục Developers → API Keys, với các phạm vi lệnh cần.

- `Authorization`
Gửi `Authorization: <api key>` hoặc `Authorization: Bearer <api key>`.
- `template:read`
`list_templates`, `get_template`, `render_template` và `list_template_versions`.
- `template:create`
`create_template`.
- `template:update`
`update_template` và `restore_template_version`.
- `template:delete`
`delete_template`.
- `Token hộp thư`
Token webmail không dùng được ở đây. Nó bị từ chối với `WRONG_CREDENTIAL_TYPE`.
- `IP được phép`
Nếu key có danh sách IP cho phép, yêu cầu từ IP khác nhận `403 IP_NOT_ALLOWED`.

## Nối lệnh với $ref

Tạo một mẫu và kết xuất nó trong cùng yêu cầu để kiểm tra kết quả.

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"
        }
      }
    }
  ]
}
```

## Giới hạn tốc độ và cache

- `15 yêu cầu / giây`
Theo IP + API key. Vượt quá sẽ nhận HTTP 429 với header `Retry-After`.
- `15 đồng thời`
Tối đa 15 yêu cầu được xử lý cùng lúc, theo IP + API key.
- `Theo lệnh, theo tài khoản`
`create_template` 30/phút · `update_template` 60/phút · `restore_template_version` 20/phút · `delete_template` 30/phút. Vượt quá, lệnh đó trả về `RATE_LIMITED`.
- `Cache đọc: 1,5 s`
Cùng một lần đọc trong 1,5 giây trả về cùng kết quả. Bất kỳ thao tác ghi nào trên route sẽ xóa nó.

## Lỗi

Lỗi ở mức HTTP trả về `type`, `code` và `message`. Lỗi đánh dấu CMD được trả về trong kết quả lệnh, với HTTP 200.

- 400`INVALID_JSON`
Body không phải {"commands":[...]} hoặc lớn hơn 1 MB.
- 400`INVALID_ARGS`
Yêu cầu không có lệnh nào hoặc có hơn 10 lệnh.
- 401`MISSING_AUTH_HEADER`
Không gửi header Authorization.
- 401`INVALID_API_KEY`
API key không tồn tại, đã bị thu hồi hoặc bị tắt trong dashboard.
- 401`API_KEY_EXPIRED`
API key đã quá ngày hết hạn đặt trong dashboard.
- 401`WRONG_CREDENTIAL_TYPE`
Đã gửi một token hộp thư. Templates API cần API key của dashboard.
- 403`IP_NOT_ALLOWED`
Yêu cầu đến từ IP không nằm trong danh sách IP được phép của key.
- 403`ACCOUNT_BLOCKED`
Tài khoản bị khóa.
- 403`MISSING_SCOPE`
Key không có scope `template:*` nào, nên không lệnh nào chạy được.
- 429`RATE_LIMITED`
Hơn 15 yêu cầu/giây hoặc 15 yêu cầu đồng thời cho cùng IP + API key. Chờ số giây trong Retry-After.
- 503`AUTH_UNAVAILABLE`
Xác thực tạm thời không khả dụng. Hãy thử lại.
- CMD`UNKNOWN_COMMAND`
Tên lệnh không tồn tại.
- CMD`INTERNAL`
Lỗi máy chủ. Chi tiết chỉ được ghi vào log máy chủ.
