> ## Documentation Index
> Fetch the complete documentation index at: https://developers.ebioro.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Autenticación

> Cómo autenticar solicitudes API usando Digest Auth

La API de Ebioro Merchant usa **Autenticación HMAC Digest**. Cada solicitud debe incluir tres headers que prueban que posees las claves API.

## Headers requeridos

| Header               | Descripción                                     |
| -------------------- | ----------------------------------------------- |
| `X-Digest-Key`       | Tu clave API pública (`pk_...` o `pk_test_...`) |
| `X-Digest-Timestamp` | Timestamp Unix actual (segundos)                |
| `X-Digest-Signature` | Firma HMAC-SHA256 de la solicitud               |

## Calcular la firma

La firma es un hash HMAC-SHA256 de la concatenación de:

```
path + timestamp + method + body
```

Donde:

* `path` — la ruta de la solicitud incluyendo query string (ej., `/payments`)
* `timestamp` — el mismo valor que `X-Digest-Timestamp`
* `method` — método HTTP en mayúsculas (ej., `POST`, `GET`)
* `body` — el cuerpo JSON crudo para solicitudes POST, cadena vacía para GET

Firmado con tu `api_secret_key`.

## Ejemplos de código

<CodeGroup>
  ```javascript Node.js theme={null}
  const crypto = require('crypto');

  function buildAuthHeaders(publicKey, secretKey, path, method, body = '') {
    const timestamp = Math.floor(Date.now() / 1000).toString();
    const data = method !== 'GET' ? JSON.stringify(body) : '';
    const toSign = path + timestamp + method + data;
    const signature = crypto
      .createHmac('sha256', secretKey)
      .update(toSign)
      .digest('hex');

    return {
      'X-Digest-Key': publicKey,
      'X-Digest-Timestamp': timestamp,
      'X-Digest-Signature': signature,
      'Content-Type': 'application/json',
    };
  }
  ```

  ```python Python theme={null}
  import hmac
  import hashlib
  import time
  import json

  def build_auth_headers(public_key, secret_key, path, method, body=None):
      timestamp = str(int(time.time()))
      data = json.dumps(body) if body and method != 'GET' else ''
      to_sign = path + timestamp + method + data
      signature = hmac.new(
          secret_key.encode(),
          to_sign.encode(),
          hashlib.sha256
      ).hexdigest()

      return {
          'X-Digest-Key': public_key,
          'X-Digest-Timestamp': timestamp,
          'X-Digest-Signature': signature,
          'Content-Type': 'application/json',
      }
  ```

  ```php PHP theme={null}
  function buildAuthHeaders($publicKey, $secretKey, $path, $method, $body = '') {
      $timestamp = (string) time();
      $data = $method !== 'GET' ? json_encode($body) : '';
      $toSign = $path . $timestamp . $method . $data;
      $signature = hash_hmac('sha256', $toSign, $secretKey);

      return [
          'X-Digest-Key' => $publicKey,
          'X-Digest-Timestamp' => $timestamp,
          'X-Digest-Signature' => $signature,
          'Content-Type' => 'application/json',
      ];
  }
  ```
</CodeGroup>

## Validación del timestamp

El servidor rechaza solicitudes donde el timestamp tiene más de **5 minutos** de diferencia con el tiempo del servidor. Asegúrate de que el reloj de tu servidor esté sincronizado.

## Errores

| Código | Razón                                       |
| ------ | ------------------------------------------- |
| 401    | Valores de header faltantes                 |
| 401    | Clave pública inválida                      |
| 401    | Verificación de firma fallida               |
| 401    | La solicitud expiró (timestamp muy antiguo) |
