`@nexgensys/crdca-sdk` is the Node client. It has the same four-operation grammar as the CLI and the
Python SDK, is written in modern JavaScript with ESM modules and declares no dependencies. There is no
build step: the modules are the package's `.js` files.

```bash
npm i @nexgensys/crdca-sdk
```

It needs Node 22 or later. The package declares `"type": "module"`; use `import`, not `require`.

## The surface
| Export | What it does |
| --- | --- |
| `enroll(cardUrl, {clave, nuevaClave, perfil, servidor, publicarToken, timeoutMs})` | ACME account, key and order; sends the CSR in `finalize` |
| `sign(tarjeta, {cadena, clave, inclusion, log, timeoutMs})` | detached JWS over JCS with the inclusion in the header |
| `renew({cert, clave, servidor, ari, ahora, timeoutMs})` | queries ARI and renews at half life with `replaces` |
| `instance({cardUrl, cadena, clave, replicas, vidaS, ahora})` | one JWT per replica, verified before being returned |
| `verificarInstancia(token, {cadena, cardUrl, replica, ahora})` | checks a credential against the class key and chain |

`enroll` and `renew` are asynchronous because they talk to the ACME directory. `sign` returns a promise
and only uses the network when you request the proof from the log. `instance` is synchronous.

## Card enrollment
```typescript
import { readFileSync } from 'node:fs';
import { enroll } from '@nexgensys/crdca-sdk';

const CARD_URL = 'https://example.com/agents/invoices/agent-card.json';
const alta = await enroll(CARD_URL, {
  clave: readFileSync('clave.pem', 'utf8'),
  servidor: 'https://acme.crdca.org/directory',
  publicarToken: 'CHALLENGE-TOKEN',
});
console.log(alta.ordenEstado, alta.finalizado, alta.claveOrigen);
console.log(alta.keyAuthorization);
```

`publicarToken` publishes `<token>.<fingerprint>` in the card's `card-01` extension. Without that option,
enrollment stops at the order. `nuevaClave: true` generates an ephemeral key that is not returned; to
sign later, pass your key in `clave`.

The result uses camelCase names: `cardUrl`, `perfil`, `cuenta`, `orden`, `ordenEstado`, `finalizado`,
`claveOrigen`, `csr`, `keyAuthorization` and `tarjeta`.

## A complete example
This script signs the card, issues two credentials and checks the first one. It assumes you already have
`clave.pem`, `cadena.pem` and `agent-card.json` from enrollment:
```typescript
import { readFileSync, writeFileSync } from 'node:fs';
import {
  instance,
  sign,
  verificarInstancia,
} from '@nexgensys/crdca-sdk';

const CARD_URL = 'https://example.com/agents/invoices/agent-card.json';
const clave = readFileSync('clave.pem', 'utf8');
const cadena = readFileSync('cadena.pem', 'utf8');
const tarjeta = JSON.parse(readFileSync('agent-card.json', 'utf8'));

const firmada = await sign(tarjeta, {
  cadena,
  clave,
  log: 'https://cdn.crdca.org',
});
writeFileSync('agent-card.firmada.json', JSON.stringify(firmada, null, 2));
console.log('signatures', firmada.signatures.length);

const lote = instance({
  cardUrl: CARD_URL,
  cadena,
  clave,
  replicas: 2,
});
console.log('verified', lote.verificadas);
for (const credencial of lote.credenciales) {
  console.log(credencial.replica, credencial.sub, credencial.exp);
}

const comprobada = verificarInstancia(lote.credenciales[0].jwt, {
  cadena,
  cardUrl: CARD_URL,
  replica: 'r1',
});
console.log(comprobada.reclamos.sub);
```

Save the script as `ejemplo.mjs` and run it with `node ejemplo.mjs`. `sign` returns the signed card, not
an envelope: the signature stays in `signatures`.

## What it returns
| Result | Shape |
| --- | --- |
| `enroll` | `{cardUrl, perfil, cuenta, orden, ordenEstado, finalizado, claveOrigen, csr, keyAuthorization?, tarjeta?}` |
| `sign` | the input card plus `signatures: [{protected, signature}]` |
| `renew` | `{certId, cardUrl, edadS, vidaS, mediaVidaS, ventana, retryAfterS, decision, orden, replaces}` |
| `instance` | `{cardUrl, replicas, vidaS, verificadas, credenciales}` |
| `verificarInstancia` | `{cabecera, reclamos}` |

Each credential carries `replica`, `sub`, `iat`, `exp`, `jti`, `jwt` and `cabecera`. The header is
`{alg, kid, x5t#S256}`, with no `x5c`.

## Renewal and errors

```typescript
import { renew } from '@nexgensys/crdca-sdk';

const decision = await renew({
  cert: readFileSync('hoja.pem', 'utf8'),
  clave: readFileSync('clave.pem', 'utf8'),
  servidor: 'https://acme.crdca.org/directory',
});
console.log(decision.decision, decision.mediaVidaS, decision.replaces);
```

Failures are thrown as `ErrorCRDCA` with `codigo` and `mensaje`. The codes are the contract codes:
`E_USO`, `E_ENTRADA`, `E_CLAVE`, `E_CADENA`, `E_INCLUSION`, `E_SERVIDOR`, `E_INSTANCIA`.

::: nota No tsc
The package ships as ESM JavaScript with JSDoc annotations. You do not need TypeScript or a build step
to use it, and you do not need one to read its types in an editor that understands JSDoc.
:::

## Next step

- [Verify a card](/en/use/verify), including the TypeScript A2A plugin.
- [The human mandate](/en/use/mandate) for the `CRDCA-Mandate` header.
- [Python](/en/use/python) and [Go](/en/use/go) for the other surfaces.
