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

# Store extracted fields as metadata on the file

> Store fields extracted from contracts and other documents as metadata on each file in Box, then filter them with the Box Query API or a no-code Box Apps view.

After you extract fields such as counterparties, renewal dates, and contract values from thousands of documents, store the results as metadata on each file in Box. The values stay attached to the document they came from, and you can search and filter them later with the Box Query API or in a no-code Box Apps view. Box Apps is available on Box Enterprise Advanced plans.

This guide uses a contract example. You create a metadata template, extract fields into it with Box AI, write the values to each file, and then filter the files by renewal date and contract value.

## Why store extracted fields as metadata

* You don't need a separate table that maps results to file IDs since you apply a metadata instance to a specific file.
* You can filter by date ranges and numeric thresholds as template fields can be `string`, `float`, `date`, `enum`, `multiSelect`, or `taxonomy`. See metadata fields.
* Access follows Box permissions. Collaborators who can view an item can view its metadata, and collaborators with upload permission can edit the values. See [Using metadata](https://docs.box.com/en/box-fundamentals/for-users/staying-organized/organizing-and-tracking-content/using-metadata) on Box Support.
* Box search indexes the values. People can also filter search results by template in the Box web app.
* Box AI fills the same template. When you pass a metadata template to Box AI, it returns values under the template's field keys.

## Before you start

You need:

* A Box app with the **Read and write all files and folders stored in Box** and **Manage AI** scopes. See Box AI API prerequisites.
* Admin permission to create the metadata template. Only admins, and co-admins with rights to create and edit metadata templates, can create templates.
* The ID of the folder that holds your documents.

## Create a metadata template for the fields

Use `date` for dates and `float` for amounts so you can filter by range. Use `enum` for fields with a fixed set of values.

```bash theme={null}
curl -X POST "https://api.box.com/2.0/metadata_templates/schema" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "scope": "enterprise",
    "templateKey": "contract",
    "displayName": "Contract",
    "fields": [
      {"type": "string", "key": "counterparty", "displayName": "Counterparty", "description": "The company that signed the contract with us"},
      {"type": "float", "key": "contractValue", "displayName": "Contract value", "description": "Total contract value in US dollars"},
      {"type": "date", "key": "renewalDate", "displayName": "Renewal date", "description": "The date the contract renews or expires"},
      {"type": "enum", "key": "autoRenews", "displayName": "Auto-renews", "description": "Whether the contract renews automatically", "options": [{"key": "Yes"}, {"key": "No"}]}
    ]
  }'
```

Box creates the template in your enterprise scope, `enterprise_<ENTERPRISE_ID>`. The query examples in this guide write it as `enterprise_${ENTERPRISE_ID}`. To find your scope, list your enterprise templates. Admins can also create templates in the Admin Console. See [Customizing metadata templates](https://docs.box.com/en/box-admin-tools/managing-content/metadata/customizing-metadata-templates).

## Extract the fields with the template

Call `POST /2.0/ai/extract_structured` with the template instead of a list of fields. The `answer` object in the response holds the values under the template's field keys. The response also includes `ai_agent_info`, `created_at`, and `completion_reason`. Box AI leaves out fields it can't find, and returns an empty `answer` when the file contains none of them.

```bash Request theme={null}
curl -X POST "https://api.box.com/2.0/ai/extract_structured" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d @- <<EOF
{
  "items": [{"id": "$FILE_ID", "type": "file"}],
  "metadata_template": {
    "template_key": "contract",
    "type": "metadata_template",
    "scope": "enterprise"
  }
}
EOF
```

```json Answer theme={null}
{
  "counterparty": "Acme Logistics LLC",
  "contractValue": 125000,
  "renewalDate": "2027-03-01T00:00:00Z",
  "autoRenews": "Yes"
}
```

To process thousands of files, call the endpoint once per file. A request with several files returns one combined answer. See Run extraction across thousands of documents and Best practices for the Extract Structured API.

> [!TIP]
>
> To extract and store fields without code, use a Custom Extract Agent in Box Extract. The agent processes files in its assigned folders and saves the values as metadata. Custom Extract Agents are available on Enterprise Advanced plans. See [Using Box Extract](https://docs.box.com/en/box-extract/using-box-extract).

## Write the values to the file

Send the `answer` object to `POST /2.0/files/:file_id/metadata/:scope/:template_key`. Box returns `201 Created` with the new metadata instance.

```bash theme={null}
curl -X POST "https://api.box.com/2.0/files/$FILE_ID/metadata/enterprise/contract" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "counterparty": "Acme Logistics LLC",
    "contractValue": 125000,
    "renewalDate": "2027-03-01T00:00:00Z",
    "autoRenews": "Yes"
  }'
```

Metadata `date` fields store RFC 3339 timestamps. Box AI returns dates in this format when you extract with a template, so you can write them without conversion.

> [accordion] Update a file that already has the metadata
>
> If the file already has an instance of the template, for example when you extract a file again, Box returns `409 Conflict`. Update the instance with `PUT /2.0/files/:file_id/metadata/:scope/:template_key` and a JSON Patch body. The `add` operation sets each value whether or not the field already has one.
>
> ```bash theme={null}
> curl -X PUT "https://api.box.com/2.0/files/$FILE_ID/metadata/enterprise/contract" \
>   -H "Authorization: Bearer $ACCESS_TOKEN" \
>   -H "Content-Type: application/json-patch+json" \
>   -d '[
>     {"op": "add", "path": "/counterparty", "value": "Acme Logistics LLC"},
>     {"op": "add", "path": "/contractValue", "value": 125000},
>     {"op": "add", "path": "/renewalDate", "value": "2027-03-01T00:00:00Z"},
>     {"op": "add", "path": "/autoRenews", "value": "Yes"}
>   ]'
> ```

For Python and TypeScript code that runs this create-or-update pattern on every upload, see Automate invoice intake with Box AI Extract.

## Filter the files with the Box Query API

Call the Box Query API to find files by their metadata values. This request finds contracts in a folder and its subfolders that renew in the first quarter of 2027 and are worth at least 100,000, sorted by renewal date.

```bash Request theme={null}
curl -X POST "https://api.box.com/2.0/query" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "box-version: 2026.0" \
  -H "Content-Type: application/json" \
  -d @- <<EOF
{
  "query": {
    "predicate": "enterprise_${ENTERPRISE_ID}:contract:renewalDate >= :start AND enterprise_${ENTERPRISE_ID}:contract:renewalDate < :end AND enterprise_${ENTERPRISE_ID}:contract:contractValue >= :minValue",
    "params": {
      "start": "2027-01-01T00:00:00Z",
      "end": "2027-04-01T00:00:00Z",
      "minValue": 100000
    },
    "ancestors": [{"id": "$FOLDER_ID", "type": "folder"}]
  },
  "order_by": [{"field_key": "enterprise_${ENTERPRISE_ID}:contract:renewalDate", "direction": "asc"}],
  "fields": [
    "box:item:name",
    "enterprise_${ENTERPRISE_ID}:contract:counterparty",
    "enterprise_${ENTERPRISE_ID}:contract:contractValue",
    "enterprise_${ENTERPRISE_ID}:contract:renewalDate"
  ],
  "limit": 50
}
EOF
```

```json Response theme={null}
{
  "entries": [
    {
      "type": "file",
      "id": "12346",
      "etag": "0",
      "box": {
        "item": {
          "name": "initech-services-contract.pdf"
        }
      },
      "enterprise_12345678": {
        "contract": {
          "$parent": "file_12346",
          "$scope": "enterprise_12345678",
          "$template": "contract",
          "$version": 0,
          "counterparty": "Initech Services Ltd.",
          "contractValue": 310500,
          "renewalDate": "2027-02-01T00:00:00.000Z"
        }
      }
    },
    {
      "type": "file",
      "id": "12345",
      "etag": "0",
      "box": {
        "item": {
          "name": "acme-logistics-msa.pdf"
        }
      },
      "enterprise_12345678": {
        "contract": {
          "$parent": "file_12345",
          "$scope": "enterprise_12345678",
          "$template": "contract",
          "$version": 0,
          "counterparty": "Acme Logistics LLC",
          "contractValue": 125000,
          "renewalDate": "2027-03-01T00:00:00.000Z"
        }
      }
    }
  ],
  "limit": 50,
  "next_marker": null
}
```

Each entry includes the file `id`, which you can use to download the file or show it in Content Preview.

* In `predicate`, reference template fields as `<scope>:<templateKey>:<fieldKey>` and item fields as `box:item:<field>`. For operators, see Query syntax.
* Each value in `params` must match its field type. Use a number for a `float` field and an RFC 3339 timestamp for a `date` field.
* `limit` can be up to 100. When `next_marker` isn't `null`, pass it as `marker` in the next request and keep the other parameters the same.

Matches on `string` fields are case sensitive. Use `ILIKE` to ignore case, and add `%` wildcards to match part of a value. Values for `enum` fields must match an option key exactly. To find files where Box AI didn't return a value, use `EXISTS(:template) AND enterprise_${ENTERPRISE_ID}:contract:contractValue IS NULL` with `"template": "enterprise_${ENTERPRISE_ID}:contract"` in `params`.

### Get totals with the Box Query Insights API

To get totals instead of a list of files, send a predicate to `POST /2.0/query_insights`. This request sums the value of contracts that renew in the first quarter of 2027.

```bash Request theme={null}
curl -X POST "https://api.box.com/2.0/query_insights" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "box-version: 2026.0" \
  -H "Content-Type: application/json" \
  -d @- <<EOF
{
  "query": {
    "predicate": "enterprise_${ENTERPRISE_ID}:contract:renewalDate >= :start AND enterprise_${ENTERPRISE_ID}:contract:renewalDate < :end",
    "params": {
      "start": "2027-01-01T00:00:00Z",
      "end": "2027-04-01T00:00:00Z"
    },
    "ancestors": [{"id": "$FOLDER_ID", "type": "folder"}]
  },
  "metrics": {
    "renewingValue": {"type": "sum", "field": "enterprise_${ENTERPRISE_ID}:contract:contractValue"}
  }
}
EOF
```

```json Response theme={null}
{
  "insights": [
    {
      "key": [],
      "type": "overall",
      "metrics": {
        "renewingValue": {
          "type": "sum",
          "values": {
            "sum": 435500.0
          }
        }
      }
    }
  ]
}
```

For totals per group, such as per contract type, see Build a contract analytics dashboard with Box Query Insights API.

### Choose an API for finding files by metadata

| API | Use it to |
| - | - |
| Box Query API, `POST /2.0/query` | Filter and sort files by fields from one or more templates and by item fields. |
| Box Query Insights API, `POST /2.0/query_insights` | Get sums, averages, minimums, maximums, and counts instead of file lists. |
| Metadata Query API, `POST /2.0/metadata_queries/execute_read` | Configure the Content Explorer metadata view, which takes a query in this format. This API queries one template at a time. |
| Search API, `GET /2.0/search` with `mdfilters` | Run a keyword search and filter the results by one metadata template. |

## Give your team a filterable view in Box Apps

If the people who work with the contracts don't write code, build a Box App on the same template. Box Apps is a no-code builder for apps based on metadata. A View block is a saved search based on one template, and the template's fields become the view's filters and columns. Box Apps is available on Box Enterprise Advanced plans.

1. A Box Admin enables Box Apps in **Admin Console** > **Enterprise Settings** > **Content & Sharing**, and chooses who can use Box Apps and who can manage apps. See [Enabling Box Apps](https://docs.box.com/en/box-apps/box-apps-for-box-admins/enabling-box-apps).
2. In Box Apps, select **New App** and name the app.
3. Add a **View** block, enter a view name, and choose the template, for example **Contract**. You can't change the template after you create the view. See [Creating an app](https://docs.box.com/en/box-apps/using-box-apps/apps/creating-an-app).
4. Open the view and select **Edit**. On the **Filters** tab, choose the fields people can filter by, such as **Renewal date** and **Contract value**. On the **Columns** tab, choose the columns to show, then select **Save**. See [Configuring View in Box Apps](https://docs.box.com/en/box-apps/using-box-apps/configuring-view-in-box-apps).
5. Share the app with your team. People with Viewer access can't change the app.

In the view, people can sort by columns that support sorting and export the metadata to a CSV file of up to 100,000 items. If an admin enables Box AI for files, they can also filter the results with a Box AI prompt. They can also correct values Box AI got wrong. Inline editing works when the app allows it, and Box Apps Managers can edit several files at once. See [Managing metadata in Box Apps](https://docs.box.com/en/box-apps/using-box-apps/managing-metadata-in-box-apps).

To show a total, such as the combined value of the contracts in a view, add a **Metric** block. It calculates the sum, average, maximum, minimum, or item count of a numeric field for the items in a linked view.

App permissions control what people can do with the app. The files and metadata they see in it respect the permissions they already have in Box. See [Box Apps permissions](https://docs.box.com/en/box-apps/box-apps-for-box-admins/box-apps-permissions).

## Embed a metadata view in your own app

To show a filterable list of files and their metadata in your own web app, use the Content Explorer metadata view. It takes a Metadata Query API definition and gives each template field filter and edit controls for its type. See Build a document dashboard with the Content Explorer metadata view.

## Limits to plan for

* The Box Query API returns `403 Forbidden` when a query matches too many items, or too many items the user can't access. Narrow the query with `ancestors` and a more specific predicate.
* The Box Query API returns `429 Too Many Requests` when your enterprise exceeds the allowed request rate. Wait and retry. See Rate limits.

**RELATED GUIDES**
