Skip to main content

Formula Fields

Formula fields automatically compute their value from other fields in the template using arithmetic expressions. Use them for totals, taxes, discounts, commissions, and any other calculated values that depend on signer input.

How formulas work

  1. Add a formula property to any number or payment field in the template schema.
  2. Reference other fields by name using {Field Name} syntax.
  3. DocuTrust evaluates the formula in real time as the signer fills in referenced fields.
  4. The computed value updates instantly in the signing form.

Formula syntax

Field references

Reference any field by wrapping its name in curly braces:
Field references are case-sensitive and must match the field’s name property exactly. If the referenced field is empty or has a non-numeric value, it evaluates to 0.

Supported operators

Operator precedence follows standard arithmetic rules. Use parentheses to override precedence.

Numeric literals

You can include numeric constants in formulas:

Schema example

A complete field with a formula:

Full invoice example

An invoice template with quantity, unit price, subtotal, tax, and grand total fields. The last three are formula fields that compute automatically.
When the signer enters Quantity = 10 and Unit Price = 25, the computed values update instantly:

Dynamic payment pricing

Combine formula fields with the payment field type to charge signers a dynamically computed amount based on their input.
The payment amount updates dynamically as the signer fills in quantity, pricing, and other fields. The signer sees the computed total before confirming payment.
Payment fields require payment configuration to be set up in your account settings. See the Payments feature guide for setup instructions.

Creating formulas via API

Add formula fields when creating or updating a template via PUT /api/templates/:id:

Safe evaluation

Formula evaluation is sandboxed and safe:
  • No code execution — only arithmetic expressions are parsed. JavaScript, Ruby, or any other code is rejected.
  • No external access — formulas cannot access variables, functions, or data outside the template fields.
  • Division by zero — dividing by zero returns 0 instead of throwing an error.
  • Non-numeric values — if a referenced field contains a non-numeric value (or is empty), it evaluates to 0.
  • Circular references — if field A references field B and field B references field A, both evaluate to 0 to prevent infinite loops.
  • Maximum length — formulas are limited to 500 characters.
Formula fields that reference other formula fields create a dependency chain. DocuTrust evaluates them in dependency order, but circular references are detected and resolved to 0.
Set formula fields to readonly: true so the signer sees the computed value but cannot override it. If you leave readonly: false, the signer can manually change the calculated value.