npm package: @fluidbyte-llc/fluidbyte-client
Description: Library to access FluidByte's services.
Author: Ray Ng
Latest version: 1.2.1

# FluidByte Client

[![npm version](https://img.shields.io/npm/v/@fluidbyte/fluidbyte-client)](https://www.npmjs.com/package/@fluidbyte-llc/fluidbyte-client)
[![License](https://img.shields.io/npm/l/@fluidbyte-llc/fluidbyte-client)](LICENSE)

FluidByte Client is a lightweight TypeScript library for interfacing with the FluidByte API. It works in modern browsers and Node.js environments without external dependencies—leveraging native `fetch` and `WebSocket` implementations. With a simple API, you can easily run text engines and workflows using the default FluidByte service endpoint.

## Installation

Install via npm:

```bash
npm install @fluidbyte/fluidbyte-client
```

## Usage

FluidByte Client requires only an API key to get started. The default FluidByte API endpoint is used internally.

### Using ES Modules (Browser or Bundler)

Import the client in your project as follows:

```js
import FluidByteClient from '@fluidbyte/fluidbyte-client';

(async () => {
  // Instantiate the client with your FluidByte API key.
  const client = new FluidByteClient("YOUR_API_KEY");

  // Initialize the client (loads user profile, text engines, and workflows).
  await client.initialize();

  // Optionally, view your profile info in the console.
  client.me();

  // Run a text engine. Replace "YOUR_TEXT_ENGINE_ID" with the appropriate engine identifier.
  try {
    const result = await client.runTextEngine("YOUR_TEXT_ENGINE_ID", {
      /* your input JSON data */
    });
    console.log("Text engine output:", result);
  } catch (err) {
    console.error("Error running text engine:", err);
  }
})();
```

### Using UMD via a Script Tag (or via a CDN)

You can also load FluidByte Client using the UMD bundle from a CDN like jsDelivr:

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>FluidByte Client UMD Example</title>
    <!-- Load the UMD bundle from jsDelivr -->
    <script src="https://cdn.jsdelivr.net/npm/@fluidbyte/fluidbyte-client/dist/umd/index.js"></script>
  </head>
  <body>
    <script>
      // FluidByteClient is available as a global variable.
      const client = new FluidByteClient("YOUR_API_KEY");

      client.initialize()
        .then(() => {
          // Run the text engine (replace with your engine ID and input data)
          return client.runTextEngine("YOUR_TEXT_ENGINE_ID", {
            /* your input JSON data */
          });
        })
        .then((result) => console.log("Engine output:", result))
        .catch((err) => console.error("Error:", err));
    </script>
  </body>
</html>
```

## API Overview

- **FluidByteClient(apiKey: string)**
  Creates a new FluidByte client instance with the provided API key. The FluidByte API endpoint is set internally.

- **initialize(): Promise<void>**
  Asynchronously initializes the client by loading the current user's profile, organization details, available text engines, and workflows.

- **me(): void**
  Logs the current user and organization information to the console.

- **runTextEngine(textEngineId: string, data: any, timeout?: number): Promise<any>**
  Runs a text engine identified by the given ID using the provided input data. Returns the engine's output data on completion.

- **runWorkflow(workflowAlias: string, data?: any, timeout?: number): Promise<any>**
  Runs a workflow, identified by its alias, with the given input data.

- **close(): Promise<void>**
  Performs any necessary cleanup for the client (if applicable).

## Example Test Page

A sample HTML file is provided in the `test/` directory to help you quickly test the library in your browser. This test page allows you to enter your API key, text engine ID, and input JSON; it then displays the output from running a text engine.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.