# apdu

> Lightweight library for constructing APDU commands for smart card communication (ISO 7816-4)

Latest version **0.1.1** (published 2025-12-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install apdu
pnpm add apdu
yarn add apdu
bun add apdu
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2025-12-28 |
| First published | 2014-09-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 10.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | tom@tomkp.com |
| Maintainers | tomkp |
| Keywords | apdu, smart-card, smartcard, iso-7816, nfc, pcsc, contactless |

## Links

- npm: https://www.npmjs.com/package/apdu
- Repository: https://github.com/tomkp/apdu
- Issues: https://github.com/tomkp/apdu/issues
- npm.io page: https://npm.io/package/apdu

## Recent versions

- 0.1.1 (latest) — 2025-12-28
- 0.1.0 — 2025-12-28
- 0.0.5 — 2021-02-15
- 0.0.3 — 2016-03-07
- 0.0.2 — 2016-03-07
- 0.0.1 — 2016-02-27
- 0.0.0 — 2014-09-23

## README

# apdu

Lightweight Node.js library for constructing APDU (Application Protocol Data Unit) commands for smart card communication (ISO 7816-4).

## Installation

```bash
npm install apdu
```

## Usage

```javascript
import Apdu from 'apdu';

// SELECT command example
const selectCommand = new Apdu({
  cla: 0x00, // Class byte
  ins: 0xa4, // Instruction: SELECT
  p1: 0x04, // P1: Select by DF name
  p2: 0x00, // P2: First or only occurrence
  data: [0xa0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10], // AID
  le: 0x00, // Expected response length
});

console.log(selectCommand.toString()); // "00a4040007a000000004101000"
console.log(selectCommand.toByteArray()); // Byte array
console.log(selectCommand.toBuffer()); // Node.js Buffer
```

## API

### `new Apdu(options)`

Creates a new APDU command.

| Option | Type     | Required | Description              |
| ------ | -------- | -------- | ------------------------ |
| `cla`  | number   | Yes      | Class byte (0x00-0xFF)   |
| `ins`  | number   | Yes      | Instruction byte         |
| `p1`   | number   | Yes      | Parameter 1              |
| `p2`   | number   | Yes      | Parameter 2              |
| `data` | number[] | No       | Command data bytes       |
| `le`   | number   | No       | Expected response length |

### Methods

#### `toString()`

Returns the APDU command as a lowercase hex string.

#### `toByteArray()`

Returns the APDU command as an array of bytes.

#### `toBuffer()`

Returns the APDU command as a Node.js Buffer.

## APDU Cases

The library automatically handles all four APDU cases defined in ISO 7816-4:

- **Case 1**: No data field, no response data expected (CLA INS P1 P2)
- **Case 2**: No data field, response data expected (CLA INS P1 P2 Le)
- **Case 3**: Data field present, no response data expected (CLA INS P1 P2 Lc Data)
- **Case 4**: Data field present, response data expected (CLA INS P1 P2 Lc Data Le)

## License

MIT

---
_Source: https://npm.io/package/apdu · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
