# POST /forms/templates/:id/wiring

**Resource:** [Forms](./forms.md)  
**Scopes:** `forms:write`  
**Write operation:** yes

Bulk-update CRM variable wiring on a form template's fields in one atomic call. Optionally creates new workspace custom fields on company_settings before applying mappings -- useful for wiring AI-generated templates whose fields need new CRM targets. Fields not listed in mappings are untouched. Pass crm_variable: null to clear an existing wiring. Requires forms:write scope.

## Parameters

| Name | In | Type | Required | Description |
|------|----|------|----------|-------------|
| `id` | path | uuid | yes | Form template ID |
| `mappings` | body | object[] | no | Array of { field_id (uuid, required), crm_variable (string or null to clear), crm_variable_mode ("write" \| "overwrite", default "overwrite") }. At least one of mappings or create_custom_fields must be provided. |
| `create_custom_fields` | body | object[] | no | Array of { label (string), type ("text"\|"textarea"\|"dropdown"\|"number"\|"datetime"\|"checkbox"), entity ("deal"\|"account"\|"contact"), options (string[], required for dropdown) }. New fields are persisted to company_settings.custom_fields before mappings are applied, so you can immediately wire to them. |

## Request example

```bash
{
  "mappings": [
    {
      "field_id": "uuid-field-1",
      "crm_variable": "contact.first_name",
      "crm_variable_mode": "overwrite"
    },
    {
      "field_id": "uuid-field-2",
      "crm_variable": "account.tax_number",
      "crm_variable_mode": "write"
    },
    {
      "field_id": "uuid-field-3",
      "crm_variable": "contact.metadata.marital_status",
      "crm_variable_mode": "overwrite"
    }
  ],
  "create_custom_fields": [
    {
      "label": "Marital Status",
      "type": "dropdown",
      "entity": "contact",
      "options": [
        "Single",
        "Married",
        "De facto",
        "Divorced",
        "Widowed"
      ]
    }
  ]
}
```

## Response example

```json
{
  "data": {
    "updated_fields": [
      {
        "id": "uuid-field-1",
        "label": "First Name",
        "type": "text",
        "content": {
          "crm_variable": "contact.first_name",
          "crm_variable_mode": "overwrite"
        }
      },
      {
        "id": "uuid-field-2",
        "label": "ABN",
        "type": "text",
        "content": {
          "crm_variable": "account.tax_number",
          "crm_variable_mode": "write"
        }
      },
      {
        "id": "uuid-field-3",
        "label": "Marital Status",
        "type": "select",
        "content": {
          "crm_variable": "contact.metadata.marital_status",
          "crm_variable_mode": "overwrite"
        }
      }
    ],
    "created_custom_fields": [
      {
        "entity": "contact",
        "id": "marital_status",
        "label": "Marital Status",
        "type": "dropdown",
        "variable": "contact.metadata.marital_status"
      }
    ],
    "stats": {
      "fields_updated": 3,
      "custom_fields_created": 1
    }
  },
  "meta": {
    "credits_remaining": 9490
  }
}
```

---
Base URL: `https://api.trustpager.com/functions/v1/api/v1` — Auth: `Authorization: Bearer YOUR_API_KEY`