# registerNuiCallback (Client)

> Registers a function the calling resource's interface page calls with a `fetch` to `/name` on its own address.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Client
- **Category**: Web interface
- **URL**: https://wiki.mymp.gg/en/registerNuiCallback

## Syntax

```lua
bool registerNuiCallback(string name, function handler)
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `name` | `string` | The callback's name, the path the page requests without the `/`. |
| `handler` | `function` | Function called with `(data, cb)`: `data` is the request's body read as JSON (an empty table if it is empty or invalid) and `cb(response)` answers the page with the table as JSON. |

## Returns

`true` if it was registered. `false` if the resource shows no page (logs a warning).

It only works if the resource declares a page with `<ui src="...">` in its `meta.xml`. Registering the same name again replaces the function.

## Example

```lua
registerNuiCallback("buy", function(data, cb)
    triggerServerEvent("shop:buy", localPlayer, data.item)
    cb({ ok = true })
end)

-- in the page: fetch("/buy", { method: "POST", body: JSON.stringify({ item: "pizza" }) })
```

### Note

If `cb` is not called within 30 seconds, the page gets `{}`.

## See also

- [sendNuiMessage (Client)](https://wiki.mymp.gg/en/sendNuiMessage.md): Sends a message to the interface page of the calling resource. The page receives it in the window's message event.
- [setNuiFocus (Client)](https://wiki.mymp.gg/en/setNuiFocus.md): Gives or takes focus from the calling resource's interface page. With focus, the page gets the mouse and the keyboard.
- [triggerServerEvent (Client)](https://wiki.mymp.gg/en/triggerServerEvent.md): Sends an event to the server, which triggers it on source there. The function returns before the server receives it.
