> 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/identity-api/identity-api-reference/onboarding/ocr/ocr-document.md).

# Document Reader

This service is used to read information from a document image using **FacePhi OCR technology** and organize the information into fields.

The service can handle different document types, including identity cards, foreign resident cards, invoices, and PDFs. You must specify the **document type** and the **model** to process the document correctly.

For a complete list of supported document types and models, see [Documents Supported by OCR](#documentos-soportados-por-ocr).

### Documents Supported by OCR

#### Document Types

The OCR service can process different document types. Use the `type` parameter to specify the document type to process:

| Type                  | Value          | Description                                |
| --------------------- | -------------- | ------------------------------------------ |
| Identity Card         | `id_card`      | National identity cards                    |
| Foreign Resident Card | `foreign_card` | Identification cards for foreign nationals |
| PDF                   | `pdf`          | PDF documents that require OCR processing  |
| Invoice               | `invoice`      | Billing documents                          |

#### Supported Models by Document Type

*Identity Cards (ID Cards)*

For identity cards, use the country code **ISO 3166-1 alpha-3** as the `model`:

| Country   | ISO Code |
| --------- | -------- |
| Argentina | `ARG`    |
| Mexico    | `MEX`    |

*Foreign Resident Cards (Foreign Cards)*

For foreign resident cards, use the following codes as the `model`:

| Country | ISO Code |
| ------- | -------- |
| Mexico  | `MEX`    |

*Invoices*

For invoices, use the provider code as the `model`:

| Provider | Code     |
| -------- | -------- |
| TELMEX   | `TELMEX` |
| CFE      | `CFE`    |

*PDF Documents*

For PDF documents, use the document type code as the parameter `model`:

| Document type           | Code    |
| ----------------------- | ------- |
| VAT: El Salvador        | `IVA`   |
| INCOME TAX: El Salvador | `RENTA` |

### Endpoint

```
POST /ocr
```

### Headers

| Name          | Type   | Required | Description                                                |
| ------------- | ------ | -------- | ---------------------------------------------------------- |
| **x-api-key** | string | **Yes**  | Authorization API key for access.                          |
| **family**    | string | No       | Value: **OnBoarding**. Required with the Tracking service. |

{% hint style="info" %}
All calls to the Tracking Endpoints with **Identity Platform** must include the `family`.
{% endhint %}

### Request body

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

#### Parameters

| Parameter              | Type      | Required | Description                                                                                                                                                                                  |
| ---------------------- | --------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type`                 | string    | **Yes**  | Document type to process. Only types allowed in the tenant configuration can be used. Possible values: `id_card`, `passport`, `driver_license`, `foreign_card`, `invoice`, `pdf`             |
| `model`                | string    | **Yes**  | Identifier of the document model to process. For identity documents, use the country code **ISO 3166-1 alpha-3**. For invoices or PDF documents, use the specific codes provided by FacePhi. |
| `files`                | string\[] | **Yes**  | Document images encoded in **Base64** (RFC4648). **Maximum of two images** per request. The first image is treated as the front side.                                                        |
| `isCropped`            | boolean   | No       | Indicates whether the provided images are already cropped. **Default value:** `false`                                                                                                        |
| `forceDetection`       | boolean   | No       | Forces OCR detection even if automatic document detection fails. **Default value:** `false`                                                                                                  |
| `retrieveImages`       | boolean   | No       | Returns cropped images in the response when supported for the configured tenant. **Default value:** `false`                                                                                  |
| `tracking`             | object    | No       | Object representing the required tracking information.                                                                                                                                       |
| `tracking.extraData`   | string    | No       | Token generated by the SDK Mobile/Web. Contains tracking information tokenized with the Platform.                                                                                            |
| `tracking.operationId` | string    | No       | Operation identifier generated by the SDK Mobile/Web.                                                                                                                                        |

#### Example Request

```json
{
  "type": "pdf",
  "model": "IVA",
  "files": ["JVBERi0xLjQKJeLjz9MKNSAwIG9iago8P..."],
  "tracking": {
    "extraData": "BQABAQG2gBNjuHN4kLmPqYf7R...",
    "operationId": "123e4567-e89b-12d3-a456-426614174000"
  }
}
```

{% hint style="warning" %}
**Maximum request size: 6 MB.** Base64 images (`files`, maximum of two) count together toward this limit, with \~33% overhead compared to the original file (maximum of \~4.5 MB total for the original files). Compressed JPEG images of 1–2 MB each at most are recommended. If the limit is exceeded, the gateway responds `413` without processing the request. See [Request size limit](https://docs.facephi.com/pages/bc7bee9358079d52bafa44ad281828eb424e96fe#l%C3%ADmite-de-tama%C3%B1o-de-la-solicitud).
{% endhint %}

### Responses

#### `200` Success

#### Response Parameters

| Parameter              | Type    | Description                                                              |
| ---------------------- | ------- | ------------------------------------------------------------------------ |
| `serviceResultCode`    | integer | Code indicating the **overall result** of the service execution.         |
| `serviceResultLog`     | string  | Descriptive field for the result of the service execution.               |
| `timestamp`            | string  | Time when the service finished processing.                               |
| `serviceTransactionId` | string  | Transaction identifier associated with the request processed by the API. |
| `serviceResult`        | object  | Object containing the OCR result with the **extracted fields**.          |
| `serviceTime`          | string  | Total service execution time **(milliseconds)**.                         |

#### Service Result Code

The `serviceResultCode` indicates the overall result of the service execution:

| serviceResultCode | Description                                                                       | HTTP Code |
| ----------------- | --------------------------------------------------------------------------------- | --------- |
| 0                 | The service execution was successful; the module processed the request correctly. | 200       |

#### Response example

```json
{
  "serviceResultCode": 200,
  "serviceResultLog": "Service executed ok",
  "timestamp": "2024-07-12T09:43:36Z",
  "serviceTransactionId": "99999999-9999-9999-9999-999999999999",
  "serviceResult": {
    "dictionary": {
      "CommercialName": "Example Commercial Name",
      "DeclarationNumber": "11111111111",
      "EconomicActivity": "National Transportation",
      "NIT": "9999-999999-999-9",
      "NRC": "999999-9",
      "Name": "John Doe",
      "TaxedPeriod": "04 2021"
    },
    "model": "IVA",
    "side": "front",
    "type": "pdf",
    "version": "2021"
  },
  "serviceTime": "638"
}
```

#### `400` Bad Request

```json
{
  "status": 400,
  "title": "Bad Request",
  "detail": "Invalid request.",
  "type": "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
  "errors": []
}
```

#### `401` Unauthorized

```json
{
  "message": "Unauthorized"
}
```

#### `403` Forbidden

```json
{
  "Message": "User is not authorized to access this resource with an explicit deny"
}
```

#### `413` Payload Too Large

The request exceeds the maximum allowed size. The gateway rejects it **without processing it**: the response does not include `serviceResultCode` or `serviceTransactionId`. See [Request size limit](https://docs.facephi.com/pages/bc7bee9358079d52bafa44ad281828eb424e96fe#l%C3%ADmite-de-tama%C3%B1o-de-la-solicitud).

Request larger than 6 MB and up to 10 MB (`Content-Type: application/json`):

```json
{
  "message": "Request Too Long"
}
```

Request larger than 10 MB (10,485,760 bytes) (`Content-Type: text; charset=utf-8`, plain text, not JSON):

```
HTTP content length exceeded 10485760 bytes.
```

#### `502` Bad Gateway

```json
{
  "status": 502,
  "title": "Bad Gateway",
  "detail": "Server got an invalid response.",
  "type": "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502"
}
```

#### `504` Gateway Timeout

```json
{
  "message": "Endpoint request timed out"
}
```
