> ## 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.

# Mutations

[Mutations](https://graphql.org/learn/queries/) define GraphQL operations which change data on the server. They are the primary way to take actions with your data in Kana. The majority of operations below require you to provide the fields you want returned after the mutation occurs - the same principles that apply to [Queries](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md) apply here too.

At the start of every mutation operation, ensure that you specify `mutation` before the field(s).

> [!NOTE]
>
> #### Query Variables
>
> We use variables throughout our example requests. We do this in order to mirror real-world application practices whereby you would most likely want dynamic values for your arguments. [Take a look at this guide if you're unfamiliar with how variables work in GraphQL](https://graphql.org/learn/queries/#variables).

## createFeature

Create a [Feature](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature).

### Arguments

| Name | Type | Description |
| - | - | - |
| `input` | [CreateFeatureInput!](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#createfeatureinput) | The input needed to create the feature. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [Feature!](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature) | The [Feature](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature) which was created. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation createFeature($input: CreateFeatureInput!) {
>   createFeature(input: $input) {
>     id
>     name
>     type
>     unitLabel
>     unitLabelPlural
>     metadata
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "input": {
>     "id": "api-calls",
>     "name": "API Calls",
>     "type": "CONSUMABLE",
>     "unitLabel": "API Call",
>     "unitLabelPlural": "API Calls",
>     "metadata": {}
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "createFeature": {
>       "id": "api-calls",
>       "name": "API Calls",
>       "type": "CONSUMABLE",
>       "unitLabel": "API Call",
>       "unitLabelPlural": "API Calls",
>       "metadata": {}
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation createFeature($input: CreateFeatureInput!) { createFeature(input: $input) { id name type unitLabel unitLabelPlural metadata } }", "variables": "{\"input\":{\"id\":\"api-calls\",\"name\":\"API Calls\",\"type\":\"CONSUMABLE\",\"unitLabel\":\"API Call\",\"unitLabelPlural\":\"API Calls\",\"metadata\":{}}}" }'
> ```

> [tab] Node.js
>
> ```javascript 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.create({id: 'api-calls', name: 'API Calls', type: 'CONSUMABLE', unitLabel: 'API Call', unitLabelPlural: 'API Calls', metadata: {}});
> console.log(data);
> console.log(error);
> ```

## updateFeature

Update a [Feature](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature).

### Arguments

| Name | Type | Description |
| - | - | - |
| `input` | [UpdateFeatureInput!](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#updatefeatureinput) | The input needed to update the feature. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [Feature!](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature) | The [Feature](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature) which was updated. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation updateFeature($input: UpdateFeatureInput!) {
>   updateFeature(input: $input) {
>     id
>     name
>     type
>     unitLabel
>     unitLabelPlural
>     metadata
>     packages {
>       id
>       name
>       status
>       isAddon
>       updatedAt
>       features {
>         id
>         name
>         type
>         limit
>         unitLabel
>         unitLabelPlural
>         metadata
>       }
>       prices {
>         id
>         provider
>         amount
>         displayAmount
>         currency
>         interval
>       }
>     }
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "id": "api-calls",
>   "input": {
>     "name": "API Calls",
>     "unitLabel": "API Call",
>     "unitLabelPlural": "API Calls",
>     "metadata": {}
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data":{
>     "updateFeature":{
>       "id":"api-calls",
>       "name":"API Calls",
>       "type":"CONSUMABLE",
>       "unitLabel": "API Call",
>       "unitLabelPlural": "API Calls",
>       "metadata":{},
>       "packages":[
>         {
>           "id":"pro-plan",
>           "name":"Pro Plan",
>           "status":"PUBLISHED",
>           "metadata":{},
>           "isAddon":false,
>           "updatedAt":"2022-05-10T12:39:08.965Z",
>           "features":[
>             {
>               "id":"api-calls",
>               "name":"API Calls",
>               "limit":"1000",
>               "type":"CONSUMABLE"
>               "unitLabel": "API Call",
>               "unitLabelPlural": "API Calls",
>               "metadata":{}
>             }
>           ],
>           "prices":[
>             {
>               "id": "price_1L7NHvIiEnRX8aivoxbSWREF",
>               "provider": "STRIPE",
>               "amount": 5000,
>               "displayAmount": "50.00",
>               "currency": "gbp",
>               "interval": "month"
>             }
>           ]
>         }
>       ]
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation updateFeature($input: UpdateFeatureInput!) { updateFeature(input: $input) { id name type unitLabel unitLabelPlural metadata packages { id name status isAddon updatedAt features { id name type limit unitLabel unitLabelPlural metadata } prices { id provider amount displayAmount currency interval } } } }", "variables": "{\"id\":\"api-calls\",\"input\":{\"name\":\"API Calls\",\"unitLabel\":\"API Call\",\"unitLabelPlural\":\"API Calls\",\"metadata\":{}}}" }'
> ```

> [tab] Node.js
>
> ```javascript 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.update({id: 'api-calls', input: { name: 'API Calls', unitLabel: 'API Call', unitLabelPlural: 'API Calls', metadata: {} }});
> console.log(data);
> console.log(error);
> ```

## createPackage

Create a [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package).

> [!NOTE]
>
> **Package Status**
>
> Your package will always have a status of DRAFT upon being created. If you want to publish this package then you will have to do so through the [Dashboard](https://dashboard.usekana.com).

### Arguments

| Name | Type | Description |
| - | - | - |
| `input` | [CreatePackageInput!](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#createpackageinput) | The input needed to create the package. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [**Package!**](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) | The [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) which was created. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation createPackage($input: CreatePackageInput!) {
>   createPackage(input: $input) {
>     id
>     name
>     status
>     metadata
>     features {
>       id
>       name
>       limit
>       type
>       unitLabel
>       unitLabelPlural
>       metadata
>     }
>     prices {
>       id
>       provider
>       amount
>       displayAmount
>       currency
>       interval
>     }
>     isAddon
>     updatedAt
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "input": {
>     "id": "pro-plan",
>     "name": "Pro Plan",
>     "isAddon": false,
>     "metadata": {},
>     "features": {
>       "id": "api-calls",
>       "entitlement": {
>         "limit": 1000,
>         "resetPeriod": "MONTH",
>         "overageEnabled": false
>       }
>     }
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "createPackage": {
>       "id": "pro-plan",
>       "name": "Pro Plan",
>       "status": "DRAFT",
>       "metadata": {},
>       "features": [
>         {
>           "id": "api-calls",
>           "name": "API Calls",
>           "limit": "1000",
>           "type": "CONSUMABLE",
>           "unitLabel": "API Call",
>           "unitLabelPlural": "API Calls",
>           "metadata": {}
>         }
>       ],
>       "prices": [
>         {
>           "id": "price_1L7NHvIiEnRX8aivoxbSWREF",
>           "provider": "STRIPE",
>           "amount": 5000,
>           "displayAmount": "50.00",
>           "currency": "gbp",
>           "interval": "month"
>         }
>       ],
>       "isAddon": false,
>       "updatedAt": "2021-11-25T16:57:42.778Z"
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation createPackage($input: CreatePackageInput!) { createPackage(input: $input) { id name status metadata features { id name limit type unitLabel unitLabelPlural metadata } prices { id provider amount displayAmount currency interval } isAddon updatedAt }}", "variables": "{"input":{"id":"pro-plan","name":"Test User","isAddon":false,"metadata":{},"features":{"id":"api-calls","entitlement":{"limit":1000,"resetPeriod":"MONTH","overageEnabled":false}}}}" }'
> ```

> [tab] Node.js
>
> ```javascript 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.users.create({id: '124', name: 'Pro Plan', isAddon: false, metadata: {}, features: { id: 'api-calls', entitlement: { limit: 1000, resetPeriod: "MONTH", overageEnabled: false }}});
> console.log(data);
> console.log(error);
> ```

## updatePackage

Update a [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package).

> [!WARNING]
>
> Only `metadata` can be updated on the Package object through the API. All other updates will need to be done through the [Dashboard](https://dashboard.usekana.com). Let us know if you want to see further updatable fields and your use-cases!

### Arguments

| Name | Type | Description |
| - | - | - |
| `input` | [UpdatePackageInput!](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#updatepackageinput) | The input needed to update the package. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [Package!](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) | The [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) which was updated. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation updatePackage($input: UpdatePackageInput!) {
>   updatePackage(input: $input) {
>     id
>     name
>     status
>     metadata
>     features {
>       id
>       name
>       limit
>       type
>       unitLabel
>       unitLabelPlural
>       metadata
>     }
>     prices {
>       id
>       provider
>       amount
>       displayAmount
>       currency
>       interval
>     }
>     isAddon
>     updatedAt
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "id": "pro-plan",
>   "input": {
>     "metadata": {}
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "updatePackage": {
>       "id": "pro-plan",
>       "name": "Pro Plan",
>       "status": "DRAFT",
>       "metadata": {},
>       "features": [
>         {
>           "id": "api-calls",
>           "name": "API Calls",
>           "limit": "1000",
>           "type": "CONSUMABLE",
>           "unitLabel": "API Call",
>           "unitLabelPlural": "API Calls",
>           "metadata": {}
>         }
>       ],
>       "prices": [
>         {
>           "id": "price_1L7NHvIiEnRX8aivoxbSWREF",
>           "provider": "STRIPE",
>           "amount": 5000,
>           "displayAmount": "50.00",
>           "currency": "gbp",
>           "interval": "month"
>         }
>       ],
>       "isAddon": false,
>       "updatedAt": "2021-11-25T16:57:42.778Z"
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation updatePackage($input: UpdatePackageInput!) { updatePackage(input: $input) { id name status metadata features { id name limit type unitLabel unitLabelPlural metadata } prices { id provider amount displayAmount currency interval } isAddon updatedAt }}", "variables": "{ \"input\": { \"metadata\": {}}}" }'
> ```

> [tab] Node.js
>
> ```javascript 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.packages.update({id: 'pro-plan', input: { metadata: {} }});
> console.log(data);
> console.log(error);
> ```

## createUser

Create a [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user).

### Arguments

| Name | Type | Description |
| - | - | - |
| `input` | [**CreateUserInput!**](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#createuserinput) | The input needed to create the user. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [**User!**](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) | The [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) which was created. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation createUser($input: CreateUserInput!) {
>   createUser(input: $input) {
>     id
>     billingId
>     name
>     email
>     metadata
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "input": {
>     "id": "124",
>     "billingId": "cus_883y74hdjjdjdnd",
>     "name": "Test User",
>     "email": "test@usekana.com",
>     "metadata": {}
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "createUser": {
>       "id": "124",
>       "billingId": "cus_883y74hdjjdjdnd",
>       "name": "Test User",
>       "email": "test@usekana.com",
>       "metadata": {}
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation createUser($input: CreateUserInput!) { createUser(input: $input) { id billingId name email } }", "variables": "{ \"input\": { \"id\": \"124\", \"name\": \"Test User\", \"email\": \"test@usekana.com\", \"billingId\": \"cus_883y74hdjjdjdnd\" } }" }'
> ```

> [tab] Node.js
>
> ```javascript 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.users.create({id: '124', billingId: 'cus_883y74hdjjdjdnd', name: 'Test User', email: 'test@usekana.com'});
> console.log(data);
> console.log(error);
> ```

## updateUser

Update a [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user).

> [!NOTE]
>
> **How can I update the email or billingId of a user?**
>
> You will need to do this through a CSV Import within our [Dashboard](https://dashboard.usekana.com). We'll soon make this possible through the API.

### Arguments

| Name | Type | Description |
| - | - | - |
| `input` | [UpdateUserInput!](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#updateuserinput) | The input needed to update the user. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [**User!**](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) | The [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) which was updated. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation updateUser($id: String!, $input: CreateUserInput!) {
>   updateUser(id: $id, input: $input) {
>     id
>     billingId
>     name
>     email
>     metadata
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "id": "124",
>   "input": {
>     "id": "125",
>     "name": "Updated User",
>     "metadata": {}
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "createUser": {
>       "id": "125",
>       "billingId": "cus_883y74hdjjdjdnd",
>       "name": "Updated User",
>       "email": "test@usekana.com",
>       "metadata": {}
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation updateUser($id: String!, $input: UpdateUserInput!) { updateUser(id: $id, input: $input) { id billingId name email } }", "variables": "{ "\id\": \"124\", \"input\": { \"id\": \"125\", \"name\": \"Updated User\" } }" }'
> ```

> [tab] Node.js
>
> ```javascript 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.users.update('124', { id: '125', name: 'Updated User' });
> console.log(data);
> console.log(error);
> ```

## subscribe

Subscribe a [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) to a [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) - either one or multiple. Returns a [PackageSubscription](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#packagesubscription).

Users can only be subscribed to a [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) with a status of `PUBLISHED`.

### Arguments

| Name | Type | Description |
| - | - | - |
| `packageIds` | [\[String!\]!](https://docs.usekana.com/reference/admin-api-backend-reference/scalars#int) | An array of id's for the packages which the user is to be subscribed to. These are the `id` fields of a[Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package). |
| `userId` | [String!](https://docs.usekana.com/reference/admin-api-backend-reference/scalars.md#string) | The identifier of the user to subscribe the package(s) to. This maps to the `id` field as set on the [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) object. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [\[Subscription\]!](https://docs.usekana.com/reference/admin-api-backend-reference/objects#subscription) | An array of subscriptions to the packages which the user in question has just been subscribed to. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation subscribeUserToPlan($packageIds: [String!]!, $userId: String!) {
>   subscribe(packageIds: $packageIds, userId: $userId) {
>     id
>     userId
>     status
>     package {
>       id
>       name
>       status
>       metadata
>       features {
>         id
>       	name
>         limit
>         type
>         unitLabel
>         unitLabelPlural
>         metadata
>       }
>       prices {
>         id
>         provider
>         amount
>         displayAmount
>         currency
>         interval
>       }
>       isAddon
>       updatedAt
>     }
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "packageIds": ["free-trial", "proactive-messaging"],
>   "userId": "124"
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "subscribe": [
>       {
>         "id": "2",
>         "userId": "124",
>         "status": "ACTIVE",
>         "metadata": {},
>         "package": {
>           "id": "free-trial",
>           "name": "Free Trial",
>           "status": "PUBLISHED",
>           "metadata": {},
>           "features": [
>             {
>               "id": "api-calls",
>               "name": "API Calls",
>               "limit": "1000",
>               "type": "CONSUMABLE",
>               "unitLabel": "API Call",
>               "unitLabelPlural": "API Calls",
>               "metadata": {}
>             }
>           ],
>           "prices": [
>             {
>               "id": "price_1L7NHvIiEnRX8aivoxbSWREF",
>               "provider": "STRIPE",
>               "amount": 5000,
>               "displayAmount": "50.00",
>               "currency": "gbp",
>               "interval": "month"
>             }
>           ],
>           "isAddon": false,
>           "updatedAt": "2021-11-25T16:57:42.778Z"
>         }
>       },
>       {
>         "id": "3",
>         "userId": "124",
>         "status": "CANCELLED",
>         "package": {
>           "id": "proactive-messaging",
>           "name": "Proactive Messaging",
>           "status": "PUBLISHED",
>           "metadata": {},
>           "features": [
>             {
>               "id": "messages",
>               "name": "Messages",
>               "limit": "50",
>               "type": "CONSUMABLE",
>               "unitLabel": "Message",
>               "unitLabelPlural": "Message",
>               "metadata": {}
>             }
>           ],
>           "prices": [
>             {
>               "id": "price_id_6gSGwU839shamnskk7LSNME",
>               "provider": "STRIPE",
>               "amount": 2000,
>               "displayAmount": "20.00",
>               "currency": "gbp",
>               "interval": null
>             }
>           ],
>           "isAddon": true,
>           "updatedAt": "2021-11-27T18:25:10.081Z"
>         }
>       }
>     ]
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation subscribeUserToPackage($packageIds: [String!]!, $userId: String!) { subscribe(packageIds: $packageIds, userId: $userId) { id userId status package { id name status metadata features { id name limit type unitLabel unitLabelPlural metadata } prices { id provider amount displayAmount currency interval } isAddon updatedAt } } }", "variables": "{ \"packageIds\": [ \"free-plan\", \"proactive-messaging\" ], \"userId\": \"124\" } " }'
>
> ```

> [tab] Node.js
>
> ```javascript theme={null}
> import { KanaAdmin } from '@usekana/admin-kana-js'; \
>
> const client = new KanaAdmin({
>     apiKey: API_KEY // Replace with own Private API Key
> });
>
> const packageIds = ['free-plan', 'proactive-messaging']
>
> const { data, error } = await client.subscriptions.create({packageIds: packageIds, userId: '124'});
> console.log(data);
> console.log(error);
> ```

### Errors

> [accordion] Cannot subscribe to addon
>
> #### Why?
>
> A user is being subscribed to an Add-on plan, but is not subscribed yet to a base (ie. live) plan.
>
> #### **Solution**
>
> Subscribe the user to a base (or live) plan before, or during, this call. The `isAddon` field for the [plan](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#plan) object must be `false`. You can check existing Plans with the [plans](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md#plans) query.
>
> #### Example
>
> ```json theme={null}
> {
>   "message": "Cannot subscribe to addon. Missing live tier subscription.",
>   "locations": [
>     {
>       "line": 2,
>       "column": 3
>     }
>   ],
>   "path": ["subscribe"],
>   "extensions": {
>     "code": "BAD_USER_INPUT"
>   }
> }
> ```

> [accordion] Cannot subscribe to multiple tiers
>
> #### Why?
>
> A user is being subscribed to multiple base plans (ie. tiers) within the mutation, or is already subscribed to a base plan.
>
> #### **Solution**
>
> Ensure there's only one base plan in the mutation, or check that the user is not already subscribed to a base plan with the [subscription](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md#subscription) query. The `isAddon` field for the [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package) object will be `false` if it's a base plan. You can check Packages with the [packages](https://docs.usekana.com/reference/admin-api-backend-reference/queries.md#packages) query.
>
> #### Example
>
> ```json theme={null}
> {
>   "message": "Cannot subscribe to multiple tiers",
>   "locations": [
>     {
>       "line": 2,
>       "column": 3
>     }
>   ],
>   "path": ["subscribe"],
>   "extensions": {
>     "code": "BAD_USER_INPUT"
>   }
> }
> ```

> [accordion] Package Id not found for app
>
> ```json theme={null}
> {
>   "message": "Package Id not found for app",
>   "locations": [
>     {
>       "line": 2,
>       "column": 3
>     }
>   ],
>   "path": ["subscribe"],
>   "extensions": {
>     "code": "BAD_USER_INPUT"
>   }
> }
> ```

## recordUsage

Records the usage of a [Feature](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#feature) by a [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user), including the amount it was used by.

> [!NOTE]
>
> **Revert Usage**
>
> If you want to revert usage by a certain amount (ie. subtract rather than add from the usage of a feature), you can do so by providing a negative amount in the `delta `argument (ie. `-1`).

### Arguments

| Name | Type | Description |
| - | - | - |
| input | [CreateUsageEventInput!](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md#createusageeventinput) | The input needed to record the usage event. See [Inputs](https://docs.usekana.com/reference/admin-api-backend-reference/inputs.md) for more. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [RecordedUsage](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#recordedusage) | An object containing information on whether the usage of the feature was recorded or not (`recorded` boolean and `reason`). |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation recordUsage($input: CreateUsageEventInput!) {
>   recordUsage(input: $input) {
>     recorded
>     reason
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "input": {
>     "userId": "124",
>     "featureId": "api-calls",
>     "delta": 100,
>     "action": "SET"
>   }
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "recordUsage": {
>       "recorded": true,
>       "reason": null
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation recordUsage($input: CreateUsageEventInput!) { recordUsage(input: $input) { recorded reason } }", "variables": "{ \"input\": { \"userId\": \"124\", \"featureId\": \"api-calls\", \"delta\": 100, \"action\": \"SET\" } }" }'
>
> ```

> [tab] Node.js
>
> ```javascript 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: 100, action: 'SET'});
> console.log(data);
> console.log(error);
> ```

## generateSubscriptionLinks

Generate a [payment link](https://stripe.com/docs/payments/payment-links) which a user can navigate to in order to pay and subscribe to a [Package](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#package).

> [!WARNING]
>
> A billing template (which has a price associated to it) **must** be created
> and linked to the package you want a user to subscribe to. You can do so in
> the [Dashboard](https://dashboard.usekana.com). You will need to provide a
> valid `templateId` - otherwise a`SubscriptionLinkError` error will be
> returned.

> [!NOTE]
>
> Upon successful payment, we will automatically receive notification of the
> user and the packages they have subscribed to in order to create
> [PackageSubscription](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#packagesubscription)**s**.
> You do **not** need to notify Kana (ie. through the
> [subscribe](https://docs.usekana.com/reference/admin-api-backend-reference/mutations.md#subscribe)
> mutation) yourself.

### Arguments

| Name | Type | Description |
| - | - | - |
| `userId` | [String](https://docs.usekana.com/reference/admin-api-backend-reference/scalars.md#string) | The identifier of the user who will be subscribing. This maps to the `id` field as set on the [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) object. Your billing provider will otherwise create this user in Kana for you if you don't have an identifier available. |
| `templateId` | [String!](https://docs.usekana.com/reference/admin-api-backend-reference/scalars.md#string) | The identifier of the billing template which is related to the package and price which you want to subscribe the user to. |
| `successUrl` | [String!](https://docs.usekana.com/reference/admin-api-backend-reference/scalars.md#string) | The URL which you want to redirect users to upon successful payment to subscribe. |
| `cancelUrl` | [String!](https://docs.usekana.com/reference/admin-api-backend-reference/scalars.md#string) | The URL which you want to redirect users to upon a cancelled payment to subscribe. |

### Returns

| Name | Type | Description |
| - | - | - |
| `data` | [SubscriptionLink!](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#subscriptionlink) | An object containing the generated URL (`url`) which a user can navigate to in order to pay and subscribe to a package. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation generateSubscriptionLink($userId: String, $templateId: String!, $successUrl: String!, $cancelUrl: String!) {
>   generateSubscriptionLink(userId: $userId, templateId: $templateId, successUrl: $successUrl, cancelUrl: $cancelUrl) {
>     url
>   }
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "userId": "124",
>   "templateId": "pro-50gbp-month",
>   "successUrl": "https://usekana.com/pricing/success",
>   "cancelUrl": "https://usekana.com/pricing/cancelled"
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "generateSubscriptionLink": {
>       "url": "https://checkout.stripe.com/pay/cs_test_a1l1XbatH0jyo4PBOr2ZUdzSZYjgTevA6B1feZIloI8Wv0jWLDE6JTsgre#fidkdWxOYHwnPyd1blpxYHZxWjA0TkJsMlJCbWhMT2Y8QjI3bnZVajVsVWtWQzU3Z0BufHdpdEBTb11GdH00V0xNdlFsSXxqVjRGTXFCRlxsXzxSNWw0XWNRQ2ljclBTR1Q8XEBLMXBkV3ZDNTVRRGZAbDZ1cycpJ2N3amhWYHdzNHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYCkndnF3bHVgRGZmanBrcSc%2FL2RmZnFaNE50TXVuTXxWVGowPUF2byd4JSUn"
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation generateSubscriptionLink($userId: String, $templateId: String!, $successUrl: String!, $cancelUrl: String!) { generateSubscriptionLink(userId: $userId, templateId: $templateId, successUrl: $successUrl, cancelUrl: $cancelUrl) { url } }", "variables": "{ \"userId\": \"124\", \"templateId\": \"pro-50gbp-month\", \"successUrl\":\"https://usekana.com/pricing/success\",\"cancelUrl\": \"https://usekana.com/pricing/cancelled\" }" }'
> ```

> [tab] Node.js
>
> ```javascript theme={null}
> // Not yet available in SDKs
> // We're working on this currently - stay tuned!
> ```

## generateUserToken

Generates a user-specific token for the client-side [Client SDK (Frontend)](https://docs.usekana.com/client-sdk-frontend.md). You can find out more in our [Authorization (Frontend)](https://docs.usekana.com/client-sdk-frontend/authorization-frontend.md) guide.

### Arguments

| Name | Type | Description |
| - | - | - |
| userId | [String!](https://docs.usekana.com/reference/admin-api-backend-reference/scalars.md#string) | The id of the [User](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#user) who you want to generate the token for. |

### Return Fields

| Name | Type | Description |
| - | - | - |
| `data` | [**GeneratedUserToken**](https://docs.usekana.com/reference/admin-api-backend-reference/objects.md#generatedusertoken) | An object containing the `token` which can be used to authenticate a user with the Client SDK. |
| `errors` / `error` | See [Errors](https://docs.usekana.com/reference/admin-api-backend-reference.md#errors) | Returns any errors which may have occurred with the request. |

### Examples

> [tab] Request/Response
>
> #### Request Body
>
> ```javascript theme={null}
> mutation generateUserToken($userId: String!) {
>   generateUserToken(userId: $userId)
> }
> ```
>
> #### Request Query Variables
>
> ```json theme={null}
> {
>   "userId": "124"
> }
> ```
>
> **Response JSON**
>
> ```json theme={null}
> {
>   "data": {
>     "generateUserToken": {
>       "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpLOPTE.eyJhcFAKERI6InRlc3RfZmhqZGlqaHMiLCJ1c2VySWQiOiIxMjQiLCJpYXQiOjE2NTIyNjI4MTUsImV4cCI6MTY1NDg1NDgxNSwiYXVkIjoia2FuYS1jbGllbnQiLCJpc3MiOiJ1c2VrYW5hLmNvbSJ9.hD3NeQlykA7ucCvbLp3bYEHye23zlca85yhq1s2C6a8"
>     }
>   }
> }
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://api.usekana.com/graphql \
> -X POST \
> -H "Authorization: INSERT_API_KEY" \
> -H "Content-Type:application/json" \
> -d '{ "query": "mutation generateUserToken($userId: String!) { generateUserToken(userId: $userId) { token } }", "variables": "{ \"userId\": \"124\" }" }'
> ```

> [tab] Node.js
>
> ```javascript theme={null}
> import { KanaAdmin } from '@usekana/admin-kana-js'; \
>
> const client = new KanaAdmin({
>     apiKey: API_KEY // Replace with own Private API Key
> });
>
> const packageIds = ['free-plan', 'proactive-messaging']
>
> const { data, error } = await client.users.generateToken({userId: '124'});
> console.log(data);
> console.log(error);
> ```


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