> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dualentry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Read and Write Custom Fields Through the API

> Set custom field values on DualEntry record headers and lines through the API: field levels, item shapes, clearing values, and validation errors.

Custom fields store data that DualEntry doesn't model natively, such as an external system's ID, on a record's header or on each of its lines. This guide shows how to set a field's level through the API, send values on records in either of the two accepted item shapes, update and clear values, and fix the validation errors. For which record types accept custom fields, the field types, and where values appear in the app and in reports, see [Custom Fields: Record Types, Types, and Limits](https://docs.dualentry.com/accountants/platform-configuration/custom-fields.md).

## Prerequisites

Before you write custom field values through the API, confirm the following:

* **API key**: Your requests send your organization's API key in the `X-API-KEY` header. See [authenticating your requests](https://docs.dualentry.com/developers/guides/api/api-keys/how-to-authenticate-your-requests.md).
* **Field definition**: A custom field exists for the record type you write to, at the level you need. Create it in the app under **Configuration > Accounting Setup > Custom Fields**, or through the API as described below.
* **Field name or ID**: You know the field's exact name, or its `id` if two fields share a name.

## Set a field's header or line level

A custom field's level is set per record type in its `applies_to` list, so one definition can hold a header value, a line value, or both on each record type. Each entry has a `type`, the record type such as `bill`, and a `layer`: `transaction` for the header level (the default when you omit it) or `entry` for the line level. To show one field at both levels on a record type, send two entries for that type, one per layer.

The following request body creates a text field named **Project code** that bills hold at both the header and line levels. Send it with `POST /public/v2/custom-fields/`:

```json theme={null}
{
  "name": "Project code",
  "field_type": "text",
  "applies_to": [
    { "type": "bill", "layer": "transaction" },
    { "type": "bill", "layer": "entry" }
  ]
}
```

An update with `PUT /public/v2/custom-fields/{custom_field_id}/` replaces the whole `applies_to` list, so send every entry you want to keep. To list fields and find each field's `id`, call `GET /public/v2/custom-fields/`. Filter it with `layer=entry` to list only fields available at the line level.

Because definitions are writable through the API, an integration can create the fields it needs when it's installed and then fill them on every record it writes. Definitions are shared across the organization, so agree on field names with any other integration that writes to the same record types.

## Send custom field values on a record

Record create and update requests take custom field values in `custom_fields` arrays: one at the top level of the record for header values, and one on each line for line values. A field available only at the line level is rejected if you send it in the header `custom_fields`.

To write a value, follow these steps:

1. Find the field's exact name, or its `id` if two fields share a name, with `GET /public/v2/custom-fields/`.
2. Add an item for the field to the record's top-level `custom_fields` for a header value, or to a line's `custom_fields` for a line value, in either shape described below.
3. Send the record's create or update request.
4. Check the `custom_fields` arrays in the response. They return the stored value for the field in the canonical shape.

After these steps, the value appears on the record in the app and in the record's list and detail responses.

Line `custom_fields` are on the line objects for bills, direct expenses, vendor credits, purchase orders, invoices, cash sales, customer credits, sales orders, customer payments, customer deposits, customer refunds, item receipts, item fulfillments, and journal entries in API v1, v2, and v3-alpha, and on quote lines from v2. The public intercompany journal entry endpoints carry no custom fields at either level; enter those values in the app.

The [DualEntry CLI](https://docs.dualentry.com/developers/guides/cli/reference.md) has no line-level options of its own. Put `layer` and line `custom_fields` in the JSON file you pass with `--file` to `dualentry custom-fields create`, `dualentry custom-fields update`, or a record's `create` and `update` commands.

Each item in a `custom_fields` array sets one field. You can write an item in either of two shapes, described below, and mix them in one array.

### Name-keyed shape

A name-keyed item is a one-key object whose key is the field's exact name and whose value is the value to store. In the following bill fragment, the header sets `Department code` and one expense line sets `Project code`. Other required bill and line fields are left out:

```json theme={null}
{
  "custom_fields": [{ "Department code": "OPS" }],
  "expenses": [
    {
      "position": 1,
      "account_number": 6100,
      "custom_fields": [{ "Project code": "P-100" }]
    }
  ]
}
```

### Canonical shape

A canonical item is an object with the field's ID under `field` and the value under `value`:

```json theme={null}
{
  "custom_fields": [
    {
      "field": { "id": 42 },
      "value": { "custom_field_id": 42, "value": "P-100" }
    }
  ]
}
```

Use the canonical shape when two custom fields share a name, because a name-keyed item can't tell them apart.

## Update, clear, and read values

On an update, only the custom fields you send change, and a field you leave out of the array keeps its current value. To clear a field, send its item with the value `null`, either `{ "Project code": null }` or `"value": null` in the canonical shape. On lines, send each existing line's `id` so its values carry over. A line without an `id` is created as a new line.

The following bill update fragment clears `Department code` on the header and changes `Project code` on an existing expense line, identified by its `id`. Other required bill and line fields are left out:

```json theme={null}
{
  "custom_fields": [{ "Department code": null }],
  "expenses": [
    {
      "id": 9876,
      "position": 1,
      "account_number": 6100,
      "custom_fields": [{ "Project code": "P-200" }]
    }
  ]
}
```

After a successful write, the response returns every stored value in the canonical shape, whichever shape you sent. Each item's `field` holds the full field definition, including `id`, `name`, and `field_type`, and `value` holds the stored value with its `custom_field_id` and `custom_field_name`. List and detail endpoints return the same arrays, so you don't need a second call per record to read values.

## Errors

A request with a custom field problem fails with a validation error that lists each problem. The table below lists the messages, what causes each one, and how to fix it:

| Message | Cause | Fix |
| - | - | - |
| `Unknown custom field name: ...` | No field in the organization has that exact name. | Check the name against `GET /public/v2/custom-fields/`. |
| `Unknown custom field id: ...` | No field in the organization has that ID. | Look up the ID with `GET /public/v2/custom-fields/`. |
| `Custom field name '...' is ambiguous — use the canonical shape with the field id` | Two or more fields share the name in a name-keyed item. | Send the item in the canonical shape with the field's `id`. |
| `Custom field '...' does not apply to this record` | The field isn't active for that record type at that level, such as a line-only field sent in the header. | Add the record type and layer to the field's `applies_to`, or send the value at the other level. |
| `Custom field '...': ...` | The value doesn't fit the field's type, such as text in a number field. | Send a value that matches the field's `field_type`. |
| `Name-keyed custom field item must have exactly one key (the custom field name)` | A name-keyed item sets more than one field. | Send one item per field. |
| An error naming the companies the field allows | The field is scoped to companies, and the record's company isn't one of them. | Write to a record in an allowed company, or add the company to the field. |

For the error format that applies to every endpoint, see [Errors](https://docs.dualentry.com/developers/guides/api/core-concepts/errors.md).

## Related pages

* [Custom Fields: Record Types, Types, and Limits](https://docs.dualentry.com/accountants/platform-configuration/custom-fields.md) for the record types, field types, company scoping, and reporting behavior.
* [How to add custom fields to transaction lines](https://docs.dualentry.com/accountants/platform-configuration/how-to-add-custom-fields-to-transaction-lines.md) for setting up line-level fields in the app.
* [List custom fields](https://docs.dualentry.com/developers/api/resources-v2/custom-fields/list-custom-fields.md) and [Create custom field](https://docs.dualentry.com/developers/api/resources-v2/custom-fields/create-custom-field.md) in the API reference.
* [How Custom Integrations Work](https://docs.dualentry.com/developers/guides/integrations-and-tools/how-custom-integrations-work.md) for integration design guidance.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.