# Adjust a balance source

Operation / SDK: `adjustBalance` · `adjust_balance` · `AdjustBalance`

Add, subtract, set or edit a balance source guarded by its expected available amount.

Last updated: 2026-11-05

[Web version](https://www.longplayai.com/docs/api/adjust-balance)

<a id="request"></a>

## Request

- **Credential**: Server Key with `credits:manage`. Call from your backend only.
- **SDK**: JavaScript `createLongplayServer().adjustBalance` · Python `adjust_balance` · Go `AdjustBalance`
- **Success**: HTTP 200

The response shows example data.

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/balances/source/adjust" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "command": {
    "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
    "sourceId": "31b22d42-ce50-47ea-8da0-553fd0a8a869",
    "action": "add",
    "amount": "5",
    "expectedAvailable": "10"
  }
}'
```

**TypeScript: adjustBalance.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function adjustBalance() {
  return longplay.adjustBalance({
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
    command: {
      requestId: "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
      sourceId: "31b22d42-ce50-47ea-8da0-553fd0a8a869",
      action: "add",
      amount: "5",
      expectedAvailable: "10",
    },
  });
}
```

**Request body: request.json**

```json
{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "command": {
    "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
    "sourceId": "31b22d42-ce50-47ea-8da0-553fd0a8a869",
    "action": "add",
    "amount": "5",
    "expectedAvailable": "10"
  }
}
```

**Response: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "balance": {
    "sourceId": "31b22d42-ce50-47ea-8da0-553fd0a8a869",
    "available": "1",
    "held": "1",
    "spent": "1",
    "adjustmentGrantId": "160ae076-cc5c-44dc-84c1-6fa724cc1c21"
  }
}
```

<a id="parameters"></a>

## Body parameters

**application/json**

- `customerId` (uuid, required): The Customer's UUID.
- `command` (object | object, required): The command to run; its `action` or `operation` selects the other fields.
- `command.requestId` (uuid | uuid, required): Business request UUID. Reuse for retries of one operation; generate a new value for a new operation.
- `command.sourceId` (uuid | uuid, required): balance source ID.
- `command.expectedAvailable` (string | string, required): The source's `available` amount from your last read, as a decimal string; if it has changed, the request fails with `CONFLICT` and you should read the source again.
- `command.action` (enum | "edit", required): Business action. Target fields and quantity requirements vary by action; see the request structure and example.
- `command.amount` (string, optional): Credit or ledger amount, using the field’s decimal-string or integer type. pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `command.granted` (string, optional): Granted quantity. pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `command.expiresAt` (date-time | null, optional): Expiry time (ISO 8601, UTC).
- `command.nextResetAt` (date-time | null, optional): Timestamp (ISO 8601).
- `command.expectedRevision` (integer, optional): The current revision; prevents overwriting other changes. min: 0 · max: 9007199254740991

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

## Response fields

Every successful response includes `requestId` (this request's ID, for support) and `scope` (App and environment).

**HTTP 200 · application/json**

- `requestId` (uuid, required): UUID the server generated for this HTTP request; include it when reporting a problem. It is not your business requestId.
- `scope` (object, required): The App and environment of this response. Headers only verify scope; they cannot switch key scope.
- `scope.projectId` (string, required): App ID bound to the credential. minLength: 1
- `scope.environment` (enum, required): Separate data scope: sandbox or live. Values: `"sandbox"`, `"live"`.
- `balance` (object, required): Balance.
- `balance.sourceId` (uuid, required): balance source ID.
- `balance.available` (string, required): Available quantity. pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `balance.held` (string, required): Holdings. pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `balance.spent` (string, required): Spent quantity. pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `balance.adjustmentGrantId` (uuid | null, required): Always `null`; adjustments change the source directly.

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

## Errors

Failures return an error object; handle them by `error.code` and `error.recovery`. The table lists the errors that follow from this operation's credential, scope and idempotency fields; depending on the request it can also return `NOT_FOUND` (a referenced resource doesn't exist), `NOT_CONFIGURED` (a required integration isn't connected) or `PAYLOAD_TOO_LARGE`. Some refusals, such as insufficient balance, are not errors but a `reason` in a successful response.

**Possible errors**

| code | HTTP | When | recovery |
| --- | --- | --- | --- |
| `INVALID_REQUEST` | 400 | The request doesn't match this page's parameters. A missing or malformed protocol header returns `UNSUPPORTED_PROTOCOL_VERSION` or `INVALID_PROTOCOL_VERSION` instead. | `fix_request` |
| `UNAUTHENTICATED` | 401 | The credential is missing, invalid, expired or revoked. | `authenticate` |
| `FORBIDDEN` | 403 | The credential lacks `credits:manage`. | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` or `X-Indiana-Environment` doesn't match the credential's App and environment. | `check_scope` |
| `CONFLICT` | 409 | The same `requestId` was sent with different input (`reason: IDEMPOTENCY_CONFLICT`). | `resolve_conflict` |
| `RATE_LIMITED` | 429 | Too many requests; wait for `Retry-After` and retry the same request. | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | The service is temporarily unavailable; retry the same request. | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | The outcome is unknown; read the original operation or replay the original ID and input. | `recover_operation` |

- [Error codes and recovery](https://www.longplayai.com/docs/api/errors.md)
- [Request conventions](https://www.longplayai.com/docs/api.md)
- [All operations](https://www.longplayai.com/docs/api/operations.md#credits)
