# Template Create (https://developers.doctavian.com/openapi/latest/reference/Signatures/envelope-template-signatures-template-create)

## `POST /signatures/template/create`

**Template Create**

Creates a reusable envelope template: a stored envelope configuration — documents, fields, signing order, notification settings — with placeholders in place of real recipients. Envelopes are then created from it with `signatures-envelope-create-from-template`, supplying only what changes between instances.

A template has no real recipients, so each one is a placeholder identified by its `templateRole` — a label such as `Counterparty` or `Approver`. Every `templateRole` in the request must be unique across recipients and sign groups together, otherwise the request is rejected with `DUPLICATE_TEMPLATE_ROLE`. A recipient that is not in a sign group must carry one, and every sign group must.

`template.templateName` is the only value the server requires. `documents`, `recipients`, `fields` and `signGroups` may all be absent or empty, so a template can be saved as a stub and completed later with `signatures-template-update` — `signatures-envelope-create` has no equivalent allowance.

To file the new template in a folder, send the folder's id as `template.folderId`; the response returns the folder's name as `template.folderName`. Without it, the template is saved at the root.

Documents still have to be in Storage first. Upload each one with `signatures-document-upload` and reference the returned id in `documents[].urn` with `documents[].loadMethod` set to `Storage`; there is no way to send document content inline. Storage keeps an uploaded document for 24 hours, and a reference that cannot be resolved fails the request with a `500` carrying `FILE_NOT_EXISTS_IN_STORAGE`.

This operation is metered. A successful call reports one unit of the `templates-created` dimension, returned in `consumption`.

### Request body (`application/json`)

- `documents` (array<DocumentTemplateInput>) — The documents the envelope template is built from. Optional — unlike `signatures-envelope-create`, a template may be saved with none.
  - `referenceDocumentId` (integer) **required** — Client-assigned correlation id used to link fields to this document within the same request; not a server document id.
  - `name` (string) **required** — Display name for the document within the template. Mandatory; rejected with `DOCUMENT_NAME_REQUIRED` when absent.
  - `loadMethod` (string) — How to source the document's content. `Storage` is the only recognized value, and it fetches the bytes identified by `urn`. There is no inline or base64 content field. Anything else, or omitting it, fails the request with the same `500` `FILE_NOT_EXISTS_IN_STORAGE` as a missing file rather than a validation error — always send `Storage`.
  - `urn` (string) — Storage id of the file, as returned by `signatures-document-upload` — or any other Storage file id, such as the `urn` of a document generated with `deliveryMethod` `Storage`. Only consulted when `loadMethod` is `Storage`. Using the id moves the file into the envelope template: it is removed from Storage as soon as the envelope template is created, and the id stops resolving. Each id can therefore be used once — send the same id again, to another create or to an update, and the request fails with `FILE_NOT_EXISTS_IN_STORAGE`; upload the file again instead. If no file can be found for this id, the request fails with a `500` carrying `FILE_NOT_EXISTS_IN_STORAGE`.
  - `externalContext` (any) — Arbitrary JSON object for this document, stored with it and echoed back unchanged in the response and by `signatures-template-get`. Its shape is not validated, fixed or interpreted — use it for whatever the integration needs, such as your own record id or a correlation key. One common use is matching the entry in the response back to the document you sent: the response returns server-generated ids and does not echo the client reference id, so an `externalContext` is what ties the two together.
- `fields` (array<FieldTemplateInput>) — The fields placed on the documents for recipients to complete or sign. Optional.
  - `referenceSignerId` (integer) — The `recipients[].referenceSignerId` of the placeholder who completes this field. Set this or `referenceSignGroupId`, never both and never neither.
  - `referenceSignGroupId` (integer) — The `signGroups[].referenceSignGroupId` of the group whose members may complete this field. Set this or `referenceSignerId`, never both and never neither.
  - `referenceDocumentId` (integer) **required** — The `documents[].referenceDocumentId` of the document this field is placed on. Mandatory; rejected with `FIELD_REFERENCE_DOCUMENT_ID_REQUIRED` when absent.
  - `name` (string) **required** — PDF field name.
  - `value` (string) — Required (non-empty) only for type=note; format otherwise depends entirely on type and is not validated by the server.
  - `type` (any) **required** — What kind of field this is, which determines how it is rendered and what the recipient does with it. Mandatory; rejected with `FIELD_TYPE_REQUIRED` when absent and `FIELD_TYPE_INVALID` when outside the listed set. At most one `digitalsignature` field is allowed per recipient (or per sign group) per document — a second is rejected with `ONLY_ONE_DIGITAL_SIGNATURE_FIELD_ALLOWED`.
  - `anchorString` (string) — Text to search for in the document; the field is placed onto that text rather than at fixed coordinates, and takes the same dimensions as the matched text, so the length of the anchor determines the width of the field. Matching is case-sensitive, and a field is placed at **every** occurrence of the text in the document. When the text occurs once, the field keeps this `name`. When it occurs more than once, one field is created per occurrence and each is named with a numeric suffix — `myFieldName__1`, `myFieldName__2` and so on — so field names stay unique. Read values back, and match fields in responses, by those suffixed names. Anchors are resolved while the envelope is created, against the PDF the document was converted to. Supply this or the full coordinate set, never both. The anchor text is left in the document exactly as authored. It is not removed, and the field placed over it does not hide it, so format it in the document to be invisible — normally by setting its font color to the background color, white on a white page. An anchor left in a readable color is printed in the signed document and seen by every recipient. Wrap the anchor in a delimiter that will not occur in ordinary prose — `@signature_buyer@` rather than `Sign here:` — so that it matches only where you intended. Because every occurrence produces a field, an anchor made of everyday words picks up headings, footers and body text as well as the spot you had in mind. The delimiter is a convention, not syntax: `@` has no special meaning to the matcher and the text is matched literally, so any distinctive wrapper works as long as you use it consistently in the template.
  - `page` (integer) — 1-based page number the field is placed on. Part of the coordinate placement set — supply `page`, `height`, `width`, `positionX` and `positionY` together, or use `anchorString` instead.
  - `height` (number) — Height of the field in pixels. Part of the coordinate placement set — supply `page`, `height`, `width`, `positionX` and `positionY` together, or use `anchorString` instead.
  - `width` (number) — Width of the field in pixels. Part of the coordinate placement set — supply `page`, `height`, `width`, `positionX` and `positionY` together, or use `anchorString` instead.
  - `positionX` (number) — Horizontal position of the field's top-left corner, in pixels measured from the left edge of the page. Part of the coordinate placement set — supply `page`, `height`, `width`, `positionX` and `positionY` together, or use `anchorString` instead.
  - `positionY` (number) — Vertical position of the field's top-left corner, in pixels measured from the top edge of the page. Larger values move the field down the page. Part of the coordinate placement set — supply `page`, `height`, `width`, `positionX` and `positionY` together, or use `anchorString` instead.
  - `isRequired` (boolean) — Whether the recipient must complete this field before they can finish signing.
  - `tooltip` (string) — Help text shown to the recipient when they reach the field.
  - `properties` (any) — Styling and behavior for this field.
- `recipients` (array<RecipientTemplateInput>) — The placeholder recipients this envelope template defines. Optional. A template has no real recipients, so each one is a placeholder identified by its `templateRole` — a label such as `Counterparty` or `Approver`. The actual name and email are supplied later, when an envelope is created from the template.
  - `referenceSignerId` (integer) — Client-assigned id for this placeholder recipient, unique within the request. `fields[].referenceSignerId` uses it to assign fields to this placeholder. The server-generated id is returned in the response.
  - `referenceSignGroupId` (integer) — The `signGroups[].referenceSignGroupId` of the group this placeholder belongs to. When set, the group's `role`, signing order and mandatory flag govern this recipient, and `role` and `templateRole` on the recipient are not validated.
  - `name` (string) — Optional default name for whoever fills this role. Unlike an envelope recipient, an envelope template recipient does not have to carry one — supply the real name when creating an envelope from the template.
  - `email` (string) _(format: email)_ — Optional default email address for whoever fills this role. Only the address format is checked, with `RECIPIENT_INVALID_EMAIL_ADDRESS` on a malformed value; unlike an envelope recipient, an envelope template recipient does not have to carry one.
  - `role` (any) — What whoever fills this role does with the envelope. Validated only when the recipient has no `referenceSignGroupId`; for a grouped recipient the group's role applies instead. A value outside the listed set is rejected with `RECIPIENT_ROLE_INVALID`.
  - `templateRole` (string) — Must be unique within the request across recipients and signGroups.
  - `signOrder` (integer) — Required to be non-zero only when template.isSignOrder is true.
  - `mandatory` (boolean) — Whether whoever fills this role must complete their part for the envelope to complete.
  - `hostEmail` (string) _(format: email)_ — Email address of the person who facilitates in-person signing for this recipient. That host runs the signing session, and must open the signing using this exact address — an address that does not match cannot open it. The notification for this recipient goes to the host, not to the recipient, and tells the host the signing is an in-person flow. Only the address format is checked here. Unlike an envelope recipient, an envelope template recipient whose role is `inpersonsigner` is not required to carry one.
  - `isMfaEnabled` (boolean) — Whether this recipient must pass multi-factor authentication before signing. Requires the subscription to include MFA. When the account's MFA channel is not email, `phoneNumber` becomes mandatory on the envelope created from this envelope template.
  - `phoneNumber` (string) — No format validated. Required only if isMfaEnabled is true and the account's MFA channel is not email.
  - `language` (any) — Language the signing pages are shown in for whoever fills this placeholder role on an envelope created from the envelope template. Optional. Must be `en` or `es`, matched without regard to case and stored in lowercase; any other value is rejected with `RECIPIENT_LANGUAGE_INVALID`. When omitted, null or empty, the placeholder gets the calling user's `ENVELOPE_RECIPIENT_LANGUAGE` setting, which can be set at account, subscription or user level and is `en` unless changed. `signatures-envelope-create-from-template` copies it to the recipient unless that request's `recipients[].language` or `signGroups[].language` replaces it.
  - `externalContext` (any) — Arbitrary JSON object for this placeholder recipient, stored with it and echoed back unchanged in the response and by `signatures-template-get`. Its shape is not validated, fixed or interpreted — use it for whatever the integration needs, such as your own record id or a correlation key. One common use is matching the entry in the response back to the placeholder recipient you sent: the response returns server-generated ids and does not echo the client reference id, so an `externalContext` is what ties the two together.
- `signGroups` (array<SignGroupTemplateInput>) — Groups of placeholder recipients that share a role, a position in the signing order and a set of fields. Optional.
  - `referenceSignGroupId` (integer) **required** — Client-assigned id for this sign group, unique within the request. Mandatory; rejected with `REFERENCE_SIGN_GROUP_ID_REQUIRED` when absent. `recipients[].referenceSignGroupId` and `fields[].referenceSignGroupId` both point at it.
  - `name` (string) — Optional display name for the group. Unlike an envelope sign group, an envelope template sign group does not have to carry one.
  - `role` (any) **required** — What members of this group do with the envelope. Mandatory; rejected with `SIGN_GROUP_ROLE_REQUIRED` when absent and `SIGN_GROUP_ROLE_INVALID` when outside the listed set. It governs every recipient assigned to the group, whose own `role` is then not validated.
  - `signOrder` (integer) — Required to be non-zero only when template.isSignOrder is true.
  - `mandatory` (boolean) — Whether this group must complete its part for the envelope to complete.
  - `templateRole` (string) **required** — Must be unique within the request across recipients and signGroups.
- `template` (any) — Settings that apply to the envelope template and to every envelope created from it: the template's own name and description, the notification email, the sender shown to recipients, signing order, expiry and reminders.
- `externalContext` (any) — Arbitrary JSON object supplied by the caller and echoed back unchanged in the response envelope, on success and on error alike, for correlating a request with its response. Its shape is not validated, fixed or interpreted. To store a context with the envelope template, send it as `template.externalContext`.

### Responses

#### `201` — The envelope template was created. `result.data.template.id` is the id to pass to `signatures-envelope-create-from-template`. `result.data` is the new template in full, in the same shape `signatures-template-get` returns: the template defaults, including the `template.externalContext` stored with it, and every document, placeholder recipient, sign group and field with its server-generated id. Match each returned entry to the object you sent by its marker: documents and recipients echo the `externalContext` supplied for them, and sign groups and fields echo their `name`, which is unique across the request. The client reference ids are not echoed back. `consumption` reports one unit of `templates-created`.

`application/json`:

_any_

#### `400` — The request was rejected. `error.innerErrors[].code` identifies the specific problem. Template identity: `TEMPLATE_NAME_REQUIRED`. Template roles: `RECIPIENT_TEMPLATE_ROLE_REQUIRED`, `SIGN_GROUP_TEMPLATE_ROLE_REQUIRED`, `DUPLICATE_TEMPLATE_ROLE`. Reference ids: `REFERENCE_DOCUMENT_ID_REQUIRED`, `REFERENCE_SIGN_GROUP_ID_REQUIRED`, `DUPLICATE_REFERENCE_DOCUMENT_ID`, `DUPLICATE_REFERENCE_RECIPIENT_ID`, `DUPLICATE_REFERENCE_SIGNGROUP_ID`. Documents: `DOCUMENT_NAME_REQUIRED`. Recipients: `RECIPIENT_INVALID_EMAIL_ADDRESS`, `RECIPIENT_ROLE_INVALID`, `RECIPIENT_EITHER_SIGNER_OR_SIGNGROUP_REQUIRED`, `RECIPIENT_SIGN_ORDER_REQUIRED`, `HOST_INVALID_EMAIL_ADDRESS`, `RECIPIENT_LANGUAGE_INVALID`. Sign groups: `SIGN_GROUP_ROLE_REQUIRED`, `SIGN_GROUP_ROLE_INVALID`, `SIGN_GROUP_SIGN_ORDER_REQUIRED`. Fields: `PDF_FIELD_NAME_REQUIRED`, `DUPLICATE_FIELD_NAME`, `FIELD_TYPE_REQUIRED`, `FIELD_TYPE_INVALID`, `FIELD_REFERENCE_DOCUMENT_ID_REQUIRED`, `FIELD_EITHER_SIGNER_OR_SIGNGROUP_REQUIRED`, `FIELD_BOTH_SIGNER_AND_SIGNGROUP_CANNOT_BE_SET`, `ANCHOR_NOT_PROVIDED_POSITION_REQUIRED`, `ANCHOR_PROVIDED_POSITION_NOT_REQUIRED`, `ONLY_ONE_DIGITAL_SIGNATURE_FIELD_ALLOWED`, `FIELD_REGEX_PATTERN_INVALID`. Template settings: `SENDER_INVALID_EMAIL_ADDRESS`, `ENVELOPE_EXPIRY_DAYS_TOO_LOW`.

`application/json`:

- `error` (object) **required** — What went wrong. `statusCode` and `message` describe the failure category, `innerErrors` the specific causes.
  - `innerErrors` (array<object>) **required** — One entry per distinct problem found with the request. At least one entry is always present, and a request failing several checks reports each of them separately -- read the whole array rather than only the first entry.
    - `code` (string) **required** — Stable, machine-readable Doctavian error code identifying what went wrong, in SCREAMING_SNAKE_CASE. This is the field to branch on in code: the two message fields are prose and may be reworded, but a code, once published, keeps its meaning.
    - `message` (string) **required** — Technical explanation of this specific failure, written for developers and logs. May reference internal specifics and is not safe to show an end user -- use `userMessage` for anything user-facing.
    - `userMessage` (string) **required** — The same failure phrased for an end user and safe to display in a UI verbatim. Says what the user can do about the problem where there is something to do, and never exposes internal detail.
    - `urn` (string) — The request property this error concerns, when the error is about one property. Optional: most errors leave it out. Named by the property's server-side name in PascalCase, not the JSON field name — `TemplateName` for `template.templateName`. Not related to the `urn` that identifies a file elsewhere in the API.
    - `eventId` (string) _(format: uuid)_ — Identifier of the logged failure behind this entry. Present only when the failure was logged, so treat it as optional. Quote it together with `operationId` when reporting the problem to support.
  - `message` (string) **required** — Short summary of the failure category, mirroring the HTTP status text (e.g. `Bad Request`). It names the class of failure only; the specific cause is in `innerErrors`.
  - `statusCode` (integer) **required** — HTTP status code of the error, repeated inside the body so the envelope stays self-contained when it is logged or forwarded. Always a number, never a string, and always equal to the response's own HTTP status.
  - `externalErrors` (array<any>) — Additional error detail attached as the server produced it. Each entry can be any JSON value and its shape is not fixed: an object returned by an external service the request depended on — such as the Google Apps Script execution layer used for `gdoc`, `gsheet` and `gslide` generation — or a plain string, such as the message of an unhandled exception. Use it for diagnostics only, and branch on `innerErrors[].code` instead. Empty or absent when there is nothing to add.
- `dateTime` (string) **required** _(format: date-time)_ — UTC timestamp when the error response was generated, with between three and seven decimal places. Doctavian's own responses end in `Z`; a response from the gateway — a `401` for a missing or invalid credential, or a `404` for a request that matches no operation — has seven decimal places and ends in `+00:00` instead.
- `operationId` (string) — Identifier of this operation in Doctavian's Azure logs, generated per request and returned on both success and failure. This is the primary reference to quote when reporting a problem to support; when `eventId` is also present, quote both. Left out of a response from the gateway, which answers some requests before they reach Doctavian: a `401` for a missing or invalid credential, and a `404` for a request that matches no operation, such as one without a mandatory query parameter. Such a response has no operation to log, and its `origin` may be empty.
- `eventId` (string) _(format: uuid)_ — Identifier of the corresponding entry in Doctavian's server-side error log. Present only when the failure was handled and logged, so treat it as optional and don't rely on it being there. When reporting a problem to support, quote it together with `operationId`: `operationId` identifies the request that was made, `eventId` identifies the logged failure behind it, and having both is what lets support trace the request end to end. A failure can instead, or also, carry an `eventId` on individual `innerErrors` entries.
- `origin` (string) **required** — The HTTP method and full request URL that produced this response, e.g. `POST https://api.doctavian.com/v1/documents/document/generate`. Useful when the envelope is logged or forwarded away from the call that produced it.
- `userId` (string) _(format: uuid)_ — The unique identifier of the user or system that initiated the request. Left out of the response when the request carried no bearer token — a guest caller on an operation that does not require one, such as a recipient signing with a `tid` — because there is no signed-in user to identify. Also left out when the caller has no Doctavian user yet: `platform-subscription-resolve` and `platform-subscription-trial-start` run before the caller's user exists, so their responses carry no `userId`.
- `externalContext` (object) — Optional, arbitrary JSON object echoed back unchanged from the request's externalContext, if one was supplied. Used to correlate this response with the request that produced it.

#### `500` — The envelope template could not be created. Returned when a document referenced by `documents[].urn` cannot be found in Storage (`FILE_NOT_EXISTS_IN_STORAGE`), and when an unhandled error occurs while saving the template (`FUNCTION_EXECUTION_ERROR`). Nothing is persisted. When one is available, this response carries an `eventId` identifying the logged failure — quote it together with `operationId` when reporting the problem to support.

`application/json`:

- `error` (object) **required** — What went wrong. `statusCode` and `message` describe the failure category, `innerErrors` the specific causes.
  - `innerErrors` (array<object>) **required** — One entry per distinct problem found with the request. At least one entry is always present, and a request failing several checks reports each of them separately -- read the whole array rather than only the first entry.
    - `code` (string) **required** — Stable, machine-readable Doctavian error code identifying what went wrong, in SCREAMING_SNAKE_CASE. This is the field to branch on in code: the two message fields are prose and may be reworded, but a code, once published, keeps its meaning.
    - `message` (string) **required** — Technical explanation of this specific failure, written for developers and logs. May reference internal specifics and is not safe to show an end user -- use `userMessage` for anything user-facing.
    - `userMessage` (string) **required** — The same failure phrased for an end user and safe to display in a UI verbatim. Says what the user can do about the problem where there is something to do, and never exposes internal detail.
    - `urn` (string) — The request property this error concerns, when the error is about one property. Optional: most errors leave it out. Named by the property's server-side name in PascalCase, not the JSON field name — `TemplateName` for `template.templateName`. Not related to the `urn` that identifies a file elsewhere in the API.
    - `eventId` (string) _(format: uuid)_ — Identifier of the logged failure behind this entry. Present only when the failure was logged, so treat it as optional. Quote it together with `operationId` when reporting the problem to support.
  - `message` (string) **required** — Short summary of the failure category, mirroring the HTTP status text (e.g. `Bad Request`). It names the class of failure only; the specific cause is in `innerErrors`.
  - `statusCode` (integer) **required** — HTTP status code of the error, repeated inside the body so the envelope stays self-contained when it is logged or forwarded. Always a number, never a string, and always equal to the response's own HTTP status.
  - `externalErrors` (array<any>) — Additional error detail attached as the server produced it. Each entry can be any JSON value and its shape is not fixed: an object returned by an external service the request depended on — such as the Google Apps Script execution layer used for `gdoc`, `gsheet` and `gslide` generation — or a plain string, such as the message of an unhandled exception. Use it for diagnostics only, and branch on `innerErrors[].code` instead. Empty or absent when there is nothing to add.
- `dateTime` (string) **required** _(format: date-time)_ — UTC timestamp when the error response was generated, with between three and seven decimal places. Doctavian's own responses end in `Z`; a response from the gateway — a `401` for a missing or invalid credential, or a `404` for a request that matches no operation — has seven decimal places and ends in `+00:00` instead.
- `operationId` (string) — Identifier of this operation in Doctavian's Azure logs, generated per request and returned on both success and failure. This is the primary reference to quote when reporting a problem to support; when `eventId` is also present, quote both. Left out of a response from the gateway, which answers some requests before they reach Doctavian: a `401` for a missing or invalid credential, and a `404` for a request that matches no operation, such as one without a mandatory query parameter. Such a response has no operation to log, and its `origin` may be empty.
- `eventId` (string) _(format: uuid)_ — Identifier of the corresponding entry in Doctavian's server-side error log. Present only when the failure was handled and logged, so treat it as optional and don't rely on it being there. When reporting a problem to support, quote it together with `operationId`: `operationId` identifies the request that was made, `eventId` identifies the logged failure behind it, and having both is what lets support trace the request end to end. A failure can instead, or also, carry an `eventId` on individual `innerErrors` entries.
- `origin` (string) **required** — The HTTP method and full request URL that produced this response, e.g. `POST https://api.doctavian.com/v1/documents/document/generate`. Useful when the envelope is logged or forwarded away from the call that produced it.
- `userId` (string) _(format: uuid)_ — The unique identifier of the user or system that initiated the request. Left out of the response when the request carried no bearer token — a guest caller on an operation that does not require one, such as a recipient signing with a `tid` — because there is no signed-in user to identify. Also left out when the caller has no Doctavian user yet: `platform-subscription-resolve` and `platform-subscription-trial-start` run before the caller's user exists, so their responses carry no `userId`.
- `externalContext` (object) — Optional, arbitrary JSON object echoed back unchanged from the request's externalContext, if one was supplied. Used to correlate this response with the request that produced it.