# Add Feature Consumption

### Endpoint

* Method: `POST`
* Path: `/api/v4/add_feature_consumption`
* Description: Adds 1 or more consumptions to a license feature (licenses with consumption features only).

### Authentication

See [License API Authorization](/license-api/license-api-authorization.md).

#### Required headers

* `Date` (string) — RFC7231 GMT date string
* `Authorization` (string)

#### Recommended headers

* `Accept: application/json`
* `Content-Type: application/json`

### Request

#### Body parameters

Required:

* `hardware_id` (string)
* `product` (string)
* `feature` (string)

One of:

* `license_key` (string)
* `username` (string)

Optional:

* `license_id` (number)
* `consumptions` (number) — Defaults to `1`. Can be negative if allowed.
* `event` (array of strings) — `consumption_add` (default) or `offline_floating_consumptions_sync` (mutually exclusive)

### Schema

<details>

<summary><strong>Request schema (TypeScript + JSON Schema)</strong></summary>

**TypeScript**

```typescript
type AddFeatureConsumptionRequestBody = ({
  
  // required for key-based products:
  license_key: string,
 
} | { 
  // required for user-based products:
  username: string,

}) & {
  // required parameters:
  product: string,
  hardware_id: string,
  feature: string,
  
  // optional parameters:
  license_id?: string | undefined,
  consumptions?: number | undefined,
  event?: ('consumption_add' | 'offline_floating_consumptions_sync')[] | undefined,
}
```

**JSON Schema**

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "allOf": [
    {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "license_key": { "type": "string" }
          },
          "required": ["license_key"],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "username": { "type": "string" }
          },
          "required": ["username"],
          "additionalProperties": false
        }
      ]
    },
    {
      "type": "object",
      "properties": {
        "hardware_id": { "type": "string" },
        "product": { "type": "string" },
        "feature": { "type": "string" },
        "license_id": { "type": "number" },
        "consumptions": { "type": "number" },
        "event": {
          "type": "array",
          "items": { "type": "string", "enum": ["consumption_add", "offline_floating_consumptions_sync"] },
          "minItems": 1,
          "not": {
            "allOf": [
              { "contains": { "const": "consumption_add" } },
              { "contains": { "const": "offline_floating_consumptions_sync" } }
            ]
          }
        }
      },
      "required": ["hardware_id", "product", "feature"],
      "additionalProperties": false
    }
  ]
}
```

</details>

<details>

<summary><strong>Response schema (TypeScript + JSON Schema)</strong></summary>

**TypeScript**

```typescript
type AddFeatureConsumptionResponseBody = {
    allow_negative_consumptions: boolean,
    allow_overages: boolean,
    allow_unlimited_consumptions: boolean,
    consumption_period: 'daily' | 'weekly' | 'monthly' | 'annually' | null,
    is_floating_cloud: boolean,
    is_floating: boolean,
    max_consumptions: number,
    max_overages: number,
    reset_consumption: boolean,
    total_consumptions: number,
    
    // the following properties are only present on floating and floating cloud features:
   floating_timeout?: number | null,
   floating_users?: number,
}
```

**JSON Schema**

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": { "type": "number" },
    "max_consumptions": { "type": "number" },
    "total_consumptions": { "type": "number" },
    "allow_unlimited_consumptions": { "type": "boolean" },
    "allow_negative_consumptions": { "type": "boolean" },
    "allow_overages": { "type": "boolean" },
    "max_overages": { "type": "number" },
    "reset_consumption": { "type": "boolean" },
    "consumption_period": { "type": [ "string", "null" ], "enum": [ "daily", "weekly", "monthly", "annually", null ] },
    "is_floating_cloud": { "type": "boolean" },
    "is_floating": { "type": "boolean" },
    "floating_timeout": { "type": [ "number", null ] },
    "floating_users": { "type": "number" }
  },
  "required": [
    "id",
    "max_consumptions",
    "total_consumptions",
    "allow_unlimited_consumptions",
    "allow_negative_consumptions",
    "allow_overages",
    "max_overages",
    "reset_consumption",
    "consumption_period",
    "is_floating_cloud",
    "is_floating"
  ],
  "additionalProperties": false
}
```

</details>

### Response

<details>

<summary><strong>Success response example (200)</strong></summary>

```json
{
  "total_consumptions": 5,
  "max_consumptions": 20,
  "allow_unlimited_consumptions": false,
  "allow_negative_consumptions": true,
  "allow_overages": false,
  "max_overages": 0,
  "reset_consumption": true,
  "consumption_period": "daily",
  "is_floating": false,
  "is_floating_cloud": false
}
```

</details>

<details>

<summary><strong>Error response example (400)</strong></summary>

```json
{
  "unknown_product": "Provided product was not found",
  "license_not_found": "License with the provided license user not found",
  "license_not_enabled": "The license is not enabled",
  "device_not_found": "An active device matching the hardware_id not found.",
  "blacklisted": "This device is blacklisted",
  "license_not_active": "This license is not activated",
  "unsupported_product_feature": "One or more of requested product features     do not exists or they are not enabled on the product.",
  "Feature_not_enough_consumptions": "Not enough consumptions left."
}
```

</details>

### Examples

{% tabs %}
{% tab title="curl" %}

```bash
curl --location --request POST '/api/v4/add_feature_consumption' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Date: string' \
--header 'Authorization: string' \
--data-raw '{
  "hardware_id": "some-unique-id",
  "product": "XY",
  "feature": "FEATURE",
  "license_key": "AAAA-BBBB-CCCC-DDDD",
  "consumptions": 1
}'
```

{% endtab %}

{% tab title="nodejs" %}

```javascript
var request = require('request');
var options = {
  method: 'POST',
  url: '/api/v4/add_feature_consumption',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Date': 'string',
    'Authorization': 'string'
  },
  body: JSON.stringify({
    hardware_id: 'some-unique-id',
    product: 'XY',
    feature: 'FEATURE',
    license_key: 'AAAA-BBBB-CCCC-DDDD',
    consumptions: 1
  })
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});
```

{% endtab %}

{% tab title="javascript (fetch)" %}

```javascript
var myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Date", "string");
myHeaders.append("Authorization", "string");

var raw = JSON.stringify({
  hardware_id: "some-unique-id",
  product: "XY",
  feature: "FEATURE",
  license_key: "AAAA-BBBB-CCCC-DDDD",
  consumptions: 1
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("/api/v4/add_feature_consumption", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
```

{% endtab %}

{% tab title="python" %}

```python
import requests

url = "/api/v4/add_feature_consumption"

payload = {
  "hardware_id": "some-unique-id",
  "product": "XY",
  "feature": "FEATURE",
  "license_key": "AAAA-BBBB-CCCC-DDDD",
  "consumptions": 1
}

headers = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "Date": "string",
  "Authorization": "string"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
```

{% endtab %}

{% tab title="ruby" %}

```ruby
require "uri"
require "net/http"
require "json"

url = URI("/api/v4/add_feature_consumption")

http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Content-Type"] = "application/json"
request["Date"] = "string"
request["Authorization"] = "string"
request.body = JSON.dump({
  hardware_id: "some-unique-id",
  product: "XY",
  feature: "FEATURE",
  license_key: "AAAA-BBBB-CCCC-DDDD",
  consumptions: 1
})

response = http.request(request)
puts response.read_body
```

{% endtab %}
{% endtabs %}

### License Authorization Method

There are two types of product licenses based on how the client application authorizes itself to interact with a license:

* Key-based product licenses: client interactions with the license have to be authorized using a `license_key`
* User-based product licenses: the license has a corresponding "license user" instead of a license key. Client interactions with the license have to be authorized using a `username`

### Errors

If an error occurs, the response will have an HTTP status code of 400 or higher, and the response body will contain an error description in the following format:

:::CodeblockTabs

```typescript
{
  status: number,
  code: string,
  message: string
}
```

:::

JSON Schema

:::CodeblockTabs

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "status": { "type": "number" },
    "code": { "type": "string" },
    "message": { "type": "string" }
  },
  "required": [
    "status",
    "code",
    "message"
  ],
  "additionalProperties": false
}
```

:::

<details>

<summary>List of exceptions</summary>

* unknown\_product (400): Provided product was not found
* license\_not\_found (400): License with the provided license user not found
* license\_not\_active (400): The license is not active
* license\_not\_enabled (400): The license is not enabled
* unsupported\_product\_feature (400): One or more of requested product features do not exists or they are not enabled on the product
* feature\_not\_enough\_consumptions (400): Not enough consumptions left
* negative\_consumptions\_not\_allowed (400): Negative consumptions not allowed
* device\_not\_found (400): An active device matching the hardware\_id not found
* blacklisted (400): This device is blacklisted

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.licensespring.com/license-api/consumption/add-feature-consumption.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
