# Add document template field

Add a named field or a text-document insert to a template. Choose before or after a landmark returned by ListDocumentTemplateFields, or end. A field is filled from its instruction; an insert copies its selected text document at a pinned version when a document is generated. Returns the stable field ID and draft version.

POST https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField

## Access & visibility

Allow `documents.AddDocumentTemplateField` on your API key.

**Acting member required.** Send `Brigg-Act-As` with an [approved member account ID](https://developer.brigg.app/authentication#member-delegation). The key supplies permissions; the member selects whose data is used.

The selected member must be active and approved by the credential. This selects whose private data is used; the credential supplies permissions. Private ownership and project boundaries still apply.

[Set up authentication](https://developer.brigg.app/authentication) · [Choose permissions](https://developer.brigg.app/permissions)

## Request fields

Send a JSON object with the following fields. Required fields must be included; omit optional fields you do not need.
#### kind = "field"

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `template_id` | string | Yes | Use a template_id from the template catalogue; this is not the ID of a generated document. [workspace.ListTemplates](https://developer.brigg.app/reference/workspace/ListTemplates). At least 1 character. |
| `kind` | string | Yes | field adds generated text from an instruction; insert references an existing collaborative text document. Choose one request variant. Always `"field"`. |
| `instruction` | string | Yes | Instructions used to fill this template field. At least 1 character. At most 500 characters. |
| `placement` | [object (2 variants)](#request-variant1-placement) | Yes | Place the field before or after a named landmark, or at the end of the template. List the current landmarks first. No HTML or byte offset is accepted. |

### request.variant1.placement

#### Variant 1

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `position` | string | Yes | Where to place the new field or insert relative to a landmark, or at the end. Allowed: `"before"`, `"after"`. |
| `landmark_id` | string | Yes | Stable insertion landmark from documents.ListDocumentTemplateFields. Use it with position before or after; position end needs no landmark. Pattern: `^[a-z0-9][a-z0-9-]{0,63}$`. |

#### position = "end"

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `position` | string | Yes | Where to place the new field or insert relative to a landmark, or at the end. Always `"end"`. |

#### kind = "insert"

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `template_id` | string | Yes | Use a template_id from the template catalogue; this is not the ID of a generated document. [workspace.ListTemplates](https://developer.brigg.app/reference/workspace/ListTemplates). At least 1 character. |
| `kind` | string | Yes | field adds generated text from an instruction; insert references an existing collaborative text document. Choose one request variant. Always `"insert"`. |
| `file_id` | string | Yes | An existing collaborative text document from ListDocuments (kind=text_document). An uploaded PDF is not an insertable text document. At least 1 character. |
| `layout` | string | No | Display layout for the inserted text document. Allowed: `"one-column"`, `"two-column"`, `"compact"`. |
| `placement` | [object (2 variants)](#request-variant2-placement) | Yes | Place the field before or after a named landmark, or at the end of the template. List the current landmarks first. No HTML or byte offset is accepted. |

### request.variant2.placement

#### Variant 1

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `position` | string | Yes | Where to place the new field or insert relative to a landmark, or at the end. Allowed: `"before"`, `"after"`. |
| `landmark_id` | string | Yes | Stable insertion landmark from documents.ListDocumentTemplateFields. Use it with position before or after; position end needs no landmark. Pattern: `^[a-z0-9][a-z0-9-]{0,63}$`. |

#### position = "end"

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `position` | string | Yes | Where to place the new field or insert relative to a landmark, or at the end. Always `"end"`. |

## Inspect, edit, then inspect again

Use [List document template fields](https://developer.brigg.app/reference/documents/ListDocumentTemplateFields) to get this template's generated fields, inserted documents and placement landmarks. A generated field contains an instruction; an insert references an existing text document. Use the returned field_id names and landmark_id values exactly.

Edits create a new template version and require template-management authority, not just read access. Pending or completed signing can lock the template. Existing generated documents are not regenerated by a template edit. The returned version_id records the changed template; it is not a precondition for another edit. Re-list before selecting a placement in the changed structure.

## Response

Returns **200 OK** with a JSON body.
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `field_id` | string | Yes | Server-assigned stable field or insert name. Keep it for subsequent instruction/layout changes or removal. |
| `version_id` | string | Yes | New template version containing this edit. It is not a collaborative text-document version or an optimistic-lock token. Re-list template fields to inspect the current structure after the edit. |

## Errors & retries

Set `BRIGG_REQUEST_ID` once for each logical action. The samples send it as `Idempotency-Key`. Reuse the same value and body on retries, including after a timeout; a missing response does not prove the action failed.

For rate limits, follow the server’s retry guidance and back off. See [HTTP errors](https://developer.brigg.app/http#errors) and [safe retries and limits](https://developer.brigg.app/reliability).

| HTTP status | Code | What to do |
| --- | --- | --- |
| 400 | `invalid_json` | Send a JSON object with Content-Type: application/json. |
| 400 | `invalid_input` | Check the request fields and types. Supply a stable Idempotency-Key containing 1–200 characters. |
| 401 | `invalid_key` | The workspace API key is missing, invalid or revoked. |
| 403 | `forbidden` | Check the key’s operation and resource grants. Supply Brigg-Act-As with a member authorized by the key’s delegation grant. The selected member must be active and approved by the credential. This selects whose private data is used; the credential supplies permissions. Private ownership and project boundaries still apply. |
| 404 | `not_found` | The requested operation or resource is not available to this credential. |
| 429 | `rate_limited` | Wait for Retry-After before retrying. Reuse the original Idempotency-Key. |
| 409 | `signing_locked` | Pending or completed signing protects this template. Do not retry the edit unchanged. |
| 400 | `not_a_template` | Choose a document template from ListTemplates, not a generated document. |
| 404 | `no_active_version` | This template has no editable version. Open it in Brigg to complete its setup. |
| 409 | `anchor_not_found` | The placement landmark no longer exists. Re-list template fields before selecting a position. |
| 409 | `anchor_ambiguous` | The landmark does not uniquely identify a position. Inspect the template and choose another current landmark or position end. |
| 400 | `not_a_text_document` | The insert must reference a collaborative text document; an uploaded PDF or generated document is not valid here. |

## Next steps

- [List document template fields](https://developer.brigg.app/reference/documents/ListDocumentTemplateFields)
- [Update document template field](https://developer.brigg.app/reference/documents/UpdateDocumentTemplateField)
- [Remove document template field](https://developer.brigg.app/reference/documents/RemoveDocumentTemplateField)
- [List documents](https://developer.brigg.app/reference/documents/ListDocuments)
- [List templates](https://developer.brigg.app/reference/workspace/ListTemplates)

[Download OpenAPI](https://developer.brigg.app/openapi.json) · [Versioning & retirement](https://developer.brigg.app/versioning)

## Examples

### Add generated text at the end

Brigg assigns the field ID; the instruction supplies the generation behavior.
#### cURL


```sh
curl 'https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField' \
  -H "Authorization: Bearer $BRIGG_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Brigg-Act-As: $BRIGG_ACT_AS" \
  -H "Idempotency-Key: $BRIGG_REQUEST_ID" \
  --data-raw '{
  "template_id": "c_template_proposal",
  "kind": "field",
  "instruction": "Summarize the project objectives and agreed deliverables.",
  "placement": {
    "position": "end"
  }
}'
```

#### TypeScript


```typescript
const actAs = process.env.BRIGG_ACT_AS;
if (!actAs) throw new Error("Set BRIGG_ACT_AS to an approved member account ID.");

const requestId = process.env.BRIGG_REQUEST_ID;
if (!requestId) throw new Error("Set BRIGG_REQUEST_ID once per action and reuse it on retries.");

const response = await fetch("https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.BRIGG_API_KEY}`,
    "Content-Type": "application/json",
    "Brigg-Act-As": actAs,
    "Idempotency-Key": requestId, // Create once per action; reuse on retries.
  },
  body: JSON.stringify({
    "template_id": "c_template_proposal",
    "kind": "field",
    "instruction": "Summarize the project objectives and agreed deliverables.",
    "placement": {
      "position": "end"
    }
  }),
});
const result = await response.json();
if (!response.ok) throw new Error(`${response.status}: ${result.error} — ${result.message}`);
console.log(result);
```

#### Python


```python
import json
import os
from urllib.request import Request, urlopen
from urllib.error import HTTPError

request_id = os.environ["BRIGG_REQUEST_ID"]  # Reuse for retries of this action.
payload = {
  "template_id": "c_template_proposal",
  "kind": "field",
  "instruction": "Summarize the project objectives and agreed deliverables.",
  "placement": {
    "position": "end"
  }
}
request = Request(
    "https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField",
    data=json.dumps(payload).encode("utf-8"),
    headers={
        "Authorization": f"Bearer {os.environ['BRIGG_API_KEY']}",
        "Content-Type": "application/json",
        "Brigg-Act-As": os.environ["BRIGG_ACT_AS"],
        "Idempotency-Key": request_id,  # Create once per action; reuse on retries.
    },
    method="POST",
)
try:
    with urlopen(request) as response:
        print(json.load(response))
except HTTPError as error:
    print(error.code, json.load(error))
    raise
```

#### Response example


```json
{
  "field_id": "field-2",
  "version_id": "v_proposal_4"
}
```

### Insert existing terms after a landmark

Use an exact current landmark and a readable collaborative text document.
#### cURL


```sh
curl 'https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField' \
  -H "Authorization: Bearer $BRIGG_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Brigg-Act-As: $BRIGG_ACT_AS" \
  -H "Idempotency-Key: $BRIGG_REQUEST_ID" \
  --data-raw '{
  "template_id": "c_template_proposal",
  "kind": "insert",
  "file_id": "f_terms",
  "layout": "one-column",
  "placement": {
    "position": "after",
    "landmark_id": "project-summary"
  }
}'
```

#### TypeScript


```typescript
const actAs = process.env.BRIGG_ACT_AS;
if (!actAs) throw new Error("Set BRIGG_ACT_AS to an approved member account ID.");

const requestId = process.env.BRIGG_REQUEST_ID;
if (!requestId) throw new Error("Set BRIGG_REQUEST_ID once per action and reuse it on retries.");

const response = await fetch("https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.BRIGG_API_KEY}`,
    "Content-Type": "application/json",
    "Brigg-Act-As": actAs,
    "Idempotency-Key": requestId, // Create once per action; reuse on retries.
  },
  body: JSON.stringify({
    "template_id": "c_template_proposal",
    "kind": "insert",
    "file_id": "f_terms",
    "layout": "one-column",
    "placement": {
      "position": "after",
      "landmark_id": "project-summary"
    }
  }),
});
const result = await response.json();
if (!response.ok) throw new Error(`${response.status}: ${result.error} — ${result.message}`);
console.log(result);
```

#### Python


```python
import json
import os
from urllib.request import Request, urlopen
from urllib.error import HTTPError

request_id = os.environ["BRIGG_REQUEST_ID"]  # Reuse for retries of this action.
payload = {
  "template_id": "c_template_proposal",
  "kind": "insert",
  "file_id": "f_terms",
  "layout": "one-column",
  "placement": {
    "position": "after",
    "landmark_id": "project-summary"
  }
}
request = Request(
    "https://api.brigg.app/v1/operations/documents.AddDocumentTemplateField",
    data=json.dumps(payload).encode("utf-8"),
    headers={
        "Authorization": f"Bearer {os.environ['BRIGG_API_KEY']}",
        "Content-Type": "application/json",
        "Brigg-Act-As": os.environ["BRIGG_ACT_AS"],
        "Idempotency-Key": request_id,  # Create once per action; reuse on retries.
    },
    method="POST",
)
try:
    with urlopen(request) as response:
        print(json.load(response))
except HTTPError as error:
    print(error.code, json.load(error))
    raise
```

#### Response example


```json
{
  "field_id": "insert-2",
  "version_id": "v_proposal_4"
}
```

## JSON schemas


```json-schema-request
{
  "anyOf": [
    {
      "type": "object",
      "properties": {
        "template_id": {
          "type": "string",
          "minLength": 1,
          "description": "Use a template_id from the template catalogue; this is not the ID of a generated document. [workspace.ListTemplates](https://developer.brigg.app/reference/workspace/ListTemplates)."
        },
        "kind": {
          "type": "string",
          "const": "field",
          "description": "field adds generated text from an instruction; insert references an existing collaborative text document. Choose one request variant."
        },
        "instruction": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500,
          "description": "Instructions used to fill this template field."
        },
        "placement": {
          "anyOf": [
            {
              "type": "object",
              "properties": {
                "position": {
                  "type": "string",
                  "enum": [
                    "before",
                    "after"
                  ],
                  "description": "Where to place the new field or insert relative to a landmark, or at the end."
                },
                "landmark_id": {
                  "type": "string",
                  "pattern": "^[a-z0-9][a-z0-9-]{0,63}$",
                  "description": "Stable insertion landmark from documents.ListDocumentTemplateFields. Use it with position before or after; position end needs no landmark."
                }
              },
              "required": [
                "position",
                "landmark_id"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "position": {
                  "type": "string",
                  "const": "end",
                  "description": "Where to place the new field or insert relative to a landmark, or at the end."
                }
              },
              "required": [
                "position"
              ],
              "additionalProperties": false
            }
          ],
          "description": "Place the field before or after a named landmark, or at the end of the template. List the current landmarks first. No HTML or byte offset is accepted."
        }
      },
      "required": [
        "template_id",
        "kind",
        "instruction",
        "placement"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "template_id": {
          "type": "string",
          "minLength": 1,
          "description": "Use a template_id from the template catalogue; this is not the ID of a generated document. [workspace.ListTemplates](https://developer.brigg.app/reference/workspace/ListTemplates)."
        },
        "kind": {
          "type": "string",
          "const": "insert",
          "description": "field adds generated text from an instruction; insert references an existing collaborative text document. Choose one request variant."
        },
        "file_id": {
          "type": "string",
          "minLength": 1,
          "description": "An existing collaborative text document from ListDocuments (kind=text_document). An uploaded PDF is not an insertable text document."
        },
        "layout": {
          "type": "string",
          "enum": [
            "one-column",
            "two-column",
            "compact"
          ],
          "description": "Display layout for the inserted text document."
        },
        "placement": {
          "anyOf": [
            {
              "type": "object",
              "properties": {
                "position": {
                  "type": "string",
                  "enum": [
                    "before",
                    "after"
                  ],
                  "description": "Where to place the new field or insert relative to a landmark, or at the end."
                },
                "landmark_id": {
                  "type": "string",
                  "pattern": "^[a-z0-9][a-z0-9-]{0,63}$",
                  "description": "Stable insertion landmark from documents.ListDocumentTemplateFields. Use it with position before or after; position end needs no landmark."
                }
              },
              "required": [
                "position",
                "landmark_id"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "position": {
                  "type": "string",
                  "const": "end",
                  "description": "Where to place the new field or insert relative to a landmark, or at the end."
                }
              },
              "required": [
                "position"
              ],
              "additionalProperties": false
            }
          ],
          "description": "Place the field before or after a named landmark, or at the end of the template. List the current landmarks first. No HTML or byte offset is accepted."
        }
      },
      "required": [
        "template_id",
        "kind",
        "file_id",
        "placement"
      ],
      "additionalProperties": false
    }
  ],
  "examples": [
    {
      "template_id": "c_template_proposal",
      "kind": "field",
      "instruction": "Summarize the project objectives and agreed deliverables.",
      "placement": {
        "position": "end"
      }
    }
  ]
}
```


```json-schema-response
{
  "type": "object",
  "properties": {
    "field_id": {
      "type": "string",
      "description": "Server-assigned stable field or insert name. Keep it for subsequent instruction/layout changes or removal."
    },
    "version_id": {
      "type": "string",
      "description": "New template version containing this edit. It is not a collaborative text-document version or an optimistic-lock token. Re-list template fields to inspect the current structure after the edit."
    }
  },
  "required": [
    "field_id",
    "version_id"
  ],
  "additionalProperties": false,
  "examples": [
    {
      "field_id": "field-2",
      "version_id": "v_proposal_4"
    }
  ]
}
```

