# Refund a client balance

Source: [Refund a client balance](<https://docs.gptunnel.ru/en/business/balance-refund/>)

<a id="_top"></a>
> **Note: Partner organization key**
>
> Use the partner organization’s API key with Transfers enabled, the same key you use for [transfers](<https://docs.gptunnel.ru/en/business/transfer/>). See the [Business API overview](<https://docs.gptunnel.ru/en/business/#partner-clients>) for client eligibility and endpoint access.

<a id="refunds"></a>
## Refunds

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

Returns funds from a client account to the balance of the organization that owns the API key. Identify the source account using exactly one field. Use `user_id` for a personal account, `org_id` for an organization, or `user_data.phone` to find a personal account by phone number. Phone lookup must be enabled for the partner.

Refund rules

- **Original transfers.** You can refund only the portion of your transfers to this client that has not already been refunded. Bonus points cannot be refunded.
- **Current balance.** A refund cannot exceed the client’s current balance. If the client spends part of a transfer and later tops up the account, the top-up counts toward the balance available for a refund. The original transfer limit still applies.
- **Frozen accounts.** You can refund a balance from an account [frozen](<https://docs.gptunnel.ru/en/business/balance-spend-block/>) by the partner or GPTunneL. This lets you recover the remaining funds when a contract ends.
- **Oldest transfers first.** A refund reduces the outstanding amounts on your transfers to this client, starting with the oldest. The operation is atomic. If it fails, none of the transfers change.

Send exactly one of `amount` or `refund_all_balance: true`. A full refund returns the smaller of the customer’s balance and the total amount from transfers that has yet to be refunded. The 100,000 ₽ ceiling applies only to `amount`.

### Request headers

| Parameter | Type | Description |
| --- | --- | --- |
| `Authorization` (required) | `string` | Organization API key |
| `Idempotency-Key` (required) | `string` | Unique key for this refund, 8–60 characters from A-Za-z0-9\_- |
| `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. |
| `amount` | `number` | Amount in RUB, greater than 0 and at most 100,000, with at most two decimal places. Provide exactly one of amount or refund\_all\_balance: true. |
| `refund_all_balance` | `boolean` | Set to true to refund the full eligible balance. The 100,000 RUB limit does not apply. Provide exactly one of amount or refund\_all\_balance: true. |

> **Caution: Retries return the original refund amount**
>
> If you retry with the same idempotency key and request body, the API returns the original result with `replayed: true` without debiting the client again. This also applies to `refund_all_balance: true`. A retry does not recalculate the eligible balance. Use a new key for a new refund.

<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",
  "amount": 300
}
request = Request(
    "https://gptunnel.ru/api/v1/balance/refund",
    method="POST",
    headers={
        "Authorization": key,
        "Content-Type": "application/json",
        "Idempotency-Key": "refund-2026-11-06-000041",
    },
    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/refund' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --header 'Idempotency-Key: refund-2026-11-06-000041' \
  --data '{
  "user_id": "6ad72133269dc8d9f75a33bc",
  "amount": 300
}'
```

<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",
  "amount": 300
}
const response = await fetch("https://gptunnel.ru/api/v1/balance/refund", {
  method: "POST",
  headers: {
    Authorization: key,
    "Content-Type": "application/json",
    "Idempotency-Key": "refund-2026-11-06-000041",
  },
  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 |
| `transaction_id` | `string` | The Idempotency-Key from your request |
| `org_id` | `string` | The organization to which the money returned |
| `client_user_id` | `string \| null` | User from whose personal account the return is made |
| `client_org_id` | `string \| null` | The organization from which the return is made |
| `amount` | `number` | Amount debited from the client account in rubles. With refund\_all\_balance, the actual amount can exceed 100,000. |
| `discount_reverted` | `number` | Amount of the partner discount reversed for this refund. Zero if the transfers had no discount. |
| `net_amount` | `number` | Net credit to the organization’s balance, equal to amount minus discount\_reverted |
| `currency` | `string` | Always RUB |
| `created_at` | `string` | Operating time, ISO 8601 |
| `replayed` | `boolean` | true if this request repeats a refund with the same Idempotency-Key |

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

```json
{
  "code": 0,
  "transaction_id": "refund-2026-11-06-000041",
  "org_id": "6ad5e0c3269dc8d9f75a1f07",
  "client_user_id": "6ad72133269dc8d9f75a33bc",
  "client_org_id": null,
  "amount": 300,
  "discount_reverted": 0,
  "net_amount": 300,
  "currency": "RUB",
  "created_at": "2026-11-06T08:21:07.000Z",
  "replayed": false
}
```

<a id="tab-panel-1-1"></a>
**422 client is short of funds**

```json
{
  "code": 37,
  "message": "Insufficient funds on the client balance."
}
```

<a id="tab-panel-1-2"></a>
**422 More than translated**

```json
{
  "code": 38,
  "message": "Amount exceeds the unreturned transfers to this client."
}
```

<a id="partnership-discount"></a>
### Partnership discount

If the original transfer included a [partner discount](<https://docs.gptunnel.ru/en/business/transfer/#partnership-discount>), the refund reverses the discount in proportion to the amount refunded, using the rate recorded for each transfer. The organization receives `amount`. A separate transaction deducts `discount_reverted`, leaving a net credit of `net_amount`.

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

| HTTP | code | When |
| --- | --- | --- |
| 400 | 3 | The request must identify exactly one client, use only supported fields, and specify either `amount` or `refund_all_balance: true`. An ID must contain exactly 24 characters from `0-9a-f`, and the amount must be within the allowed range. See `details`. |
| 400 | 21 | The `Idempotency-Key` header is missing or invalid. It must contain 8–60 characters from `A-Za-z0-9_-`. |
| 403 | 13 | The key belongs to a personal account. This endpoint requires an organization key. |
| 403 | 22 | Transfers are disabled on the key. |
| 403 | 27 | Phone lookup with `user_data` is disabled for the organization. |
| 403 | 35 | The refund endpoint is not enabled for the organization that owns the key, transfers are disabled for the organization, or a distributor manages it. |
| 404 | 17 | The user does not exist, has been deleted, or is not a client of this partner. |
| 404 | 24 | The organization does not exist, has been deleted, or is not a client of this partner. |
| 409 | 20 | The `Idempotency-Key` has already been used for a different client or `amount`. |
| 409 | 39 | Another operation is in progress for this organization. Retry with the same `Idempotency-Key`. |
| 422 | 37 | The client’s balance is below the requested amount. With `refund_all_balance`, this means there are no eligible funds to refund. |
| 422 | 38 | The requested `amount` exceeds the portion of your transfers to this client that has not yet been refunded. |

The API checks the key’s permissions first, then the `Idempotency-Key` header, and finally the request body. A key without access receives HTTP `403` even when the header is missing.

If the client is deleted while the refund is being processed, the response is HTTP `404` with code `23` for a user or `25` for an organization. If the organization’s current owner is not your client, the API returns HTTP `404` with code `24`. You cannot use this endpoint to recover funds from that organization, even if you previously transferred funds to it.

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.
