<p align="center">
  <img src="https://github.com/settlemint/sdk/blob/main/logo.svg" width="200px" align="center" alt="SettleMint logo" />
  <h1 align="center">DALP SDK</h1>
  <p align="center">
    <a href="https://settlemint.com">https://settlemint.com</a>
    <br/>
    Fully typed TypeScript SDK for the Digital Asset Lifecycle Platform API.
  </p>
</p>

<p align="center">
<a href="https://www.npmjs.com/package/@settlemint/dalp-sdk" rel="nofollow"><img src="https://img.shields.io/npm/v/@settlemint/dalp-sdk" alt="npm version"></a>
<a href="https://www.npmjs.com/package/@settlemint/dalp-sdk" rel="nofollow"><img src="https://img.shields.io/npm/dw/@settlemint/dalp-sdk" alt="npm downloads"></a>
</p>

<div align="center">
  <a href="https://settlemint.com">Website</a>
  <span>&nbsp;&nbsp;&bull;&nbsp;&nbsp;</span>
  <a href="https://www.npmjs.com/package/@settlemint/dalp-sdk">NPM</a>
  <span>&nbsp;&nbsp;&bull;&nbsp;&nbsp;</span>
  <a href="mailto:support@settlemint.com">Support</a>
  <br />
</div>

## About

The DALP SDK provides a fully typed client for the Digital Asset Lifecycle Platform API. Built on [oRPC](https://orpc.unnoq.com/) with contract-first types, it gives you auto-complete for every API namespace — tokens, identities, compliance, transactions, and more.

## Installation

```bash
# npm
npm install @settlemint/dalp-sdk

# bun
bun add @settlemint/dalp-sdk
```

## Quick Start

```typescript
import { createDalpClient } from "@settlemint/dalp-sdk";

const dalp = createDalpClient({
  url: "https://dalp.example.com",
  apiKey: "sm_dalp_...",
  organizationId: "org_...", // optional, for multi-org setups
});

// Every method is fully typed with auto-complete
const tokens = await dalp.token.list({ query: {} });
const system = await dalp.system.read({});
```

## Auth Client

Use `createDalpAuthClient` for Better Auth operations such as sign-up, session listing, organization management, API
keys, device authorization, and wallet verification setup.

```typescript
import { createDalpAuthClient } from "@settlemint/dalp-sdk";

const auth = createDalpAuthClient({
  url: "https://dalp.example.com",
  apiKey: "sm_dalp_...",
  organizationId: "org_...",
});

const apiKeys = await auth.apiKey.list();
```

For Node-side session flows, use the platform client. It keeps Better Auth cookies in an SDK-owned store and exposes the
DAPI client and auth client together.

```typescript
import { createDalpPlatformClient } from "@settlemint/dalp-sdk";

const platform = createDalpPlatformClient({
  url: "https://dalp.example.com",
});

await platform.auth.signUp.email({
  email: "user@example.com",
  password: "Password123!",
  name: "Example User",
});

const sessionCookie = platform.cookieStore.header;
```

### Auth Namespaces

All namespaces below are fully typed on `DalpAuthClient` — every operation has auto-complete and a typed return value.

| Namespace             | Operations                                                                                                    | Description                                                |
| --------------------- | ------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- |
| `signIn`              | `email`                                                                                                       | Sign in with email and password                            |
| `signUp`              | `email`                                                                                                       | Register a new account                                     |
| `getSession`          | —                                                                                                             | Fetch the current session                                  |
| `listSessions`        | —                                                                                                             | List all active sessions                                   |
| `revokeSession`       | —                                                                                                             | Revoke a specific session                                  |
| `revokeOtherSessions` | —                                                                                                             | Revoke all sessions except the current one                 |
| `apiKey`              | `create`, `list`, `delete`                                                                                    | Manage API keys                                            |
| `organization`        | `create`, `list`, `setActive`, `inviteMember`, `acceptInvitation`, `listMembers`, `addMember`, `removeMember` | Manage organizations and membership                        |
| `admin`               | `listUsers`, `createUser`, `removeUser`, `setRole`                                                            | Administrative user management                             |
| `device`              | `code`, `token`, `approve`, `deny`                                                                            | Device authorization flow                                  |
| `twoFactor`           | `enable`, `disable`, `verifyTotp`, `verifyBackupCode`, `generateBackupCodes`                                  | Account-level TOTP two-factor authentication               |
| `wallet.pincode`      | `enable`, `disable`, `update`, `reset`                                                                        | Wallet pincode protection                                  |
| `wallet.twoFactor`    | `enable`, `disable`, `verify`                                                                                 | Wallet two-factor verification                             |
| `wallet.secretCodes`  | `generate`, `confirm`                                                                                         | Wallet secret (recovery) codes                             |
| `passkey`             | —                                                                                                             | Account-level passkey registration (browser/WebAuthn only) |

```typescript
// Enable account-level 2FA and verify a TOTP code
await auth.twoFactor.enable({ password: "..." });
await auth.twoFactor.verifyTotp({ code: "123456" });
```

```typescript
// Enable wallet pincode protection
await auth.wallet.pincode.enable({ pincode: "1234", password: "..." });
```

## Configuration

```typescript
import { createDalpClient } from "@settlemint/dalp-sdk";

const dalp = createDalpClient({
  // Required
  url: "https://dalp.example.com",

  // Required for authenticated routes
  apiKey: "sm_dalp_...",

  // Optional
  organizationId: "org_...", // multi-org scoping
  headers: { "User-Agent": "MyApp" }, // override default headers
  idempotencyKey: "req_abc123", // ⚠️ sent on EVERY request — use only for single-mutation clients
  requestValidation: false, // validate requests client-side (default: false)
  responseValidation: false, // validate responses client-side (default: false)
  fetch: customFetch, // custom fetch implementation
});
```

## API Namespaces

| Namespace          | Description                |
| ------------------ | -------------------------- |
| `account`          | Manage accounts            |
| `actions`          | Manage actions             |
| `addons`           | Manage addons              |
| `admin`            | Administrative operations  |
| `contacts`         | Manage contacts            |
| `exchangeRates`    | View exchange rates        |
| `externalToken`    | Manage external tokens     |
| `identityRecovery` | Recover lost identities    |
| `monitoring`       | Platform monitoring        |
| `search`           | Search across the platform |
| `settings`         | Platform settings          |
| `system`           | System administration      |
| `token`            | Manage security tokens     |
| `transaction`      | View transactions          |
| `user`             | Manage platform users      |

## Endpoint Coverage

The SDK covers 100% of the v2 REST API: it is generated from the same oRPC contract that the server implements, and a
CI check (`bun run check:sdk-coverage`) diffs every mounted API route against the contract on every change. Endpoints
intentionally outside the SDK contract are named in a committed allowlist with a reason each, in these categories:

- **v1-frozen** — the deliberately frozen v1 REST surface
- **internal-ops** — health probes, CSP report sink, theme CSS, static manifests
- **webhook-receiver** — provider webhook receivers (DFNS, Ripple, compliance providers)
- **better-auth** — auth plumbing, covered by the SDK auth client instead
- **docs / asset-delivery / bundler / orpc-mount** — API docs pages, binary asset delivery, the ERC-4337 bundler
  JSON-RPC passthrough (covered by the SDK bundler client), and the oRPC transport mounts themselves

## Subpath Exports

```typescript
// Main entry — client factory, error codes, serializers
import { createDalpAuthClient, createDalpClient, createDalpPlatformClient } from "@settlemint/dalp-sdk";

// Type-only — for function signatures without runtime code
import type { DalpClient, DalpClientConfig } from "@settlemint/dalp-sdk/types";

// Plugins — optional oRPC link plugins for advanced usage
import { BatchLinkPlugin, RequestValidationPlugin } from "@settlemint/dalp-sdk/plugins";
```

## Security

- **API key authentication** — issued via the DALP dashboard (Settings → API Keys) or via `dalp login`
- Public routes can be called without an API key; authenticated routes still require one
- Security headers (`x-api-key`, `x-organization-id`) cannot be overridden by custom headers
- Client-side request validation can be enabled to catch schema violations early

## Errors

DAPI errors thrown by the SDK use `DalpSdkError` and include `message`, `why`, `fix`, `status`, `retryable`, and the
public DAPI error id when the server returns one.

## License

This project is licensed under the **SettleMint Commercial Customer Source License**. See the LICENSE file included in the package for full terms.
