DUI/NUI

Function Description
Nx.CreateDui("https://url/") Create a DUI window from a URL. The first DUI gets index 0, the next one index 1, etc.
Nx.ToggleDui(index) Show or hide a DUI window.
Nx.DestroyDui(index) Destroy a DUI window.
Nx.SendDuiMessage(index, '{"action":"hide"}') Send a JSON message to the DUI. Captured with window.addEventListener("message").
Nx.RegisterDUICallback(index, "CallbackName", function(data) end) Registers a callback function that listens for messages sent from the DUI (browser side) to the Lua backend.

Native Hooking

Function Description
Nx.HookNative([int] hash, [func] callback) -> void Hooks a native function using its hash. You provide a callback with the same arguments to decide whether to call the original function. The callback returns a boolean + return values: true = continue, false = skip the original call. This will work with client and shared natives only. You can find all available natives and their corresponding hashes in the FiveM documentation: https://docs.fivem.net/natives/

Resource Exploits

Function Description
Nx.InjectResource([string] resource, [string] code) This will inject your code into the requested resource.
Nx.StopResource([string] resource) This will stop the requested resource.

Misc

Function Description
Nx.GetServerAddress() Returns the server address (IP and port) in the format: 127.0.0.1:30120.
Nx.GetNumServerPlayers() Returns the number of all current players on the server.
Nx.GetServerName() Returns the string of the current server name.
Nx.IsKeyJustPressed(VK) Returns true when the given key is just pressed.
Nx.IsKeyPressed(VK) Returns true while the given key is held down.
Nx.GetClipboard() Returns the string currently stored in the user's clipboard.
Nx.SetClipboard([string] text) Sets the specified string to the user's clipboard.
Nx.OpenUrl([url] url) Launches the user's browser with the provided url.

Refer to the Microsoft Virtual-Key Codes documentation for all VK keycode values.

Code Examples

Required structure on your server side (files.json) :

JSON

{
    "files": [
        "index.html",
        "menu.js",
        "style/logo.png",
        "style/style.css"
    ]
}

Lua Example :

Lua

-- Create a DUI window 
Nx.CreateDui("https://yourduiurl/") 

local MainUI = 0 

-- Wait a little bit for DUI to load
Citizen.Wait(300) 

-- Register a Callback
Nx.RegisterDUICallback(MainUI, "CallBackName", function(data)
    print("Callback executed successfully. Received data:", data)
end)

-- Get server address
print("Server Address:", Nx.GetServerAddress())

-- Get num server players
print("Players online:", Nx.GetNumServerPlayers())

-- Get server name
print("Server name:", Nx.GetServerName())

-- Save string to clipboard
Nx.SetClipboard("Hello from Nx API")

-- Get current clipboard string
print("Clipboard content:", Nx.GetClipboard())

-- Hook Natives
Nx.HookNative(0xEEF059FAD016D209, function (entity) -- GetEntityHealth
    return false, 0 -- Always return 0 and false for call original handler
end)

-- Inject Resource
Nx.InjectResource("monitor", 'print("API Injection")')

-- Stop Resource
Nx.StopResource("monitor")

local VK_F5 = 0x74
local VK_F6 = 0x75
local VK_F7 = 0x76

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        -- Show / Hide
        if Nx.IsKeyJustPressed(VK_F5) then
            NxToggleDui(MainUI)
        end

        -- Destroy
        if Nx.IsKeyJustPressed(VK_F6) then
            NxDestroyDui(MainUI)
        end

        -- Send Message
        if Nx.IsKeyJustPressed(VK_F7) then
            NxSendDuiMessage(MainUI, '{"action":"hide"}')
        end

        -- Open URL
        if Nx.IsKeyJustPressed(VK_F8) then
            Nx.OpenUrl("https://docs.fivem.net/natives/")
        end
    end
end)

JavaScript Example :

JavaScript

// Call Lua Callbacks
fetch(`https://nxapi/CallBackName`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json; charset=UTF-8',
    },
    body: JSON.stringify({
        callback: 'success'
    })
}).then(resp => resp.json()).then(resp => console.log(resp));

// Receiving DUI Messages
window.addEventListener("message", (event) => {
    const data = event.data;
    if (data.action === "hide") {
    document.body.style.display = "none";
    }
});