> For the complete documentation index, see [llms.txt](https://docs.kojascripts.eu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kojascripts.eu/koja-lib/api-reference/client-api/callbacks.md).

# 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](file:///9510884/server/callbacks.md) for full server-side documentation.


---

# 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.kojascripts.eu/koja-lib/api-reference/client-api/callbacks.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.
