# Read client spending by period

Source: [Read client spending by period](<https://docs.gptunnel.ru/en/business/balance-spending/>)

<a id="_top"></a>
> **Note: Partner organization key**
>
> Use the partner organization’s API key. The organization must have access to the partner endpoints. See the [Business API overview](<https://docs.gptunnel.ru/en/business/#partner-clients>) for client eligibility and how to enable access.

<a id="expenditure-during-the-period"></a>
## Expenditure during the period

`POST https://gptunnel.ru/api/v1/balance/spending`

Returns client spending in rubles for the requested period, grouped by day or month. Identify the client using exactly one of the following fields.

- `user_id` includes the user’s spending from their personal account and from the accounts of the partner’s client organizations where they work.
- `org_id` includes spending by all employees from the specified organization’s account.
- `user_data.phone` selects a user’s personal account by phone number in the format `+79XXXXXXXXX`. Phone lookup must be enabled for the partner.

Spending includes charges for model requests and other paid features, minus refunds for failed generations. A refund can arrive on a different day from the charge, so the total for a period can be negative. Top-ups, transfers, and refunds of transfers are excluded.

This read-only operation does not require an `Idempotency-Key` header.

### Request headers

| Parameter | Type | Description |
| --- | --- | --- |
| `Authorization` (required) | `string` | Organization API key |
| `Content-Type` (required) | `string` | application/json |

### Request parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `user_id` | `string` | Client user ID, 24 lowercase hexadecimal characters. Provide exactly one of user\_id, org\_id, and user\_data. |
| `org_id` | `string` | Client organization ID, 24 lowercase hexadecimal characters. Provide exactly one of user\_id, org\_id, and user\_data. |
| `user_data` | `object` | Look up a user by phone: {"phone":"+79XXXXXXXXX"}. This feature requires separate activation. |
| `date_from` (required) | `string` | First day of the period, in YYYY-MM-DD format (inclusive) |
| `date_to` (required) | `string` | Last day of the period, in YYYY-MM-DD format (inclusive). Must be on or after date\_from. |
| `group_by` | `string` | day (default) or month |
| `by_user` | `boolean` | Set to true for an employee breakdown in each period. Requires org\_id. |

Day and month boundaries use Moscow time. The maximum date range is 92 days with `group_by: "day"` and 366 days with `group_by: "month"`.

<a id="example-request"></a>
### Example request

Set `GPTUNNEL_API_KEY` and replace the placeholders with values for your organization.

<a id="tab-panel-0-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

key = os.environ.get("GPTUNNEL_API_KEY")
if not key:
    raise SystemExit("Set GPTUNNEL_API_KEY")

body = {
  "user_id": "6ad72133269dc8d9f75a33bc",
  "date_from": "2026-11-01",
  "date_to": "2026-11-02"
}
request = Request(
    "https://gptunnel.ru/api/v1/balance/spending",
    method="POST",
    headers={
        "Authorization": key,
        "Content-Type": "application/json",
    },
    data=json.dumps(body).encode("utf-8"),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-0-1"></a>
**cURL**

```bash
curl --request POST \
  --url 'https://gptunnel.ru/api/v1/balance/spending' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{
  "user_id": "6ad72133269dc8d9f75a33bc",
  "date_from": "2026-11-01",
  "date_to": "2026-11-02"
}'
```

<a id="tab-panel-0-2"></a>
**JavaScript (Node.js)**

```javascript
const key = process.env.GPTUNNEL_API_KEY
if (!key) throw new Error("Set GPTUNNEL_API_KEY")

const body = {
  "user_id": "6ad72133269dc8d9f75a33bc",
  "date_from": "2026-11-01",
  "date_to": "2026-11-02"
}
const response = await fetch("https://gptunnel.ru/api/v1/balance/spending", {
  method: "POST",
  headers: {
    Authorization: key,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

<a id="response"></a>
### Response

### Response fields

| Parameter | Type | Description |
| --- | --- | --- |
| `code` | `number` | 0 on success |
| `type` | `string` | user or org - for which the report is built |
| `id` | `string` | User or organization ID |
| `date_from` | `string` | Start of period from request |
| `date_to` | `string` | End of period from request |
| `group_by` | `string` | day or month |
| `currency` | `string` | Always RUB |
| `periods` | `array` | One entry per day or month in chronological order. Periods without spending are included with zero amounts. |
| `summary` | `object` | Totals for the entire date range, with the same fields as each periods entry except period and users. |

### One period (periods)

| Parameter | Type | Description |
| --- | --- | --- |
| `period` | `string` | YYYY-MM-DD for the day group, YYYY-MM by month |
| `amount` | `number` | Spent rubles. Negative if returns are higher than write-offs during the period |
| `bonus` | `number` | Bonus points spent. One point is worth 0.5 RUB. |
| `total` | `number` | Total in rubles, including amount and the ruble value of bonus points spent |
| `personal` | `object` | For user only: personal account expenditures - amount, bonus, total |
| `orgs` | `array` | Returned only for a user. Spending from organization accounts includes org\_id, amount, bonus, and total. Sorted by total in descending order. |
| `users` | `array` | Returned only with org\_id and by\_user: true. For each employee it includes user\_id, amount, bonus, and total. Sorted by total in descending order. |

With monthly grouping, a partial month includes only the days from `date_from` through `date_to`, inclusive.

<a id="tab-panel-1-0"></a>
**200 Users**

```json
{
  "code": 0,
  "type": "user",
  "id": "6ad72133269dc8d9f75a33bc",
  "date_from": "2026-11-01",
  "date_to": "2026-11-02",
  "group_by": "day",
  "currency": "RUB",
  "periods": [
    {
      "period": "2026-11-01",
      "amount": 86.4,
      "bonus": 0,
      "total": 86.4,
      "personal": { "amount": 36.4, "bonus": 0, "total": 36.4 },
      "orgs": [
        { "org_id": "6ad7a4f2269dc8d9f75a41d0", "amount": 50, "bonus": 0, "total": 50 }
      ]
    },
    {
      "period": "2026-11-02",
      "amount": 0,
      "bonus": 0,
      "total": 0,
      "personal": { "amount": 0, "bonus": 0, "total": 0 },
      "orgs": []
    }
  ],
  "summary": {
    "amount": 86.4,
    "bonus": 0,
    "total": 86.4,
    "personal": { "amount": 36.4, "bonus": 0, "total": 36.4 },
    "orgs": [
      { "org_id": "6ad7a4f2269dc8d9f75a41d0", "amount": 50, "bonus": 0, "total": 50 }
    ]
  }
}
```

<a id="tab-panel-1-1"></a>
**200 Staff organization**

```json
{
  "code": 0,
  "type": "org",
  "id": "6ad7a4f2269dc8d9f75a41d0",
  "date_from": "2026-10-01",
  "date_to": "2026-10-31",
  "group_by": "month",
  "currency": "RUB",
  "periods": [
    {
      "period": "2026-10",
      "amount": 1240.75,
      "bonus": 0,
      "total": 1240.75,
      "users": [
        { "user_id": "6ad72133269dc8d9f75a33bc", "amount": 900.5, "bonus": 0, "total": 900.5 },
        { "user_id": "6ad8c0be269dc8d9f75a52e1", "amount": 340.25, "bonus": 0, "total": 340.25 }
      ]
    }
  ],
  "summary": { "amount": 1240.75, "bonus": 0, "total": 1240.75 }
}
```

<a id="errors"></a>
### Errors

| HTTP | code | When |
| --- | --- | --- |
| 400 | 3 | Identify exactly one client and use only supported fields. An ID must contain exactly 24 characters from `0-9a-f`. Dates must exist and use `YYYY-MM-DD`. The period must be within the limit, and `date_from` cannot be later than `date_to`. Using `by_user: true` requires `org_id`. See `details`. |
| 403 | 13 | The key belongs to a personal account rather than an organization |
| 403 | 27 | Phone lookup with `user_data` is disabled for the organization. |
| 403 | 40 | This endpoint is not enabled for the organization that owns the key. |
| 404 | 17 | User not found, deleted or not a customer of partner |
| 404 | 24 | Organization not found, deleted or not a customer of partner |

The API checks the key’s permissions before validating the request body. A key without access receives HTTP `403` even if the body is invalid.

See the [Business API error reference](<https://docs.gptunnel.ru/en/business/errors/>) for all error codes, including the `401` response for an invalid key.
