> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tposs/latest/api-reference/rpc/balance_api.md).

# Balance API

TPOSS Balance API allows you to read and manage balances using these RPC methods:

- [Charging.Balance.Add](#chargingbalanceadd) - to provision the new balance
- [Charging.Balance.VolumeUpgrade](#chargingbalancevolumeupgrade) - to upgrade the existing balance
- [Charging.Balance.Delete](#chargingbalancedelete) - to mark the existing balance as deleted
- [Charging.Balance.Read](#chargingbalanceread) - to fetch the existing balance by the given identifier
- [Charging.Balance.List](#chargingbalancelist) - to fetch the list of balances filtered by the given criteria

## Data model

A balance is represented by an object with the following properties:

|Attribute | Type | Description
|---|---|---
| BalanceId | string | Unique identifier for the balance object.
| BalanceDefinitionId | string | Balance type identifier.
| IMSI | string | Unique identifier of a SIM card.
| CarrierGroupId | string | Carrier group identifier.
| ValidFrom | string | Validity range start date provided in the ISO 8601 format. Specifies the date of provisioning of the balance.
| ValidTill | string | Validity range end date provided in the ISO 8601 format. Specifies the end of balance validity.
| ValidityTime | uint64 | Validity of the balance in seconds. Specifies how long the balance is valid in seconds.
| CarrierGroupName | string | Name of the carrier group.
| TotalOctets | uint64 | Amount of traffic in bytes.
| InitialBalanceId | string | Identifier of the parent balance.
| IMEI | string | Optional field which contains unique identifier of a user device.

## API methods

### Charging.Balance.Add

This method allows you to add a balance for the given charging account. If the `ValidityTime` value is present in the incoming request, the `ValidFrom` and `ValidTill` values are calculated based on this value. If `ValidFrom` and `ValidTill` are present, the`ValidityTime` value is calculated based on these values. If all three parameters are present in the incoming request or none of them is given, the error message is returned.

You can pass the following parameters in the request:

| Name | Type | Required / Optional | Description |
|---|---|---|---|
| AuthKey | string | required | Pre-shared authentication key. |
| FeId | string | required | Static frontend identifier which must be unique to the system. |
| FeSId | string | optional | Dynamic frontend session identifier which must be reset upon system startup. |
| ChargingAccountId | string | required | Unique identifier for the charging account. |
| Balance | object | required | The balance object. For more information, see the [data model](#data-model). |

Example of the request:

```bash
curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://127.0.0.1:12000 --data-binary @- <<EOF
{
  "id": "205",
  "jsonrpc": "2.0",
  "method": "Charging.Balance.Add",
  "params": {
    "AuthKey": "secret",
    "FeId": "bss1.<tenant>",
    "FeSId": "13.bss1.<tenant>",
    "ChargingAccountId": "700032310@account.<tenant>",
    "Balance": {
      "BalanceId": "13@balance.charging.<tenant>",
      "BalanceDefinitionId": 3,
      "IMSI": "262019876543210",
      "CarrierGroupId": "27@carriergroup.charging.<tenant>",
      "ValidityTime": 86400,
      "TotalOctets": 52428800,
      "IMEI": "012345678901237"
    }
  }
}
EOF
```

Example of the successful response:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "result": "true"
}
```

In the case of an error, the response contains the `error` object with an error code and message:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "error": {
    "code": "-33333",
    "message": "Not authenticated"
  }
}
```

The following error codes may be returned:

- `-31803` - Either all three validity time parameters are present or none of them.
- `-31804` - There is no charging account with the given account identifier.
- `-31805` - A balance with the given `BalanceId` already exists.
- `-32700` - Can't decode JSON request body.
- `-30000` - Required field is missing.
- `-33333` - Not authenticated.

### Charging.Balance.VolumeUpgrade

You can use this method to extend the allowed volume in the parent balance of the given charging account. TPOSS uses the following parameters to look for a parent balance:

- `ChargingAccountId`
- `IMSI`
- `CarrierGroupId`

The parent balance is referenced by the `BalanceId` attribute within the `Balance` object. If the given charging account identifier is not found in the system, the error is returned.

You can pass the following parameters in the request:

| Name | Type | Required / Optional | Description |
|---|---|---|---|
| AuthKey | string | required | Pre-shared authentication key. |
| FeId | string | required | Static frontend identifier which must be unique to the system. |
| FeSId | string | optional | Dynamic frontend session identifier which must be reset upon system startup. |
| ChargingAccountId | string | required | Unique identifier for the charging account. |
| Balance | object | required | Balance object. For more information, see the [data model](#data-model). |

Example of the request:

```bash
curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://127.0.0.1:12000 --data-binary @- <<EOF
{
  "id": "1",
  "jsonrpc": "2.0",
  "method": "Charging.Balance.VolumeUpgrade",
  "params": {
    "AuthKey": "secret",
    "FeId": "bss1.<tenant>",
    "FeSId": "13.bss1.<tenant>",
    "ChargingAccountId": "700032310@account.charging.<tenant>",
    "Balance": {
      "BalanceId": "13@balance.charging.<tenant>",
      "BalanceDefinitionId": 3,
      "IMSI": "262019876543210",
      "CarrierGroupId": "27@carriergroup.charging.<tenant>",
      "TotalOctets": 52428800,
      "IMEI": "012345678901237"
    }
  }
}
EOF
```

Example of the successful response:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "result": "true"
}
```

In the case of an error, the response contains the `error` object with the error code and message:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "error": {
    "code": "-33333",
    "message": "Not authenticated"
  }
}
```

The following error codes may be returned:

- `-31804` - There is no charging account with the given account identifier.
- `-31805` - A balance with the given `BalanceId` already exists.
- `-31807` - There is no existing balance to which you can add volume.
- `-32700` - Can't decode JSON request body.
- `-30000` - Required field is missing.
- `-33333` - Not authenticated.

### Charging.Balance.Delete

The method allows you to mark the balance associated with the given identifier as a candidate for deletion. The error is returned if the balance with the given identifier is not found.

> [!NOTE]
> The TPOSS CCS subsystem **does not** delete balances from the database based on the API request. Instead, the objects are only marked in the database as `deleted` and not further visible to API consumers. An internal process finally deletes these objects from the database.

You can pass the following parameters in the request:

| Name | Type | Required / Optional | Description |
|---|---|---|---|
| AuthKey | string | required | Pre-shared authentication key. |
| FeId | string | required | Static frontend identifier which must be unique to the system. |
| FeSId | string | optional | Dynamic frontend session identifier which must be reset upon system startup. |
| ChargingAccountId | string | required | Unique identifier for the charging account. |
| BalanceId | string | required | A unique identifier for the balance. |

Example of the successful request:

```bash
curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://127.0.0.1:12000 --data-binary @- <<EOF
{
  "id": "1",
  "jsonrpc": "2.0",
  "method": "Charging.Balance.Delete",
  "params": {
    "AuthKey": "secret",
    "FeId": "bss1.<tenant>",
    "FeSId": "13.bss1.<tenant>",
    "BalanceId": "13@balance.charging.<tenant>"
  }
}
EOF
```

Example of the successful response:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "result": "true"
}
```

In the case of an error, the response contains the `error` object with an error code and message:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "error": {
    "code": "-33333",
    "message": "Not authenticated"
  }
}
```

The following error codes may be returned:

- `-31806` - There is no balance object with the given `BalanceId`.
- `-32700` - Can't decode JSON request body.
- `-30000` - Required field is missing.
- `-33333` - Not authenticated.

### Charging.Balance.Read

The method allows you to fetch the balance associated with the `BalanceId` identifier. If the balance is not found in the system, TPOSS returns an error.

You can pass the following parameters in the request:

| Name | Type | Required / Optional | Description |
|---|---|---|---|
| AuthKey | string | required | Pre-shared authentication key. |
| FeId | string | required | Static frontend identifier which must be unique to the system. |
| FeSId | string | optional | Dynamic frontend session identifier which must be reset upon system startup. |
| BalanceId | string | required | Unique identifier for the balance object. |

Example of the request:

```bash
curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://127.0.0.1:12000 --data-binary @- <<EOF
{
  "id": "1",
  "jsonrpc": "2.0",
  "method": "Charging.Balance.Read",
  "params": {
    "AuthKey": "secret",
    "FeId": "TPUIBrowser",
    "FeSId": "TPUIBrowser",
    "BalanceId": "13@balance.charging.<tenant>"
  }
}
EOF
```

Example of the successful response:

```json
{
 "id": "101",
 "jsonrpc": "2.0",
 "result": {
   "BalanceId": "13@balance.charging.<tenant>",
   "BalanceDefinitionId": 3,
   "IMSI": "262019876543210",
   "CarrierGroupId": "27@carriergroup.charging.<tenant>",
   "ValidityTime": 86400,
   "ValidFrom": "2017-12-01T00:00:00Z",
   "ValidTill": "2017-12-31T23:59:59Z",
   "TotalOctets": 52428800,
   "InitialBalanceId": null
 }
}
```

In the case of an error, the response contains the `error` object with an error code and message:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "error": {
    "code": "-33333",
    "message": "Not authenticated"
  }
}
```

The following error codes may be returned:

- `-31806` - Balance with the given `BalanceId` is not found.
- `-30000` - Required field is missing.
- `-33333` - Not authenticated.
- `-32700` - Can't decode JSON request body.

### Charging.Balance.List

This method is similar to the [Charging.Balance.Read](#chargingbalanceread) API call. The difference is that the `Charging.Balance.List` method allows you to fetch multiple balances at once and apply a filter based on the provided criteria.

You can pass the following parameters in the request:

| Name | Type | Required / Optional | Description |
|---|---|---|---|
| AuthKey | string | required | Pre-shared authentication key. |
| FeId | string | required | Static frontend identifier which must be unique to the system. |
| FeSId | string | optional | Dynamic frontend session identifier which must be reset upon system startup. |
| Filter | string | required | Defines a filter to apply to the results of the query. |

The `Filter` field allows you to filter the balances. The format of the filter is: `FilterKey:FilterOperator:FilterValue`. The `FilterKey` represents the field name of a balance or `ChargingAccountId`. The possible fields are listed in the [data model](#data-model) section. The value of the `FilterOperator` parameter can be one of the following:

- `is` - equal to
- `neq` - not equal to
- `lte` - lower than or equal to
- `gt` - greater than
- `lt` - lower than
- `gte` - greater than or equal to
- `starts_with` - match objects which start with the given pattern
- `ends_with` - match objects which end with the given pattern
- `contains` - match objects which contain the given pattern

The value of the `FilterValue` parameter specifies what you want to filter from the results.

Example of the request:

```bash
curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://127.0.0.1:12000 --data-binary @- <<EOF
{
  "id": "1",
  "jsonrpc": "2.0",
  "method": "Charging.Balance.List",
  "params": {
    "AuthKey": "secret",
    "FeId": "TPUIBrowser",
    "FeSId": "TPUIBrowser",
    "Filter": "BalanceId:starts_with:14"
  }
}
EOF
```

Example of the successful response:

```json
{
  "id": "101",
  "jsonrpc": "2.0",
  "result": {
    "Balances": [
      {
        "BalanceId": "13@balance.charging.<tenant>",
        "BalanceDefinitionId": 3,
        "IMSI": "262019876543210",
        "CarrierGroupId": "27@carriergroup.charging.<tenant>",
        "ValidityTime": 86400,
        "ValidFrom": "2017-12-01T00:00:00Z",
        "ValidTill": "2017-12-31T23:59:59Z",
        "TotalOctets": 52428800,
        "InitialBalanceId": null
      }
    ]
  }
}
```

In the case of an error, the response contains the `error` object with an error code and message:

```json
{
  "id": "1",
  "jsonrpc": "2.0",
  "error": {
    "code": "-33333",
    "message": "Not authenticated"
  }
}
```

The following error codes may be returned:

- `-31411` - Invalid filter.
- `-30000` - Required field is missing.
- `-31810` - Validation error.
- `-33333` - Not authenticated.
- `-32700` - Can't decode JSON request body.
