Skip to content

ApiBridge ​

Source: shiva-core/server/sv_api_bridge.lua

Server-side global that connects the FiveM server to the external shiva-api HTTP service. Events emitted via ApiBridge.relay() are queued and flushed in batches.

Availability ​

ApiBridge is a server-only global. It is available in all server-side shiva module scripts automatically. Client scripts do not have access to it.

When api.endpoint is not configured in config/api.lua, ApiBridge silently no-ops — all calls are safe to make even without shiva-api installed.

Configuration ​

shiva-core/config/api.lua:

lua
return {
    endpoint          = '',       -- shiva-api base URL, e.g. 'http://localhost:4000'
    secret            = '',       -- shared HMAC secret
    flushInterval     = 2000,     -- ms between batch flushes (default: 2000)
    maxQueueSize      = 200,      -- max queued events before forced flush (default: 200)
    heartbeatInterval = 30000,    -- ms between heartbeat pings (default: 30000)
    relay = {
        events = {},              -- event names to auto-relay from EventBus
    },
}

Methods ​

ApiBridge.connect() ​

Establish the connection and start the flush loop. Called automatically by shiva-core on boot — you don't need to call this manually.

lua
ApiBridge.connect()

ApiBridge.relay(event, payload) ​

Queue an event to be sent to shiva-api.

ParameterTypeDescription
eventstringEvent name
payloadtablePayload (must be JSON-serialisable)
lua
ApiBridge.relay('player:moneyChanged', {
    playerId = source,
    amount   = 500,
    type     = 'cash',
})

Events are not sent immediately — they are batched and flushed every flushInterval milliseconds.

ApiBridge.disconnect() ​

Stop the flush loop and close the connection. Called automatically on resource stop.

lua
ApiBridge.disconnect()

ApiBridge.isReachable() ​

Returns true if the last heartbeat to shiva-api succeeded.

lua
if ApiBridge.isReachable() then
    ApiBridge.relay('audit:action', payload)
end

ApiBridge.queueDepth() ​

Returns the number of events currently waiting in the queue.

lua
Log.info('api', 'queue depth', { depth = ApiBridge.queueDepth() })

Auto-relay via EventBus ​

Set relay.events in the config to automatically forward EventBus events to shiva-api without any extra code:

lua
relay = {
    events = {
        'player:moneyChanged',
        'player:jobChanged',
        'economy:transaction',
    },
},

Any time one of those events fires on the EventBus, ApiBridge will queue it automatically.

Example ​

lua
-- In any server-side service
RegisterCommand('give', function(source, args)
    local amount = tonumber(args[1]) or 0
    Economy:give(source, 'cash', amount)

    ApiBridge.relay('economy:giveCommand', {
        admin    = GetPlayerName(source),
        amount   = amount,
        serverId = source,
    })
end)

Released under the MIT License.