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

# Common flow

Validation services that work **by reference** share the same setup: obtaining a token, creating an operation, and uploading assets to storage. Only then is the specific service called with the keys obtained.

This page covers that setup so it doesn't have to be repeated for each service. Each step links to the page documenting its contract.

### Diagram

```mermaid
sequenceDiagram
    actor C as Consumer
    participant API as Middleware API v2

    C->>API: POST /consumer/token
    API-->>C: consumer token (valid for 1 hour)

    C->>API: POST /operation
    API-->>C: operationId + expiresAt

    loop For each asset: front, back, selfie
        C->>API: POST /storage (content + context)
        API-->>C: fileKey
    end

    C->>API: Validation service (fileKeys + operation-id)
    API-->>C: Service result
```

### The steps

1. **Obtain the consumer token.** A single token authenticates all subsequent calls and is valid for one hour. It is issued on behalf of the platform and for a specific consumer. See [Authentication](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/autenticacion.md).
2. **Create the operation.** The operation groups the session's assets and sets their expiration. It is created with `POST /operation`, which returns the `operationId` and its `expiresAt`. It's best to create it **when capture starts**, not earlier, because the expiration clock starts at that point. See [Create Operation](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/operations/create-operation.md).
3. **Upload each asset.** One call per capture, specifying the **context** that applies to it. Each upload returns the `fileKey` used to reference the asset later. See [Save Asset](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage/save-asset.md), and [Storage](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/storage.md) for the available contexts and key format.
4. **Call the validation service.** Send the `fileKeys` **as returned by** `POST /storage`, with the `operationId` in the header `operation-id`. The key is not composed or interpreted: adding context to it yourself duplicates it, and the asset cannot be resolved. If you need to retrieve the keys for an operation, they are in [Get File Keys](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/operations/get-file-keys.md).

### Which services follow this flow

Identity validation **services** accept both asset modes by default: inline content and key reference. This flow is for the key-reference mode. The exceptions are explicit:

| Service                                                                                                                                                                                                                                           | Supported modes                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
| [Form OCR](https://docs.facephi.com/docs.facephi-en/rest-api/midapi-v2/document-services/form-ocr.md)                                                                                                                                             | Inline content only                       |
| [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.md)                                                                                                                       | Key reference only, without the `source`  |

Services that support only inline content skip steps 2 and 3: the content is sent in the request itself, and they do not include an `operation-id`.
