ThingWorx WebSocket-based Edge MicroServer (WS EMS) and Lua Script Resource (LSR) > Configuring a Template for the Lua Script Resource > Implementing Services Using the Lua Script Engine
  
Implementing Services Using the Lua Script Engine
Use the services group to implement the services that you declared when defining services.
Once a service has been defined, you can implement custom logic for the service, using the Lua Script engine. To speed implementation, you can use the add-ons of the Lua community and ThingWorx-specific Web Services. The Web Services are included in the WS EMS distribution to facilitate the development of custom scripts.
Services are defined as Lua functions that can be executed remotely from the ThingWorx platform, and must provide a valid response in their return statement. For example:
services.Add = function(me, headers, query, data)
if not data.p1 or not data.p2 then
return 400, "You must provide the parameters p1 and p2"
end
return 200, data.p1 + data.p2
end

services.Subtract = function(me, headers, query, data)
if not data.p1 or not data.p2 then
return 400, "You must provide the parameters p1 and p2"
end
return 200, data.p1 - data.p2
end
Parameters
The following table lists and describes the parameters that you can use to define a service:
Parameter
Description
services.nameOfService
Implement a service. The name must match the name that is specified for the service in the service definition.
me
Create a table that refers to the thing.
headers
Create a table of HTTP headers.
query
Specify the query parameters from the HTTP request.
data
Create a Lua table that contains the parameters of the service call.
A service function must return the following values, in the following order:
1. An HTTP return code (200 for success).
2. A table of HTTP response headers that should contain a valid Content-Type header, typically with a value of application/json.
3. (Optional) A default table can easily be generated by calling tw_utils.RESP_HEADERS().
4. The response data, in the form of a JSON string. This data can be generated from a Lua table using json.encode, or tw_utils.encodeData().