> 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/storage.md).

# Storage

Storage saves the captures for an [operation](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/operations.md) before validating them. Each asset is stored once, associated with a **context** within the operation, and from then on is referenced by its key in validation services without transferring the binary again. The complete journey, from the token to the validation call, is described in [Common Flow](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/flujo-comun.md).

#### Asset modes

Validation services receive assets in two ways, using the same request structure. The mode is specified by the `source`:

| Value of `source` | Asset slot contents                           |
| ----------------- | --------------------------------------------- |
| `FILE`            | Capture content, encoded in Base64.           |
| `FILE_KEY`        | Key of an asset that has already been stored. |

`source` is unique per request, not per asset: a request cannot combine inline content and references. When the request references any asset by key, the `operation-id` header is required and specifies the operation to which all the keys belong.

{% hint style="info" %}
**Referencing assets by key is the recommended approach.** Identity validation services also accept the tokenized asset as Base64 in the call itself (`source: FILE`), but this is not the recommended way to integrate with them: the full content is sent with every validation, it is not reused across calls or services, it makes the request larger and closer to the permitted size limit, and the capture is left without the operation that provides traceability. Store each asset once and then reference it by its key (`source: FILE_KEY`).

Services that only accept inline content, listed below under [Exceptions](#excepciones), are outside this recommendation: there is no other way to send the asset to them.
{% endhint %}

#### Asset contexts

The **context** identifies which capture the asset represents and determines which slots in which services can use it. Each service specifies, in the description of its parameters, the context accepted in each slot.

| Context                | Content                                                  | Format of `asset.file`                                       |
| ---------------------- | -------------------------------------------------------- | ------------------------------------------------------------ |
| `TOKEN_FRONT_DOCUMENT` | **RAW token** of the front of the identity document.     | Token of the buffer generated by the capture SDK, in Base64. |
| `TOKEN_BACK_DOCUMENT`  | **RAW token** of the back of the identity document.      | Token of the buffer generated by the capture SDK, in Base64. |
| `TOKEN_FACE_IMAGE`     | Cropped face from the identity document.                 | Token of the buffer generated by the capture SDK, in Base64. |
| `TOKEN_BEST_IMAGE`     | Best image from the selfie capture.                      | Token of the buffer generated by the capture SDK, in Base64. |
| `TOKEN_BIN_IAD`        | Binary data from the injection attack detection capture. | Binary data in Base64.                                       |
| `RAW_IMAGE`            | Un-tokenized, unencrypted face image.                    | Unencrypted JPEG or PNG, in Base64.                          |
| `TOKEN_RAW_IMAGE`      | Token of a face image uploaded unencrypted.              | Not supported on upload: obtained by uploading `RAW_IMAGE`.  |

The (operation, context) pair uniquely identifies an asset: a second upload with the same context in the same operation returns `409`. To repeat a capture that has already been stored, create a new operation.

`RAW_IMAGE` names the content as received, not as stored, and is the only context with this asymmetry. The unencrypted image is tokenized upon receipt and is not stored: the operation retains the token, and the key returned by [Save Asset](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage/save-asset.md) uses the context `TOKEN_RAW_IMAGE`, which is then referenced and is the only way to read it back. Declaring `TOKEN_RAW_IMAGE` on upload is rejected with `400`. It is intended for the face provided by a civil registry, and is referenced as `sourceImage` in [Facial Matching](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/biometric-services/facial-matching.md).

#### Key format

```
{operationId}/{CONTEXTO}

0192a3f4-7b21-7c44-9e1a-3f5b8c2d1e04/TOKEN_FRONT_DOCUMENT
```

This is the value returned by [Save Asset](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage/save-asset.md) in `fileKey` and the value returned by [Get File Keys](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/operations/get-file-keys.md) in each entry of `fileKeys`.

{% hint style="warning" %}
The key is forwarded exactly as received. Do not construct or interpret it: adding the context duplicates it and the asset cannot be resolved.
{% endhint %}

#### Asset usage limits

A capture is stored only once. When it is uploaded, its content is checked: if the same content is already stored, the upload returns `409` and nothing is saved, even if a different operation or context is specified. Therefore, you cannot upload the same capture again to validate it another time.

A stored asset also has a limit on the number of invocations per endpoint. Exceeding it returns `429`. The limit applies independently to each asset and endpoint, so using up the limit for one endpoint does not affect another; its value is agreed upon when the service is set up.

| Situation                                                                | Response |
| ------------------------------------------------------------------------ | -------- |
| The content sent to `POST /storage` is already stored                    | `409`    |
| An asset has reached the maximum number of invocations for that endpoint | `429`    |

An invocation is counted when the asset is read from storage, which happens before the underlying service is invoked. It is counted regardless of how the call ends: a negative result, an error from the underlying service, or a subsequent failure all consume the invocation.

#### Exceptions

Identity verification services support both asset modes by default: inline content and reference by key. These are the exceptions:

| Service                                                                                                                                                                                                                                           | Supported modes                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
| [Form OCR](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/document-services/form-ocr.md)                                                                                                                                             | Only `FILE`                               |
| [Voice Enrollment](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/voice-services/voice-enrollment.md) and [Voice Authentication](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/voice-services/voice-authentication.md) | Inline content only, without the `source` |
| [Document Validation](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/document-services/document-validation/iniciar-validacion.md)                                                                                                    | Reference only, no field `source`         |

For online-only services, the content is sent in the call itself and they do not have a header `operation-id`.

#### Available services

* [**Save Asset**](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage/save-asset.md): Stores an asset in the context of an operation and returns its key.
