The `crdca` package is the Python client and the same surface as the CLI. It has four functions with the
same names as the subcommands, no fifth function, and the private key stays in your process: you generate
it or receive it from your custody, and no result includes it.

```bash
pip install crdca
```

It requires Python 3.11 or later and `openssl` on the system.

## The four functions
| Function | What it does |
| --- | --- |
| `crdca.enroll(card_url, *, clave=None, nueva_clave=False, perfil="agentcard", servidor=None, publicar_token=None, timeout=15.0)` | opens the ACME account, creates or takes the key and requests the order |
| `crdca.sign(tarjeta, *, cadena, clave, inclusion=None, log=None)` | signs the card with a detached JWS over JCS |
| `crdca.renew(*, cert, clave, servidor=None, ari=True, ahora=None, timeout=15.0)` | queries ARI and renews at half life |
| `crdca.instance(*, card_url, cadena, clave, replicas=1, vida_s=86400, ahora=None)` | issues and verifies one JWT per replica |

`clave` accepts PEM bytes or a path; `cadena` accepts bytes or text. `inclusion` and `log` are
alternatives: either you carry the base64url proof or you give the log URL so the SDK can request it.

`crdca.verificar_instancia(token, *, cadena, card_url, replica="r1", ahora=None)` is the helper that
checks a credential against the class key and chain. It is not a fifth command.

## A complete example
The example assumes you already published the card at its URL and that you have the `card-01` challenge
token. Save the key in `clave.pem` and the card in `agent-card.json`:

```python
import json
from pathlib import Path

import crdca
from crdca.sdk import claves

CARD_URL = "https://example.com/agents/invoices/agent-card.json"
SERVIDOR = "https://acme.crdca.org/directory"

clave = Path("clave.pem").read_bytes()
tarjeta = json.loads(Path("agent-card.json").read_text())

# 1. enrollment: account, order, card-01 challenge and issued chain
alta = crdca.enroll(
    CARD_URL,
    clave=clave,
    servidor=SERVIDOR,
    publicar_token="CHALLENGE-TOKEN",
)
if not alta.certificado:
    raise SystemExit("the challenge did not complete: check the card and the token")
print("order", alta.orden["url"], "key", alta.clave_origen)
Path("cadena.pem").write_bytes(alta.certificado)

# 2. sign with the proof provided by the log
firmada = crdca.sign(
    tarjeta,
    cadena=Path("cadena.pem").read_bytes(),
    clave=clave,
    log="https://cdn.crdca.org",
)
Path("agent-card.firmada.json").write_text(
    json.dumps(firmada, indent=2, ensure_ascii=False)
)

# 3. one credential per replica, verified before being returned
lote = crdca.instance(
    card_url=CARD_URL,
    cadena=Path("cadena.pem").read_bytes(),
    clave=clave,
    replicas=2,
)
for credencial in lote.credenciales:
    print(credencial["replica"], credencial["sub"], credencial["exp"])
```

The signature is not computed over the card with `signatures`: the payload is `JCS(card without
signatures)` and the challenge extension is removed before signing. The inclusion proof travels in
`crdca_inclusion` inside the protected header, never in the payload.

## What each function returns

| Result | Fields |
| --- | --- |
| `ResultadoAlta` | `card_url`, `perfil`, `cuenta`, `orden`, `csr`, `clave_publica`, `clave_origen`, `key_authorization`, `certificado` |
| `ResultadoRenovacion` | `cert_id`, `card_url`, `edad_s`, `vida_s`, `media_vida_s`, `ventana`, `retry_after_s`, `decision`, `orden` |
| `ResultadoInstancia` | `card_url`, `replicas`, `vida_s`, `credenciales`, `verificadas` |
| signed card | the input object plus `signatures` with `protected` and `signature` |

Each instance credential carries `replica`, `sub`, `iat`, `exp`, `jti`, `jwt` and `cabecera`. The header
is `{alg, kid, x5t#S256}` and has no `x5c`: the class chain travels by reference.

## Renew at half life

```python
import crdca

resultado = crdca.renew(
    cert=open("hoja.pem", "rb").read(),
    clave=open("clave.pem", "rb").read(),
    servidor="https://acme.crdca.org/directory",
)
print(resultado.decision, resultado.edad_s, resultado.media_vida_s)
if resultado.orden:
    print("replacement order", resultado.orden["url"])
```

`decision` is `espera` before half life and `renueva` after it. The replacement order carries `replaces`
with the `cert_id` of the certificate it supersedes; an order is requested only when it is time.

## Keys and errors

- `claves.generar_clave()` returns a new Ed25519 key in memory.
- `claves.cargar_clave(material)` validates an Ed25519 PKCS#8 PEM from the operator's custody.
- `claves.clave_publica_pem(clave)` returns the public key for publication or comparison.
- Failures arrive as `crdca.ErrorCRDCA` with `codigo` and `mensaje`; the codes are the CLI codes
  (`E_USO`, `E_CLAVE`, `E_CADENA`, `E_INCLUSION`, `E_INSTANCIA`, and so on).

::: nota Ephemeral key
With `nueva_clave=True` the key is generated for that call and is not returned. If you need to sign in
another invocation, generate the key with `claves.generar_clave()` or load it from your KMS and pass it
in `clave=`.
:::

## Next step

- [The CLI](/en/use/cli) if you prefer the terminal.
- [TypeScript](/en/use/typescript) and [Go](/en/use/go) for the other surfaces.
- [Verify a card](/en/use/verify) for the receiving side of the call.
