> ## Documentation Index
> Fetch the complete documentation index at: https://developer.kodexa.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

> Install and configure the Kodexa Document TypeScript SDK (@kodexa-ai/document-wasm-ts) to process KDDB documents in Node.js and modern browsers via WebAssembly.

The Kodexa Document TypeScript SDK (`@kodexa-ai/document-wasm-ts`) brings high-performance document processing to JavaScript environments. Powered by WebAssembly, it works in both Node.js and modern browsers while delivering near-native performance.

## Requirements

<Tabs>
  <Tab title="Node.js">
    * **Node.js 16 or higher**
    * npm or yarn package manager
  </Tab>

  <Tab title="Browser">
    * Modern browser with WebAssembly support
    * ES2020+ compatibility
  </Tab>
</Tabs>

## Installation

Install from npm:

```bash theme={null}
npm install @kodexa-ai/document-wasm-ts
```

Or with yarn:

```bash theme={null}
yarn add @kodexa-ai/document-wasm-ts
```

## Setup

<Tabs>
  <Tab title="Node.js">
    Node.js setup is straightforward:

    ```typescript theme={null}
    import { Kodexa } from '@kodexa-ai/document-wasm-ts';

    async function main() {
      // Initialize the WASM runtime
      await Kodexa.init();

      // Create and work with documents
      const doc = await Kodexa.createDocument();
      console.log('Document ready');

      // Clean up when done
      doc.dispose();
    }

    main();
    ```
  </Tab>

  <Tab title="Browser">
    Browser setup requires loading additional scripts:

    ```html theme={null}
    <!DOCTYPE html>
    <html>
    <head>
      <!-- sql.js for in-browser SQLite -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.11.0/sql-wasm.js"></script>

      <!-- Kodexa bridge and Go WASM runtime -->
      <script src="node_modules/@kodexa-ai/document-wasm-ts/dist/sqljs-bridge.bundle.js"></script>
      <script src="node_modules/@kodexa-ai/document-wasm-ts/dist/wasm_exec.js"></script>
    </head>
    <body>
      <script type="module">
        import { Kodexa } from './node_modules/@kodexa-ai/document-wasm-ts/dist/index.js';

        async function init() {
          await Kodexa.init();

          const doc = await Kodexa.fromText('Hello from the browser!');
          const root = await doc.getRoot();
          console.log('Content:', root.content);

          doc.dispose();
        }

        init();
      </script>
    </body>
    </html>
    ```
  </Tab>
</Tabs>

## Verify Installation

Test your setup:

```typescript theme={null}
import { Kodexa } from '@kodexa-ai/document-wasm-ts';

async function verify() {
  await Kodexa.init();

  if (Kodexa.isLoaded()) {
    console.log('Kodexa SDK initialized successfully');

    const doc = await Kodexa.createDocument();
    console.log('Document created');

    doc.dispose();
  }
}

verify();
```

## Core Concepts

### Initialization

Unlike the Python SDK, the TypeScript SDK requires explicit initialization:

```typescript theme={null}
import { Kodexa } from '@kodexa-ai/document-wasm-ts';

// Must call init() before any other operations
await Kodexa.init();

// Check if ready
if (Kodexa.isLoaded()) {
  // Safe to use
}
```

### Async API

All document operations are asynchronous:

```typescript theme={null}
// Creating documents
const doc = await Kodexa.createDocument();
const textDoc = await Kodexa.fromText('Content');
const jsonDoc = await Kodexa.fromJson('{"data": "json"}');

// Node operations
const root = await doc.getRoot();
const children = await root.getChildren();
const content = await root.getContent();
```

### Memory Management

<Warning>
  WebAssembly memory must be explicitly freed. Always call `dispose()` when done with documents to prevent memory leaks.
</Warning>

```typescript theme={null}
const doc = await Kodexa.fromText('Content');

try {
  // Work with document
  const root = await doc.getRoot();
  // ...
} finally {
  // Always dispose
  doc.dispose();
}
```

For application shutdown:

```typescript theme={null}
// Browser
window.addEventListener('beforeunload', () => {
  Kodexa.cleanup();
});

// Node.js
process.on('exit', () => {
  Kodexa.cleanup();
});
```

## Configuration

### WASM Location

In browser environments, configure the WASM file location:

```typescript theme={null}
// Before calling Kodexa.init()
window.KODEXA_WASM_BASE_URL = '/path/to/wasm/files';

await Kodexa.init();
```

### Logging

Control the logging level:

```typescript theme={null}
await Kodexa.init();

// Set log level: 'debug' | 'info' | 'warn' | 'error'
Kodexa.setLogLevel('debug');
```

## What's Next?

<CardGroup cols={2}>
  <Card title="Getting Started" icon="rocket" href="/sdk/typescript/getting-started">
    Learn the basics of creating and manipulating documents
  </Card>

  <Card title="API Reference" icon="book" href="https://github.com/kodexa-ai/kodexa-document">
    Explore the complete API documentation
  </Card>
</CardGroup>

## Package Information

| Property     | Value                                                                                                      |
| ------------ | ---------------------------------------------------------------------------------------------------------- |
| Package Name | `@kodexa-ai/document-wasm-ts`                                                                              |
| npm          | [npmjs.com/package/@kodexa-ai/document-wasm-ts](https://www.npmjs.com/package/@kodexa-ai/document-wasm-ts) |
| License      | Apache-2.0                                                                                                 |
| Repository   | [github.com/kodexa-ai/kodexa-document](https://github.com/kodexa-ai/kodexa-document)                       |
