|
| 1 | +# @kyalabs/badge-sdk |
| 2 | + |
| 3 | +Badge identity primitive for AI agents. Framework-agnostic — no MCP dependency. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @kyalabs/badge-sdk |
| 9 | +``` |
| 10 | + |
| 11 | +Requires Node.js >= 20. |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { Badge } from '@kyalabs/badge-sdk' |
| 17 | + |
| 18 | +const badge = await Badge.init() |
| 19 | + |
| 20 | +// Inject identity into outgoing requests |
| 21 | +const headers = badge.headers() |
| 22 | +// { "Kya-Token": "gp_v1_..." } |
| 23 | + |
| 24 | +// Check identity state |
| 25 | +badge.identityType // "guest" | "verified" | "offline" |
| 26 | +badge.isGuest // true for guest/offline |
| 27 | +badge.installId // persistent UUID (stored in ~/.kya/) |
| 28 | + |
| 29 | +// Clean up |
| 30 | +badge.destroy() |
| 31 | +``` |
| 32 | + |
| 33 | +## How It Works |
| 34 | + |
| 35 | +On first run, `Badge.init()` issues a guest pass from the kya API ("SSN on birth"). The token and install ID are cached to `~/.kya/` so they persist across process restarts. |
| 36 | + |
| 37 | +Agents carry their guest pass in the `Kya-Token` HTTP header. Merchants verify tokens via the [VerifAi API](https://www.kyalabs.io/docs). Guest passes can upgrade to verified badges through device auth or merchant enrollment. |
| 38 | + |
| 39 | +### Identity Lifecycle |
| 40 | + |
| 41 | +``` |
| 42 | +Badge.init() → guest pass (gp_v1_*) → enroll at merchant → badge token (kya_*) |
| 43 | + ↑ ↑ |
| 44 | + cached in ~/.kya/ requires consent key (pk_*) |
| 45 | +``` |
| 46 | + |
| 47 | +## Configuration |
| 48 | + |
| 49 | +| Environment Variable | Description | Default | |
| 50 | +|---------------------|-------------|---------| |
| 51 | +| `KYA_API_URL` | API base URL | `https://www.kyalabs.io` | |
| 52 | +| `KYA_API_KEY` | Consent key for enrollment (`pk_live_*` / `pk_test_*`) | — | |
| 53 | +| `KYA_EXTENDED_AUTH` | Enable device auth flow (`true` / `1`) | `false` | |
| 54 | + |
| 55 | +Legacy `PAYCLAW_*` prefixes are supported with a deprecation warning. |
| 56 | + |
| 57 | +## API |
| 58 | + |
| 59 | +### `Badge.init(opts?)` |
| 60 | + |
| 61 | +Create a Badge instance. Issues a guest pass on first run, reuses cache on subsequent runs. |
| 62 | + |
| 63 | +```typescript |
| 64 | +const badge = await Badge.init({ |
| 65 | + installId: 'custom-uuid', // override auto-generated ID (for Docker/CI) |
| 66 | + platform: 'node/v20.0.0', // platform string for telemetry |
| 67 | + agentClient: 'my-agent', // agent identifier |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +### `badge.headers()` |
| 72 | + |
| 73 | +Returns HTTP headers for identity injection: |
| 74 | + |
| 75 | +```typescript |
| 76 | +badge.headers() |
| 77 | +// { "Kya-Token": "gp_v1_abc..." } |
| 78 | +``` |
| 79 | + |
| 80 | +### `badge.identityType` |
| 81 | + |
| 82 | +Current identity tier: `"guest"` (API-issued guest pass), `"verified"` (device auth completed), or `"offline"` (API unreachable, local-only). |
| 83 | + |
| 84 | +### `badge.shouldNudge()` |
| 85 | + |
| 86 | +Whether the agent should be prompted to upgrade identity. Returns `false` in v1. |
| 87 | + |
| 88 | +### `enrollAndCacheBadgeToken(merchant)` |
| 89 | + |
| 90 | +Enroll at a merchant and receive a `kya_*` badge token. Requires `KYA_API_KEY`. |
| 91 | + |
| 92 | +```typescript |
| 93 | +import { enrollAndCacheBadgeToken } from '@kyalabs/badge-sdk' |
| 94 | + |
| 95 | +const token = await enrollAndCacheBadgeToken('store.example.com') |
| 96 | +// "kya_abc123..." |
| 97 | +``` |
| 98 | + |
| 99 | +**Important:** The badge token is only returned on the first enrollment per merchant per day. Re-enrollment on the same day returns `null` (the token hash is one-way). Persist the token from the first call. |
| 100 | + |
| 101 | +### `issueGuestPass(installId, platform?, agentClient?, badgeVersion?)` |
| 102 | + |
| 103 | +Low-level guest pass issuance. Returns `null` on failure (caller falls back to offline). |
| 104 | + |
| 105 | +### `getCachedBadgeToken(merchant?)` |
| 106 | + |
| 107 | +Retrieve a cached badge token. If no merchant specified, returns the most recently enrolled token. |
| 108 | + |
| 109 | +## Credential Storage |
| 110 | + |
| 111 | +Tokens are stored in `~/.kya/`: |
| 112 | + |
| 113 | +``` |
| 114 | +~/.kya/ |
| 115 | + install_id # persistent UUID |
| 116 | + guest_token # cached guest pass { token, expiresAt } |
| 117 | +``` |
| 118 | + |
| 119 | +## License |
| 120 | + |
| 121 | +MIT |
0 commit comments