{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/sendseven-com/json-schema/sendseven-com-send-template-request-schema.json",
  "title": "SendTemplateRequest",
  "description": "Request to send a template message.\n\n``variable_values`` accepts EITHER a list (positional templates) OR a dict\n(positional with numeric keys, or named with identifier keys). The accepted\nshape is determined by the template's ``parameter_format``:\n\n* ``positional`` template → ``[\"John\", \"ORD-98765\"]`` or\n  ``{\"1\": \"John\", \"2\": \"ORD-98765\"}``.\n* ``named`` template → ``{\"first_name\": \"John\", \"order_id\": \"ORD-98765\"}``.\n\nSending the wrong shape returns 400 with an actionable error. Order in the\nNAMED dict is irrelevant — Meta resolves by ``parameter_name``.\n\nReserved keys in ``variable_values`` (only valid when passed as a dict;\nthey coexist with the regular body variable keys ``\"1\"``, ``\"2\"``, ... or\nnamed identifiers):\n\n* ``url_suffix`` — replaces the ``{{1}}`` placeholder inside a dynamic URL\n  button (e.g. template URL ``https://example.com/orders/{{1}}``). Pass\n  ONLY the suffix portion that fills ``{{1}}``, not the full URL. This\n  key targets button index 0; use the indexed form below to address\n  additional dynamic URL buttons on the same template.\n* ``url_suffix_<button_index>`` — same as ``url_suffix`` but addresses a\n  specific button by its 0-based index in the template's ``BUTTONS``\n  component (e.g. ``url_suffix_1`` for the second button). When both\n  ``url_suffix`` and ``url_suffix_0`` are present, the indexed form wins.\n* ``otp_code`` — the code value for an authentication / OTP COPY_CODE\n  button. This is the string the user copies when tapping the button.\n* ``otp_code_<button_index>`` — indexed variant for templates with more\n  than one COPY_CODE button. Same precedence rules as ``url_suffix_*``.",
  "x-generated": "2026-11-24",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/sendseven-com-openapi.json#/components/schemas/SendTemplateRequest",
  "properties": {
    "contact_id": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Contact Id",
      "description": "Contact to send the message to. OPTIONAL — supply ANY ONE of `contact_id`, `contact_method_id`, `conversation_id`, or `whatsapp_id` to identify the recipient. If `contact_method_id` is also provided it WINS and this `contact_id` is ignored. When combined with `whatsapp_id`, this contact is used (and a matching WhatsApp contact method is auto-created on it if missing)."
    },
    "contact_method_id": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Contact Method Id",
      "description": "OPTIONAL. A `contact_methods.id` (UUID) of type `whatsapp_id` or `whatsapp_bsuid` owned by the tenant. When provided it determines BOTH the recipient WhatsApp id (the method's value) AND the contact (the method's owning contact). Highest-priority recipient selector — if both `contact_id` and `contact_method_id` are given, `contact_method_id` WINS and `contact_id` is ignored. A method of any other type returns 422."
    },
    "whatsapp_id": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Whatsapp Id",
      "description": "OPTIONAL. A WhatsApp recipient id sent directly — a phone number WITHOUT a leading `+` (a leading `+` is stripped; spaces/dashes/parens are tolerated for phone-shaped values), an alphanumeric WhatsApp id (Meta now permits alphanumeric ids — digits are NOT required), OR a Business-scoped user ID such as `US.13491208655302741918` (kept verbatim, periods included, and sent in Meta's `recipient` field instead of `to`). The value is normalized with the same canonical contact-method normalization used elsewhere so dedup is consistent. If `contact_id` is also supplied, that contact gains a `whatsapp_id` (or `whatsapp_bsuid`) method with this value (created if missing); otherwise an existing contact is matched by this identifier, or a new contact + method is created. Note that AUTHENTICATION templates cannot be sent to a Business-scoped user ID (Meta error 131062). When sent without a `conversation_id`, the new conversation's channel is the template's channel (optionally selected via `channel_id`)."
    },
    "channel_id": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Channel Id",
      "description": "OPTIONAL. A WhatsApp channel id (UUID) owned by the tenant. Always allowed alongside any recipient selector. Selects the SENDING channel (and disambiguates a template referenced by NAME). Templates belong to the WhatsApp Business Account, so any active channel of the same account can send the template. A channel of a different account returns 400."
    },
    "variable_values": {
      "anyOf": [
        {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        {
          "additionalProperties": {
            "type": "string"
          },
          "type": "object"
        },
        {
          "type": "null"
        }
      ],
      "title": "Variable Values",
      "description": "Override auto-mapped values. Pass a list (positional only) or a dict (positional with numeric keys, or named with identifier keys). Shape must match the template's parameter_format.\n\nReserved dict keys (coexist with body variable keys):\n- `url_suffix` — replaces `{{1}}` inside a dynamic URL button. Value is ONLY the suffix portion that fills `{{1}}`, not the full URL (e.g. for template URL `https://example.com/orders/{{1}}`, pass `\"url_suffix\": \"track/INV-2026-001\"`).\n- `otp_code` — the code copied by the user for authentication / OTP COPY_CODE buttons.\n\nA blank value (empty or whitespace) does not count as a value: the variable is then resolved from `variable_mapping`, the template's saved mapping, and finally `variable_fallbacks`."
    },
    "variable_mapping": {
      "anyOf": [
        {
          "additionalProperties": {
            "type": "string"
          },
          "type": "object"
        },
        {
          "type": "null"
        }
      ],
      "title": "Variable Mapping",
      "description": "OPTIONAL. Per-send field mapping for template variables, keyed by placeholder name (`\"1\"` / `\"first_name\"`; `\"{{1}}\"` is also accepted). Overrides the template's saved mapping for this send only. Values: `contact.first_name`, `contact.last_name`, `contact.name`, `contact.email`, `contact.phone`, `contact.birthday`, `custom_field.<key>` (or `contact.custom.<key>`), `tenant.name`, `agent.name`, `agent.nickname`, or `custom:<fixed text>`.\n\nPer variable the value is taken from, in order: a non-blank `variable_values` entry, this mapping, the template's saved mapping, then `variable_fallbacks`. If it is still blank the request is rejected with 400 before anything is sent."
    },
    "variable_fallbacks": {
      "anyOf": [
        {
          "additionalProperties": {
            "type": "string"
          },
          "type": "object"
        },
        {
          "type": "null"
        }
      ],
      "title": "Variable Fallbacks",
      "description": "OPTIONAL. Fixed fallback text per template variable, keyed like `variable_mapping`. Used when the variable otherwise resolves to an empty value (e.g. the contact has no first name)."
    },
    "language": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 10
        },
        {
          "type": "null"
        }
      ],
      "title": "Language",
      "description": "OPTIONAL. Template language to send when the template is referenced BY NAME and that name exists in multiple languages. Use the exact code stored in `whatsapp_templates.language` — the Meta language/locale code (e.g. `en`, `de`, `en_US`, `pt_BR`).\n\nResolution when a name maps to multiple language rows (after the channel filter): if only one language remains it is used; if several remain, an explicit `language` selects that variant (400 if no such variant), else the recipient contact's stored `language` is matched (exact, then base-language family e.g. `de_DE`↔`de`), else 400 asking for `language`. When the template is referenced by DB ID the language is fixed by that row; passing a conflicting `language` returns 400."
    },
    "conversation_id": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Conversation Id",
      "description": "Link to existing conversation (creates new if not provided)"
    },
    "header_media_url": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Header Media Url",
      "description": "Header media reference for IMAGE/VIDEO/DOCUMENT templates. Accepts ANY of:\n- a bare `attachment_id` (UUID) returned by `POST /attachments` or `POST /whatsapp-templates/upload-media`\n- the proxy URL `/api/v1/attachments/{id}/download` (relative or absolute)\n- the permanent public URL `/api/v1/attachments/public/{tenant_id}/{id}`\n- a legacy `https://storage.googleapis.com/...` GCS URL\n- any absolute external `https://` URL (customer CDN, etc.)\n\nThe server resolves the input to a Meta-deliverable URL via `WhatsAppHeaderMediaService`. Field name is kept for backwards compatibility — it has not been URL-only since May 2026."
    },
    "header_document_filename": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 255
        },
        {
          "type": "null"
        }
      ],
      "title": "Header Document Filename",
      "description": "Optional filename override for DOCUMENT-type template headers. Must include the correct file extension matching the document (e.g. 'invoice.pdf' for a PDF). Ignored for non-DOCUMENT headers."
    },
    "cards": {
      "anyOf": [
        {
          "items": {
            "$ref": "#/$defs/SendTemplateCardParams"
          },
          "type": "array",
          "maxItems": 10
        },
        {
          "type": "null"
        }
      ],
      "title": "Cards",
      "description": "Per-card parameters for a CAROUSEL template, supplied POSITIONALLY — entry 0 fills the first card, entry 1 the second, etc. Provide one entry per card you want to personalize. When ABSENT the non-carousel send path is unchanged. Each entry carries its own `variable_values` (same positional/named rules as the top-level field) and `header_media_url`. Only meaningful for CAROUSEL templates; ignored otherwise."
    }
  },
  "type": "object",
  "$defs": {
    "SendTemplateCardParams": {
      "properties": {
        "variable_values": {
          "anyOf": [
            {
              "items": {
                "type": "string"
              },
              "type": "array"
            },
            {
              "additionalProperties": {
                "type": "string"
              },
              "type": "object"
            },
            {
              "type": "null"
            }
          ],
          "title": "Variable Values",
          "description": "Values for this card's body placeholders. Same shape rules as the top-level `variable_values`: a list (positional only) or a dict (numeric keys for positional, identifier keys for named). Must match the template's `parameter_format`. Reserved dict keys (`url_suffix`, `otp_code`, and their indexed variants) apply per card."
        },
        "header_media_url": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "title": "Header Media Url",
          "description": "Header media reference for THIS card's IMAGE/VIDEO header. Accepts the same forms as the top-level `header_media_url` (bare attachment id, proxy URL, permanent public URL, legacy GCS URL, or an external https URL). Each carousel card has its own header media, so supply one per card."
        }
      },
      "type": "object",
      "title": "SendTemplateCardParams",
      "description": "Per-card send parameters for a CAROUSEL template.\n\nSupplied positionally in ``SendTemplateRequest.cards`` — index 0 fills the\nfirst card, index 1 the second, and so on. Each card's ``variable_values``\nfollows the SAME positional/named rules (and reserved-key handling) as the\ntop-level body ``variable_values``, resolved against that card's own body\nplaceholders. This is the template-send INPUT shape; the service maps it\nonto the unified carousel content representation before dispatch."
    }
  }
}