> 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/server-api/callbacks.md).

# Callbacks

The callback system allows server scripts to handle requests from clients and vice versa.

## RegisterServerCallback

Register a callback handler on the server that clients can trigger.

```lua
KOJA.Server.RegisterServerCallback(key, handler)
```

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

**Example**

```lua
KOJA.Server.RegisterServerCallback('myScript:getShopItems', function(source, payload, cb)
    local items = MySQL.query.await('SELECT * FROM shop_items WHERE category = ?', { payload.category })
    cb(items)
end)
```

**Triggered from the client:**

```lua
KOJA.Client.TriggerServerCallback('myScript:getShopItems', { category = 'food' }, function(items)
    for _, item in ipairs(items) do
        print(item.name, item.price)
    end
end)
```

## TriggerClientCallback

Trigger a callback registered on a specific client from the server.

```lua
KOJA.Server.TriggerClientCallback(key, playerId, payload, callback)
```

| Parameter  | Type            | Description                       |
| ---------- | --------------- | --------------------------------- |
| `key`      | `string`        | Callback identifier               |
| `playerId` | `number`        | Target player server ID           |
| `payload`  | `any`           | Data to send                      |
| `callback` | `function(...)` | Called with the client's response |

**Example**

```lua
KOJA.Server.TriggerClientCallback('myScript:getLocalCoords', source, nil, function(coords)
    print('Player is at:', coords.x, coords.y, coords.z)
end)
```

**Registered on the client:**

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

## TriggerCallback (internal)

Manually trigger an already-registered server callback from another server script.

```lua
KOJA.Server.TriggerCallback(key, source, payload, cb)
```

> This is mainly for internal use. Prefer `RegisterServerCallback` + client triggers for the standard pattern.


---

# 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/server-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.
