# Callbacks

koja-lib provides a callback system that lets client scripts request data from the server and vice versa.

## TriggerServerCallback

Trigger a callback registered on the server and receive the result asynchronously.

```lua
KOJA.Client.TriggerServerCallback(key, payload, callback)
```

| Parameter  | Type       | Description                       |
| ---------- | ---------- | --------------------------------- |
| `key`      | `string`   | Callback identifier               |
| `payload`  | `any`      | Data sent to the server           |
| `callback` | `function` | Called with the server's response |

**Example**

```lua
KOJA.Client.TriggerServerCallback('myScript:getPlayerData', nil, function(data)
    print('Player name:', data.name)
    print('Money:', data.money)
end)
```

## RegisterClientCallback

Register a callback on the client that the server can trigger.

```lua
KOJA.Client.RegisterClientCallback(key, handler)
```

| Parameter | Type                    | Description                                     |
| --------- | ----------------------- | ----------------------------------------------- |
| `key`     | `string`                | Callback identifier                             |
| `handler` | `function(payload, cb)` | Handler function — call `cb(result)` to respond |

**Example**

```lua
KOJA.Client.RegisterClientCallback('myScript:getLocalPos', function(payload, cb)
    local coords = GetEntityCoords(PlayerPedId())
    cb({ x = coords.x, y = coords.y, z = coords.z })
end)
```

## Registering a Server Callback

On the server side you register the handler:

```lua
KOJA.Server.RegisterServerCallback('myScript:getPlayerData', function(source, payload, cb)
    local name = KOJA.Server.GetPlayerName(source)
    local money = KOJA.Server.getMoney(source, 'cash')
    cb({ name = name, money = money })
end)
```

See [Server Callbacks](/koja-lib/api-reference/server-api/callbacks) for full server-side documentation.

## Related pages

- [Inventory](/koja-lib/api-reference/client-api/inventory) — Client-side inventory functions let you check what items the local player has without a server round-trip.
- [DUI](/koja-lib/api-reference/client-api/dui) — DUI (Dynamic UI) allows you to render a web page as a texture on any in-game surface — screens, billboards, laptops, phones, etc.
- [Money](/koja-lib/api-reference/client-api/money) — Retrieve the local player's money balance from the client side via a server callback.
- [Keybinds](/koja-lib/api-reference/client-api/keybinds) — Register persistent key bindings that players can rebind in the GTA V settings menu.
- [Notifications](/koja-lib/api-reference/client-api/notifications) — koja-lib provides two notification interfaces: SendNotify (routes through the configured backend) and LibNotify (built-in koja-lib notification).
- [Player](/koja-lib/api-reference/client-api/player) — Functions for reading the local player's data on the client side.
- [Client API](/koja-lib/api-reference/client-api) — back to the section overview
