> For the complete documentation index, see [llms.txt](https://docs.facephi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/document-services/document-ocr.md). Agents can also search and read this documentation through its [MCP server](https://docs.facephi.com/~gitbook/mcp).

# Document OCR

Service that extracts fields from the identity document. Supports the front and, optionally, the back.

Assets are provided inline or by reference, depending on the value of `source`.

### Endpoint

```
POST /document/ocr
```

### Headers

| Name              | Type   | Required    | Description                                                                                                                                                                                     |
| ----------------- | ------ | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Authorization** | string | **Yes**     | Consumer token in the format `Bearer <token>`. See [Authentication](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/autenticacion.md).                                              |
| **consumer-id**   | string | **Yes**     | Consumer identifier.                                                                                                                                                                            |
| **operation-id**  | string | Conditional | Identifier of the operation to which the referenced assets belong. Required when `source` is `FILE_KEY`. See [Storage](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage.md). |

### Request body

**Content-Type:** `application/json`

#### Parameters

| Parameter        | Type    | Required | Description                                                                                                                                                                          |
| ---------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `source`         | string  | **Yes**  | How the assets are provided: `FILE_KEY` for keys of stored assets, `FILE` for Base64 content. See [Storage](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage.md). |
| `files`          | array   | **Yes**  | One or two entries: index `0` the front, index `1` the back. Asset keys or Base64 content, depending on `source`.                                                                    |
| `documentType`   | string  | **Yes**  | Document type: `ID_CARD`, `PASSPORT`, `DRIVING_LICENSE` or `FOREIGN_CARD`.                                                                                                           |
| `countryCode`    | string  | **Yes**  | Document issuing country, as an **ISO 3166-1 alpha-3**.                                                                                                                              |
| `isCropped`      | boolean | No       | Indicates that the images are already cropped. Defaults to `false`.                                                                                                                  |
| `forceDetection` | boolean | No       | Forces detection even if automatic document detection fails. Defaults to `false`.                                                                                                    |
| `retrieveImages` | boolean | No       | Returns the cropped images in the response. Defaults to `false`.                                                                                                                     |

`files` supports a maximum of two entries. Multipage forms are processed using [Form OCR](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/document-services/form-ocr.md).

#### Example Request

```json
{
  "source": "FILE_KEY",
  "files": [
    "0192a3f4-7b21-7c44-9e1a-3f5b8c2d1e04/TOKEN_FRONT_DOCUMENT",
    "0192a3f4-7b21-7c44-9e1a-3f5b8c2d1e04/TOKEN_BACK_DOCUMENT"
  ],
  "documentType": "ID_CARD",
  "countryCode": "ARG"
}
```

### Responses

#### `200` Success

#### Response Parameters

| Parameter              | Type    | Description                                                                         |
| ---------------------- | ------- | ----------------------------------------------------------------------------------- |
| `serviceResultCode`    | integer | Code indicating the overall result of the service execution. `0` indicates success. |
| `serviceResultLog`     | string  | Descriptive field for the result of the service execution.                          |
| `serviceTransactionId` | string  | Transaction identifier associated with the processed request.                       |
| `serviceTime`          | string  | Total processing time **(milliseconds)**.                                           |
| `timestamp`            | string  | Time when processing finished, in **ISO 8601**.                                     |
| `serviceResult`        | object  | OCR result containing the fields extracted from the document.                       |

#### Response example

```json
{
  "serviceResultCode": 0,
  "serviceResultLog": "Service executed ok",
  "serviceTime": "820",
  "serviceResult": {
    "side": "front",
    "type": "passport",
    "model": "ESP",
    "version": "1",
    "dictionary": {
      "LastName": "DOE",
      "FirstName": "JANE",
      "DocumentNumber": "PAL140268",
      "DateOfBirth": "10/07/1987"
    }
  }
}
```

#### Other responses

| Code  | Description                                                                                                                                                                                                                                                                                                                                                             |
| ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `400` | Required field missing or unsupported value; the `operation-id` header is missing when references are present; or a key is not in the form `{operationId}/{CONTEXTO}` or specifies a context that the slot does not allow.                                                                                                                                              |
| `403` | The consumer is not provisioned with the service `DOCUMENT_OCR`.                                                                                                                                                                                                                                                                                                        |
| `404` | The operation declared in `operation-id` does not exist or belongs to another consumer.                                                                                                                                                                                                                                                                                 |
| `410` | The operation declared in `operation-id` has expired.                                                                                                                                                                                                                                                                                                                   |
| `422` | A key names an operation different from the one declared in `operation-id`, or the context has no stored asset.                                                                                                                                                                                                                                                         |
| `429` | The consumer has exceeded its request rate limit, or a referenced asset has exhausted its invocation budget for this endpoint. In the first case, the response includes `Retry-After`, `X-RateLimit-Limit` and `X-RateLimit-Burst`, and waiting resolves the issue; in the second case, it does not. The underlying service is not invoked, and the call is not billed. |

The body of an error response has the form described in [Middleware API v2](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2.md#respuestas-de-error).
