# Freeze or unfreeze a client balance

Source: [Freeze or unfreeze a client balance](<https://docs.gptunnel.ru/en/business/balance-spend-block/>)

<a id="_top"></a>
> **Note: Partner organization key**
>
> Use the API key of the partner organization. The [Business API overview](<https://docs.gptunnel.ru/en/business/#partner-clients>) explains which accounts qualify as clients and how to enable these endpoints.

> **Caution: Check endpoint availability**
>
> Before integrating, ask support whether this endpoint is available for your connection. HTTP `404` with `code: 12` means the server does not support this endpoint. It tells you nothing about the account’s freeze status. HTTP `403` with `code: 47` means the server supports the endpoint but the organization that owns the key does not have access.

<a id="freezing-and-unfreezing"></a>
## Freezing and unfreezing

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

Blocks or restores spending on a client account. For example, you can freeze spending while you process a contract termination or refund.

Specify exactly one field. Use `user_id` or `user_data` for a personal account, or `org_id` for an organization account. The freeze applies only to the specified account. Personal and organization accounts can be frozen independently.

While the account is frozen, the following rules apply.

- **New charges are blocked.** GPTunneL rejects new generations in chat, Creative Lab, and the API, as well as outgoing transfers. The client sees a message that spending is blocked.
- **Top-ups and refunds still work.** You can [refund funds](<https://docs.gptunnel.ru/en/business/balance-refund/>) from a frozen account to recover the remaining balance.
- **Generations in progress are charged.** If a response is already being generated when the freeze takes effect, it completes and the account is charged.
- **Spending history remains available.** You can still retrieve [client spending](<https://docs.gptunnel.ru/en/business/balance-spending/>), including for past periods.
- **Clients retain dashboard access.** They can sign in and view their chats, generations, and history.

Repeating a request with the same `blocked` value succeeds with `changed: false`. To read the current state, check `spend_blocked` in the response from [`balance/get`](<https://docs.gptunnel.ru/en/business/balance-get/>) or [`orgs/list`](<https://docs.gptunnel.ru/en/business/orgs-list/>).

### 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. |
| `blocked` (required) | `boolean` | Set true to freeze the account or false to unfreeze it. |

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

Set `GPTUNNEL_API_KEY` and replace `user_id` with the ID of your test client.

<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",
  "blocked": True
}
request = Request(
    "https://gptunnel.ru/api/v1/balance/setSpendBlocked",
    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/setSpendBlocked' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{
  "user_id": "6ad72133269dc8d9f75a33bc",
  "blocked": true
}'
```

<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",
  "blocked": true
}
const response = await fetch("https://gptunnel.ru/api/v1/balance/setSpendBlocked", {
  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 is a personal account of the user, org is an organization account |
| `id` | `string` | User or organization ID |
| `spend_blocked` | `boolean` | Account status after request: true — frozen |
| `changed` | `boolean` | false, if the account was already in this state and the request didn't change anything |

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

```json
{
  "code": 0,
  "type": "user",
  "id": "6ad72133269dc8d9f75a33bc",
  "spend_blocked": true,
  "changed": true
}
```

<a id="tab-panel-1-1"></a>
**404 Not your client**

```json
{
  "code": 17,
  "message": "Recipient not found."
}
```

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

| HTTP | code | When |
| --- | --- | --- |
| 400 | 3 | The request must identify exactly one client, include `blocked`, and contain only supported fields. An ID must contain exactly 24 characters from `0-9a-f`. 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 | 47 | This endpoint is not enabled for the organization that owns the key. |
| 404 | 12 | The server does not support this endpoint. Ask support about availability. |
| 404 | 17 | User not found, deleted or not a customer of partner |
| 404 | 24 | Organization not found, deleted or not a customer of partner |

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.
