# Get the completion of each field

`GET /api/v1/vaults/{vault}/record-kinds/{recordKind}/statistics/fields` and `QUERY /api/v1/vaults/{vault}/record-kinds/{recordKind}/statistics/fields`

Say, for one template, how many records fill in each of its fields.

A record counts once per field, however many values it holds there. A field added in a later version is counted against the records on the versions that have it.

This endpoint answers GET and QUERY alike, and both return the same answer. GET sends the parameters in the address, QUERY in the request body. [How QUERY works](https://my.monicahq.com/docs/api/query-method).

## Parameters

- `vault` (uuid, required): The id of the vault.
- `recordKind` (string, required): contacts for people, or the id of an entity type, as GET /entity-types gives it.
- `template` (uuid, required): The id of the template, as the statistics give it.
- `version` (integer, optional): One version number. Leave it out to count every version.
- `order` (string, optional, defaults to lowest): lowest puts the least filled fields of each section first, form keeps the order of the form.

## Returns

The completion of each field. A vault, an entity type or a template of another account, and a template of another kind, answer 404.

- `data` (object): The completion of each field.
- `data.type` (string): Always field_completion.
- `data.attributes` (object): The figures.
- `data.attributes.template` (uuid): The id of the template counted.
- `data.attributes.version` (integer): The version counted. Null for every version.
- `data.attributes.records` (integer): How many records follow it, partial contacts left out.
- `data.attributes.sections` (array): The sections of the template, each with its fields. Empty when no record follows it.
- `data.attributes.sections[]` (object): One section.
- `data.attributes.sections[].name` (string): What the section is called.
- `data.attributes.sections[].fields` (array): Its fields, in the order asked for.
- `data.attributes.sections[].fields[]` (object): One field.
- `data.attributes.sections[].fields[].key` (string): The key of the field.
- `data.attributes.sections[].fields[].label` (string): What it is called.
- `data.attributes.sections[].fields[].filled` (integer): How many records hold at least one value in it. A value set aside does not count.
- `data.attributes.sections[].fields[].offered_to` (integer): How many records are on a version that has the field.
- `data.attributes.sections[].fields[].percentage` (integer): filled out of offered_to, rounded. Null when offered_to is 0.
- `data.attributes.sections[].fields[].available_from` (integer): The first version with the field, when some versions counted lack it. Null when every version has it.
- `data.attributes.hidden_count` (integer): How many sensitive fields are left out, because the key does not hold the sensitive fields scope.

## Status codes

| Code | When | Body |
| --- | --- | --- |
| `422` Unprocessable Content | With QUERY, a parameter was sent both in the address and in the body. | A message, and an errors object naming each parameter sent twice. |

## Rate limit

Sixty requests per minute, per user, shared by every key and device. Every answer carries the budget left in `X-RateLimit-Remaining`.

## Request

```sh
curl https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/statistics/fields?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e \
  -H "Authorization: Bearer $MONICA_TOKEN"
```

```javascript
const response = await fetch('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/statistics/fields?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e', {
  headers: { Authorization: `Bearer ${token}` },
})
const data = await response.json()
```

```php
$response = Http::withToken($token)->get('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/statistics/fields?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e');
```

## Request with QUERY

```sh
curl https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/statistics/fields?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e \
  -X QUERY \
  -H "Authorization: Bearer $MONICA_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
      "template": "8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e"
  }'
```

```javascript
const response = await fetch('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/statistics/fields?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e', {
  method: 'QUERY',
  headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    template: '8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e',
  }),
})
const data = await response.json()
```

```php
$response = Http::withToken($token)->query('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/statistics/fields?template=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e', [
    'template' => '8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e',
]);
```

## Response

`200`

```json
{
    "data": {
        "type": "field_completion",
        "attributes": {
            "template": "8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e",
            "version": null,
            "records": 14,
            "sections": [
                {
                    "name": "Identification",
                    "fields": [
                        {
                            "key": "microchip",
                            "label": "Microchip number",
                            "filled": 5,
                            "offered_to": 11,
                            "percentage": 45,
                            "available_from": 3
                        }
                    ]
                }
            ],
            "hidden_count": 0
        }
    }
}
```
