> For the complete documentation index, see [llms.txt](https://docs.lingoql.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lingoql.com/sub0/apis-abi/practical-examples/wallet-backend-transactions-and-balances.md).

# Wallet backend: Transactions and balances

Use this page when the wallet starts moving real money.

### Best for

Balance updates, transaction history, and hot read endpoints.

### What it shows

* Action chaining inside one endpoint
* Extracting a prior result into a later write
* Endpoint-level caching for repeated reads

### Use this when

You want a production-shaped money flow without custom controller code.

### Pattern 1. Apply a fulfilled transaction to the user balance

This is the flagship wallet pattern on this page.

```json
{
  "id": "wallet-apply-transaction-01",
  "resource": "apply-fulfilled-transaction",
  "tokenize": {
    "type": "JWT",
    "algorithm": "HS256",
    "encryption_key": "$ENV.JWT_SECRET_KEY"
  },
  "protected": {
    "provide_as": "x-access-token",
    "extract_claims": ["id"]
  },
  "actionables": [
    {
      "id": 1,
      "mode": "QUERY",
      "no_op": true,
      "sql_query": {
        "query": "SELECT amount FROM transactions WHERE user_id = $1 AND status = 'FULFILLED' ORDER BY created_at DESC LIMIT 1",
        "parameters": ["$PROTECTED.id"]
      },
      "returnables": ["amount"]
    },
    {
      "id": 2,
      "mode": "QUERY",
      "depends_on": [
        {
          "resource_id": "self",
          "action_ids": [1],
          "extract_response_for": [
            {
              "extract": "amount",
              "for": "add_bal"
            }
          ]
        }
      ],
      "sql_query": {
        "query": "UPDATE users SET wallet_balance = wallet_balance + $1::float8, updated_at = $2::timestamptz WHERE id = $3 RETURNING wallet_balance AS user_wallet_balance",
        "parameters": ["add_bal", "$DATETIME", "$PROTECTED.id"]
      },
      "returnables": ["user_wallet_balance"],
      "main_returnable": true
    }
  ]
}
```

### Pattern 2. Cache recent transaction history

Use this when the same user opens the wallet screen often.

```json
{
  "id": "wallet-recent-transactions-01",
  "resource": "recent-transactions",
  "tokenize": {
    "type": "JWT",
    "algorithm": "HS256",
    "encryption_key": "$ENV.JWT_SECRET_KEY"
  },
  "protected": {
    "provide_as": "x-access-token",
    "extract_claims": ["id"]
  },
  "cacheable": {
    "read_from_cache": true,
    "cache_key": "recent_transactions_$PROTECTED.id",
    "duration": 60
  },
  "actionables": [
    {
      "id": 1,
      "mode": "QUERY",
      "sql_query": {
        "query": "SELECT id, amount, status, created_at::text FROM transactions WHERE user_id = $1 AND deleted_at IS NULL ORDER BY created_at DESC LIMIT 20",
        "parameters": ["$PROTECTED.id"]
      },
      "returnables": ["id", "amount", "status", "created_at"]
    }
  ]
}
```

<details>

<summary>MongoDB variant for the balance update</summary>

```json
{
  "id": "wallet-apply-transaction-mongo-01",
  "resource": "apply-fulfilled-transaction",
  "tokenize": {
    "type": "JWT",
    "algorithm": "HS256",
    "encryption_key": "$ENV.JWT_SECRET_KEY"
  },
  "protected": {
    "provide_as": "x-access-token",
    "extract_claims": ["id"]
  },
  "actionables": [
    {
      "id": 1,
      "mode": "QUERY",
      "no_op": true,
      "mongo_query": {
        "collection": "transactions",
        "action": "FETCH",
        "filter_predicate": {
          "user_id": "$PROTECTED.id",
          "status": "FULFILLED"
        }
      },
      "returnables": ["amount"]
    },
    {
      "id": 2,
      "mode": "QUERY",
      "depends_on": [
        {
          "resource_id": "self",
          "action_ids": [1],
          "extract_response_for": [
            {
              "extract": "amount",
              "for": "wallet_balance"
            }
          ]
        }
      ],
      "mongo_query": {
        "collection": "users",
        "action": "UPDATE",
        "filter_predicate": {
          "_id": "$PROTECTED.id"
        },
        "update_predicate": {
          "$inc": {
            "wallet_balance": "$PAYLOAD.wallet_balance"
          }
        },
        "with_timestamp": true
      },
      "returnables": ["wallet_balance"],
      "main_returnable": true
    }
  ]
}
```

</details>

When you need more execution detail, use [Action Chaining](/sub0/apis-abi/action-chaining.md) and [Caching](/sub0/apis-abi/caching.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lingoql.com/sub0/apis-abi/practical-examples/wallet-backend-transactions-and-balances.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
