# Avo Inspector Node.js SDK

## Quick Start Guide

Find the Quick Start Guide in our [GitHub repo](https://github.com/avohq/node-avo-inspector).

## Installation

The library is distributed with NPM, install with npm:

```bash npm2yarn
npm i node-avo-inspector
```

## Import

```ts
import * as Inspector from "node-avo-inspector";
```

## Initialization

Obtain the API key in Inspector tab in your [Avo.app workspace](https://www.avo.app/welcome).

You will need to create an instance of **AvoInspector** with the constructor:

```ts
constructor(options: {
    apiKey: string;
    env: AvoInspectorEnv;
    version: string;
    appName?: string;
    publicEncryptionKey?: string;
});
```

All the following methods are available in the **AvoInspector** class.

> #### Parameters:
>
> - `apiKey` - the API key you get in your Avo account
> - `env` - current environment: `"dev"`, `"staging"` or `"prod"`
> - `appVersion` - your application version. A lot of Inspector features rely on versioning and you need to provide a comparable string here to get value from them. We recommend using semantic versioning or integers that are incremented in every release.
> - `appName` - your application name. Optional; Provide it to make it easier to distinguish between data from different apps.
> - `publicEncryptionKey` - optional. Your hex-encoded P-256 public key for [property value validation](#property-value-validation).

#### Property value validation

You can enable property value validation to validate property values against the [constraints defined in your tracking plan](https://www.avo.app/docs/data-design/avo-tracking-plan/properties.md#property-types-and-constraints), such as allowed values, regex patterns, and min/max ranges.

> 💡 Property value validation is available for version 1.2.0+ of the Node.js Inspector SDK. If you are using an older version, please update to the latest version.

To enable property value validation:

1. **Enable property value validation in settings** - Go to workspace settings in your Avo workspace and enable [property value validation](https://www.avo.app/docs/inspector/inspector-debugger.md#enabling-advanced-debugger-features).

To be able to [decrypt the property values](https://www.avo.app/docs/inspector/inspector-debugger.md#decrypting-property-values) in the Inspector Debugger, you need to generate a public/private key pair:

**Step 1: Generate encryption keys**

Run the following command in your terminal to generate a public/private key pair:

```bash
node -e "const { createECDH } = require('crypto'); const ecdh = createECDH('prime256v1'); ecdh.generateKeys(); console.log('Private Key:', ecdh.getPrivateKey('hex')); console.log('Public Key:', ecdh.getPublicKey('hex', 'compressed'));"
```

This will output:
- **Public key** - Used by Avo to encrypt property values
- **Private key** - Used by you to decrypt values in the Inspector Debugger

**Step 2: Store your private key securely**

Save your private key in a secure location like a password manager. You'll need this to [decrypt values in the Inspector Debugger](https://www.avo.app/docs/inspector/inspector-debugger.md#decrypting-property-values). Never share or expose your private key to a third party.

**Step 3: Add the public key to the Inspector initialization**

Pass the public key when initializing the `AvoInspector` instance:

```ts
const inspector = new Inspector.AvoInspector({
  apiKey: 'YOUR-API-KEY',
  env: 'dev',
  version: '1.0.0',
  appName: 'My App',
  publicEncryptionKey: 'YOUR-HEX-PUBLIC-KEY',
});
```

> 🔒 Property values are encrypted end-to-end. Avo only stores encrypted values and cannot decrypt them. Only you can decrypt the values using your private key in the Inspector Debugger dashboard.

Learn more about [decrypting property values in the Inspector Debugger](https://www.avo.app/docs/inspector/inspector-debugger.md#decrypting-property-values) and the [property value issue types](https://www.avo.app/docs/inspector/issue-types-in-inspector.md#property-value-issues) that can be detected.

## Sending event schemas to Avo Inspector

This is the core of the **Avo Inspector SDK**.

```ts
trackSchemaFromEvent(eventName: string, eventProperties: {
    [propName: string]: any;
}): void;
```

> #### Parameters:
>
> - `eventName` - string event name, also known as event type.
> - `eventProperties` - The actual event properties, which will be converted to an event schema on the device and the event schema sent to **Avo**. The resulting keys will be object field names and the values will be object field value types converted to schema types.
>
> ##### Example format:
>
> ```js
> var eventProperties = {
>   userId: 1337,
>   emailAddress: 'jane.doe@avo.app',
>   key: 'value',
> };
> ```

## Configuring Logs

`enableLogging` controls printing of tracked event schemas and other helpful information to logcat. Enabled by default in development environments.

```js
enableLogging(enable: boolean): void;
```

> #### Parameters:
>
> - `enable` - boolean flag that sets whether **Avo Inspector SDK** will print logs
>   .
