Script service

Script service runs short Lua scripts that tenants keep in configuration, so an application can change business logic, such as a fee calculation or the routing of a submitted form, without a new release. A script runs when an application calls the API, or when a domain event it is configured for is received.

Scripts run in a sandbox with a fixed instruction quota. They get their inputs as the inputs table and can call other ADSP services through the adsp table, for example to read a form submission, create a task or send a domain event.

Client roles

client urn:ads:platform:script-service

name description
script-runner Read script definitions, run any script and test scripts. Appears in the tenant admin.
script-service Assigned to the script service account. Give this role, or roles of other services, to the service account so that scripts using it can call those services.

A script definition can also list runnerRoles. A user with any of those roles can run that script without script-runner, but cannot list or read definitions.

Tenancy

Every request runs in the tenant of the caller’s access token. The API accepts only tokens issued by a tenant realm: core (platform) tokens are rejected with 401, and there is no tenantId parameter.

Script definitions are read from the tenant’s urn:ads:platform:script-service configuration, merged with core definitions. If a core definition has the same ID as a tenant definition, the core definition is used.

Event-triggered scripts run in the tenant of the event.

API overview

All paths are relative to /script/v1.

Method Path Description Role
GET /scripts List script definitions. script-runner
GET /scripts/{script} Get a script definition. script-runner
POST /scripts/{script} Run a configured script. script-runner or a runnerRoles role
POST /scripts Test Lua source from the body without saving. script-runner

Errors are returned as { "error": "<message>" }. A missing body value returns 400, an unknown script ID returns 404, and an error raised by the script returns 500 with the Lua error in the message.

Concepts

Script definition

A script definition is stored in configuration under its ID:

Property Description
id, name Required. id matches the configuration key.
description Optional.
script Required. Lua source.
runnerRoles Roles, in addition to script-runner, that can run the script.
useServiceAccount When true, calls to other services use the script service account. Otherwise they use the caller’s token.
triggerEvents Domain events that run the script. Each has a namespace, a name and optional criteria.
includeValuesInEvent When true, the inputs and outputs are included in the script events. Leave it off if they hold personal data.

Inputs and outputs

A run’s inputs are available to the script as the global inputs table. For an event-triggered run, inputs is the event payload.

Whatever the script returns is the run’s output: the API returns the returned values as a JSON array, in order. return inputs.count * 25 returns [75] for a count of 3.

Sandbox

Scripts run in a Lua sandbox. Only safe parts of the standard library are available (no file, OS or module loading), and a script that runs more than 500,000 instructions is stopped with a quota error.

Calling other services

The adsp table has these functions. Each calls the platform service in the run’s tenant, with the caller’s token or, when useServiceAccount is set or the run was triggered by an event, the script service account. That identity needs the roles the called service requires.

Function Service
adsp.GetConfiguration(namespace, name) Configuration
adsp.GetFormData(formId) Form
adsp.GetFormSubmission(formId, submissionId) Form
adsp.DispositionFormSubmission(formId, submissionId, dispositionStatus, reason) Form
adsp.CreateTask(queueNamespace, queueName, name, description, recordId, priority, context) Task
adsp.SendDomainEvent(namespace, name, correlationId, context, payload) Event
adsp.GeneratePdf(templateId, filename, values) PDF
adsp.ReadValue(namespace, name, top, after) Value
adsp.WriteValue(namespace, name, value) Value
adsp.HttpGet(url) Any URL

adsp.HttpGet sends the run’s bearer token to the URL, so use it only for ADSP or other trusted APIs.

Testing

POST /scripts runs Lua source from the request body. The adsp functions that change data (for example SendDomainEvent, CreateTask, WriteValue, GeneratePdf and HttpGet) are replaced with stubs, while reads such as GetConfiguration call the real service. No events are sent for a test run. The tenant admin’s script editor uses this endpoint.

Event triggers

When a domain event matches a definition’s triggerEvents, the script runs with the event payload as inputs, using the service account. Events in the script-service namespace never trigger scripts, so a script’s own events cannot start a loop. A triggered run that fails is not retried.

Events

Event Sent when
script-executed A configured script ran. Includes outputs if values are included.
script-execution-failed A configured script raised an error. Includes error and source.

Both events include the jobId, the definition, executedBy for an API run or triggeredBy for an event-triggered run, and inputs when includeValuesInEvent is set. Their context has the definitionId. Test runs send no events.

Both are on the script-execution-updates stream, which users with script-runner can subscribe to through Push service.

Code examples

Configure a script

const configurationServiceUrl = await directory.getServiceUrl(adspId`urn:ads:platform:configuration-service`);

await fetch(new URL('configuration/v2/configuration/platform/script-service', configurationServiceUrl), {
  method: 'PATCH',
  headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    operation: 'UPDATE',
    update: {
      'calculate-fee': {
        id: 'calculate-fee',
        name: 'Calculate fee',
        script: 'return inputs.count * 25',
        runnerRoles: ['applicant'],
        triggerEvents: [],
      },
    },
  }),
});

Run a script

const scriptServiceUrl = await directory.getServiceUrl(adspId`urn:ads:platform:script-service`);

const response = await fetch(new URL('script/v1/scripts/calculate-fee', scriptServiceUrl), {
  method: 'POST',
  headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
  body: JSON.stringify({ inputs: { count: 3 }, correlationId: 'application-1234' }),
});
const [fee] = await response.json(); // 75

Run a script when a form is submitted

This definition runs when a form is submitted, reads the submission and creates a task for it. Because event-triggered scripts use the service account, give the service account the roles that form service and task service require.

local submission = adsp.GetFormSubmission(inputs.form.id, inputs.submission.id)
local taskId = adsp.CreateTask(
  "support", "intake", "Review application",
  "Review the submitted application.", submission.id, "Normal"
)
return taskId
{
  "id": "create-review-task",
  "name": "Create review task",
  "script": "<the Lua above>",
  "triggerEvents": [{ "namespace": "form-service", "name": "form-submitted" }]
}

This site uses Just the Docs, a documentation theme for Jekyll.