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

# Ejemplos de código

> Clientes mínimos para integraciones y documentos de cobro

## Cliente firmado para `/integrations/v1`

```javascript Node.js theme={null}
import crypto from 'node:crypto'

export async function cobrixIntegrationRequest({ baseUrl, apiKey, method, path, body }) {
  const rawBody = body === undefined ? '' : JSON.stringify(body)
  const timestamp = Math.floor(Date.now() / 1000).toString()
  const signedPayload = `${timestamp}.${method.toUpperCase()}.${path}.${rawBody}`
  const digest = crypto.createHmac('sha256', apiKey).update(signedPayload).digest('hex')

  return fetch(`${baseUrl}${path}`, {
    method,
    headers: {
      Authorization: `Bearer ${apiKey}`,
      'Content-Type': 'application/json',
      'Cobrix-Timestamp': timestamp,
      'Cobrix-Signature': `v1=${digest}`,
      ...(method.toUpperCase() === 'POST'
        ? { 'Idempotency-Key': crypto.randomUUID() }
        : {})
    },
    body: rawBody || undefined
  })
}
```

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

def cobrix_integration_request(base_url, api_key, method, path, body=None):
    raw_body = '' if body is None else json.dumps(body, separators=(',', ':'))
    timestamp = str(int(time.time()))
    signed = f'{timestamp}.{method.upper()}.{path}.{raw_body}'
    digest = hmac.new(api_key.encode(), signed.encode(), hashlib.sha256).hexdigest()
    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json',
        'Cobrix-Timestamp': timestamp,
        'Cobrix-Signature': f'v1={digest}',
    }
    if method.upper() == 'POST':
        headers['Idempotency-Key'] = str(uuid.uuid4())
    return requests.request(method, base_url + path, headers=headers, data=raw_body or None)
```

## Cliente para documentos de cobro

```javascript Node.js theme={null}
import crypto from 'node:crypto'

export async function createInvoice(baseUrl, apiKey, invoice) {
  return fetch(`${baseUrl}/v1/invoices`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${apiKey}`,
      'Content-Type': 'application/json',
      'x-idempotency': crypto.randomUUID()
    },
    body: JSON.stringify(invoice)
  })
}
```

```bash cURL theme={null}
curl --request GET \
  "https://sandbox-api.cobrix.co/api/v1/invoices?filter[status]=CREATED&sort=-created_at" \
  --header "Authorization: Bearer $COBRIX_API_KEY"
```

<Note>
  En shell, usa `curl --globoff` si tu versión interpreta los corchetes de `filter[status]` como un rango.
</Note>
