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

# Record Feature Usage

> Kana needs to know whenever a user of your product has used a feature. It's a requirement so that we have accurate information on what feature's customers have used (and by how much) within the package(s) they have subscribed to.

> [!NOTE]
>
> You should implement this each time you create a feature within Kana to ensure
> your user's usage of a feature is accurately recorded.

## Prerequisites

You will need to ensure you have details on both:

* The **user** who has used the feature

* The **feature** which the user has used

If neither the user nor feature exist yet in Kana, then you should create these first:

[Create Users](https://docs.usekana.com/guides/ruby/importing-and-creating/create-users.md)

[Create Features](https://docs.usekana.com/guides/ruby/importing-and-creating/create-features.md)

Once these are created, you will need to ensure you know the `id` of both.

Both the`id` for a [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) and a [Feature](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature) are defined by you upon creation in Kana.

You can find the identifiers for both in the [Dashboard](https://dashboard.usekana.com), or by using the [features](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md#features) and [users](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md#users) queries to pull all respective records and grab the `id `from the one you need.

## Code Sample

### Basic

```javascript Node.js theme={null}
import { KanaAdmin } from "@usekana/admin-kana-js";

const client = new KanaAdmin({
  apiKey: API_KEY, // Replace with own Private API Key
});

const { data, error } = await client.features.recordUsage({
  userId: "124",
  featureId: "api-calls",
  delta: "1",
});
console.log(data);
console.log(error);
```

### Example

```javascript Node.js theme={null}
import { KanaAdmin } from '@usekana/admin-kana-js';
import { sendMessage } from '/modules/sendMessage.js'

const userId = user.id; // Assumes there's a user object that's been fetched
const featureId = 'messages' // Replace with your feature's identifier
const delta = 1 // Defines the amount to send - function only sends 1 message

const message = { text: "Hello", to: "user@gmail.com", from: "user@company.com" }

sendMessage(message)
  .then((result) => {
    const client = new KanaAdmin({
      apiKey: API_KEY // Replace with own Private API Key
    });

    const { data, error } = await kanaClient.features.recordUsage({
      userId: userId,
      featureId: featureId,
      delta: 1
    });

    if (error) {
      console.log(error);
    } else if (data.recorded == false) {
      console.log(`Kana feature usage not recorded for ${featureId}`);
    };

    console.log(result);
  })
  .catch((error) => {
    console.log(error);
});
```

> [!NOTE]
>
> This should always take place **after the feature has been successfully
> used.** Make the call when there is no possibility of error and the action
> related to the feature has taken place. This will avoid any accidental
> recording of usage when nothing has yet happened. You can always provide a
> negative `delta` in a subsequent call to revert this if needed (more in
> [recordUsage](https://docs.usekana.com/reference/admin-api-backend-reference/mutations.md#recordusage)).

## Next Steps

> [!TIP]
>
> **Congratulations** 🎉 You've now successfully tracked your user's usage of a
> feature.

You should look to ensure this is setup for all your features and in all places where those features could be used by customers. For instance, if you had a `messages` feature, your users may be able to send messages via your UI and through an API call - both should be tracked.

As we now have details on how much of a feature a user has used, you can:

* [Fetch insights on a user's current feature usage from our API](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md#showusage)

* Block usage based on if a user has no more remaining allowance

* Subscribe a user to a new package based on their feature usage

We strongly recommend following the guide on blocking customer usage next so that feature access is properly recorded and blocked when necessary.

[Block Feature Access (Backend)](https://docs.usekana.com/guides/javascript/block-feature-access-backend.md)

[Block Feature Access (Frontend)](https://docs.usekana.com/guides/javascript/block-feature-access-frontend.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.