Lua API guide

Load opcua.api to create clients, servers, and models. Examples use ua as the local name for the returned table. Start with the object that owns the operation:

Object

Entry point and reference

Network client

ua.newClient() creates the client. See Client API for connection, session and service calls.

Local server

ua.newServer() creates the server. See Server API for its lifecycle and local operations.

Address space

ua.baseModel() loads the compiled standard model. ua.emptyModel() starts with the application namespace. See Model methods, Browser and Editor.

MQTT publisher or subscriber (full)

ua.newMqttClient() creates either. See MQTT PubSub API for broker connections, datasets and callbacks.

Configuration and version

ua.clientConfig(config)
ua.serverConfig(config)

Validate a required Lua configuration table, fill its defaults in place and return the same table. Invalid settings raise a Lua error. Constructors call the matching function themselves. Call it directly to prepare a configuration for ua.baseModel() or ua.emptyModel(). See Client configuration table and Configuration table.

ua.versionValid(version)
ua.assertVersion(version)

Compare the required string with ua.Version.Version using exact equality. The first returns a boolean. The second raises an error on a mismatch and returns no value on success. ua.Version.BuildNumber is a separate string. See Choose a build profile to identify the build settings for that release.

Identifiers, values and requests

  • NodeId covers ua.NodeId.fromString, toString and isValid. ua.NodeId.Null is the string "i=0". ua.NodeId.isNull(id) returns a boolean and accepts nil, a string or a parsed NodeId table. ua.createGuid() returns a new GUID string.

  • Use ua.AttributeId to select an attribute and ua.VariantType to identify the type of a value. Data Types describes both the Lua value and its wire representation. ua.StatusCode.Good is numeric zero.

  • ua.newFolderParams(parentNodeId, name[, newNodeId]) and ua.newVariableParams(parentNodeId, name, value[, newNodeId]) return one AddNodes item table. Supply a parent NodeId string, a name string or a table with BrowseName (QualifiedName) and DisplayName (LocalizedText), and, for a variable, a DataValue table. Omit the new NodeId string to let the server assign one. See Adding Nodes.

  • ua.newEventFilter(selectFields[, whereClause]) builds a filter for an event monitored item. See Data Change and Event Subscriptions for field selection and predicates. Events require the full profile.

  • ua.parseUrl(url) accepts an endpoint URL string and returns a table with string scheme and host, numeric port and string path. An omitted port or path is nil. An invalid URL returns nil and a numeric status code.

Identity token helpers

Most clients can use the forms of client:activateSession described in Client Authentication. When building a request by hand, the following functions return an ExtensionObject table with a TypeId string and a Body table. Each takes the server-advertised policy ID string first.

Function

Remaining arguments

ua.createAnonymousToken(policyId)

None.

ua.createUsernameToken(policyId, username, password, encryptionAlgorithm)

Username string, password bytes and optional encryption algorithm URI string.

ua.createX509Token(policyId, cert)

User certificate bytes.

ua.createIssuedToken(policyId, token, encryptionAlgorithm)

Issued token bytes and optional encryption algorithm URI string.

These helpers wrap the supplied bytes. The higher-level session API handles token encryption and signatures according to the negotiated policy.

Diagnostics and constants

ua.printTable(name, value[, output]) prints a labeled Lua value for inspection. Pass a string label and an optional function receiving one line at a time. See Logging subsystem for runtime traces.

The API exports numeric constants for node classes, attribute IDs, variant types, value ranks, browse masks and monitoring modes. It also exports NodeId strings in ua.DataTypeId and ua.ReferenceType, security policy URIs in ua.SecurityPolicy, and transport URIs in ua.TranportProfileUri (the spelling in Lua). Secure Policy lists the policies implemented by each crypto backend.

ua.crypto_engine names the backend initially chosen by the runtime ("sharkssl" in BAS, "openssl" in stock Lua). ua.crypto holds its module table. ua.setCryptoEngine(name) accepts one of those strings, loads the named module and updates these two API fields, or raises an error. The channel policy implementation reads its backend directly from opcua.crypto. Use the host’s selected backend when configuring channels. Minimal builds omit the OpenSSL Lua module.

ua.debug() starts the Lua debugger connection and requires the optional ldbgmon module. ua.ServerState contains numeric server states. ua.ServerProfile contains OPC UA profile URI constants. The build-time selectors for minimal and full are documented in Choose a build profile.