Skip to main content

API Reference

Complete reference for the Driverforge SDK.


Driverforge:Init()​

Initialize the SDK. Call this in OnDriverInit before anything else.

Driverforge:Init(token, opts?)

Parameters​

ParameterTypeRequiredDescription
tokenstringYesYour project's project token (drv_…)
optstableNoConfiguration options

The driver's filename is not a parameter. The SDK reads it from C4:GetDriverFileName() itself, so the name it reports always matches the file actually running.

Options​

OptionTypeDefaultDescription
loggertable—Your existing logger instance (see Automatic Logs)
logMaptable—Maps canonical levels to your logger's method names

logMap​

For each canonical level (fatal, error, warn, info, debug, trace), Anvil probes your logger for a method matching common case variants in this order: lowercase → PascalCase → UPPERCASE. The first match is wrapped. So a logger with info/Info/INFO style methods works without any logMap.

logMap is only needed when a method has a genuinely renamed counterpart: typically when the canonical level word doesn't appear in the method name at all. For example, a logger that uses Alert for the fatal level:

Driverforge:Init("YOUR_API_KEY", {
logger = Log,
logMap = {
fatal = "Alert", -- only the semantic mismatch needs an entry
}
})

The keys are Anvil's canonical levels (always lowercase), the values are the method names on your logger. Only include levels where the method name doesn't match any case variant of the canonical word.

Example​

function OnDriverInit(strDIR)
require('vendor.driverforge-sdk')
Driverforge:Init("YOUR_API_KEY")
end

Anvil:SetTimer()​

Create a timer with automatic error capture. Drop-in replacement for C4:SetTimer.

Anvil:SetTimer(duration, callback, ...)

Parameters​

ParameterTypeRequiredDescription
durationnumberYesMilliseconds until callback fires
callbackfunctionYesFunction to call
...anyNoAdditional args for C4:SetTimer

Returns​

The timer object from C4:SetTimer.

Example​

-- One-shot timer
Anvil:SetTimer(5000, function(timer)
RefreshStatus()
end)

-- Recurring timer
Anvil:SetTimer(30000, function(timer)
PollDevice()
timer:Start()
end)

Anvil:CaptureError()​

Manually capture an error. Use this for URL callbacks and other async contexts.

Anvil:CaptureError(message, stacktrace?, context?)

Parameters​

ParameterTypeRequiredDescription
messagestringYesThe error message
stacktracestring/nilNoStack trace (auto-captured if nil)
contexttable/nilNoAdditional context

Context Fields​

FieldTypeDescription
eventNamestringName shown in Anvil, used for grouping
argstableKey-value pairs displayed with the error

Examples​

-- Basic
Anvil:CaptureError("Connection failed")

-- With context
Anvil:CaptureError("Parse error", nil, {
eventName = "API_Response",
args = {
endpoint = "/devices",
statusCode = 200
}
})

-- In a URL callback
C4:url():OnDone(function(transfer, responses, errCode, errMsg)
local ok, err = xpcall(function()
ProcessResponse(responses)
end, function(e)
Anvil:CaptureError(e, nil, {
eventName = "HTTP_Callback",
args = { url = requestUrl }
})
return e
end)
end):Get(requestUrl)

Anvil:OnDriverInit()​

Wrap your OnDriverInit code. Ensures the SDK is fully ready before your code runs.

Anvil:OnDriverInit(callback, ...)

Parameters​

ParameterTypeRequiredDescription
callbackfunctionYesYour init function
...anyNoArguments to pass to callback

Example​

function OnDriverInit(strDIR)
require('vendor.driverforge-sdk')
Driverforge:Init("YOUR_API_KEY")

Anvil:OnDriverInit(function(strDIR)
-- Your init code here
C4:UpdateProperty("Version", C4:GetDriverConfigInfo("version"))
InitializeDevice()
end, strDIR)
end

Anvil:ForwardLog()​

Manually forward a log message to Anvil. Not needed if you're using the logger option in Driverforge:Init(); see Automatic Logs.

Anvil:ForwardLog(level, message)

Parameters​

ParameterTypeRequiredDescription
levelstringYes"DEBUG", "INFO", "WARN", "ERROR", "TRACE"
messagestringYesThe log message

Example​

Anvil:ForwardLog("INFO", "Device connected")
Anvil:ForwardLog("ERROR", "Failed to parse response")