First script
Build, wire and verify a pass data provider script that reads a member's points from an external API, with caching and a soft failure when the API is down.
The most common scripting need is to show on a pass a value that lives in another system: a points balance, a tier, a next appointment. The extension point for it is the pass data provider. The platform asks the script for data each time a pass is rendered, the script fetches it, and the pass type's templates use the values. The example is deliberately small, but it contains the three habits that every production script needs: a cache, a guard on missing input, and a soft failure.
For the full reference of the extension point, see Pass data provider. For other needs, see the table in Scripting.
What gets built
The script is called memberLookup. For a pass that carries an identifier memberId, it calls https://api.example.com/v1/members/{memberId} with an API key kept in the tenant's secrets, caches the answer for five minutes, and writes points and tier for the templates. If the member does not exist, or the API fails, the script reports that nothing was found and the pass is still rendered, without those values.
Three names have to agree, and most failures of a first script come from a mismatch between them:
Script id
memberLookup
Last part of the registration key, and scriptId in the pass type
Registration key
runtime.scriptable.passDataProvider.memberLookup
context.register(...) in the file
Namespace
memberLookup
Prefix of the keys that templates read, for example memberLookup.points
The file name does not matter to the platform, but naming the file after the script id (memberLookup.js) keeps the configuration readable.
Step 1: create the secret
The API key must not be written in the script or in YAML. It goes into the tenant's secrets, managed in the admin console, as described in API keys and secrets. The key rules are in twc/secrets. The example uses MEMBER-API-KEY.
Step 2: create the script file
In the admin console, the advanced configuration is under Settings → Advanced → Advanced configuration. Creating the file server/scripts/memberLookup.js there, with the content below, defines the script.
The script is built around a few deliberate choices.
The handler is Fill and is declared async: the platform calls it by that exact name and awaits it. Fill returns true when the script found data for the pass and false when it found none. The distinction matters because the pass type may list several data providers: the platform runs the providers that found nothing again in a following round, with what the other providers have written so far.
The identifier is read as entity['id.memberId']. Identifiers of the pass are always present in the entity under the id. prefix. A pass created without memberId simply gets nothing from this script.
fetch is called with ThrowOnError: false. By default fetch throws on any non-2xx status, and a throw inside Fill makes the whole pass render fail. With the option set to false, the script reads StatusCode and decides. Network failures still throw whatever the option says, which is why the call is wrapped in try / catch. The lookupError key gives the template a way to show a neutral message when the lookup failed, instead of a wrong value. It has a specific name because a plain error key would collide with the same key written by another provider; the namespaced form is memberLookup.lookupError.
Cache stores the response for five minutes. Only the CacheKey identifies the entry, not the URL: the key therefore includes the member id. A fixed key such as member-lookup would return the first member's answer to every member. Because responses are cached even when they are errors (ThrowOnError: false), the duration should be short enough that an outage does not linger on passes. The details are in fetch.
Describe is optional and synchronous. It runs whenever the platform needs the script's namespace and fields: in the pass template editor, when the configuration is saved, and on every pass build. It must therefore be cheap and must not call fetch. It tells the admin console which identifiers the script reads and which variables it writes, so that the pass template editor can list them with their descriptions before any pass exists. Its object uses camelCase names, listed in the Describe table.
Step 3: wire the script in the pass type
A script that is only registered is never called by a pass: the pass type has to name it. The pass types are stored one per file in server/passes/<passType>.yml, and the list of data providers sits at the top level of that file, beside keys such as kind and resources.
provider: script means that the entry runs a tenant script, and scriptId is the last part of the registration key. When the pass template editor of the admin console is used instead of YAML, the same entry is added from the Data providers section, where the tenant scripts that registered a pass data provider are offered under Tenant scripts.
Several entries can be listed; they run in rounds, as described in Pass data provider.
Templates read the values under the namespace: memberLookup.points and memberLookup.tier. The unprefixed names points and tier are published as well, and the namespaced form is the one recommended in new templates, because another provider can later write the same short key. Identifiers (id.*) and debug.* keys are never prefixed.
Step 4: save and check
Saving the advanced configuration runs the script loader: every file is parsed, its top-level code is run, and its default export is called. A syntax error is reported at once, with the file name. Saving also validates the dataProviders entries of each pass type: an entry without scriptId, or a namespace that is not acceptable, is reported with the file, the entry position and the member at fault, for example server/passes/loyalty.yml: dataProviders[0].scriptId: ....
To verify the result on a real pass:
Open a pass of that pass type that carries a
memberIdidentifier, then its data page (View data). The page lists each data provider with its outcome (found, found nothing, failed), its duration and the variables it wrote. Refresh data runs the providers again.A provider shown as failed carries the reason, such as The tenant script failed. The script threw: a handler is missing or misspelled, or the script raised an error.
A provider that found nothing means
Fillreturnedfalse: the identifier is missing, the member does not exist, or the lookup failed softly (look for thelookupErrorvariable).The pass template editor, in the Data providers section, lists the variables declared by
Describe, and the preview can show real values for a chosen pass.
If the values appear on the data page but not on the pass, the template does not read them yet: add memberLookup.points to a field of the pass type.
Why it never runs
When the data page shows nothing for the script, see Troubleshooting: the usual causes are a scriptId that differs from the registration key, a handler not named exactly Fill, or a JavaScript error in any file of the tenant.
Next steps
The same file can grow in several directions. A pass often needs a second provider, for example one that adds the store the member is attached to; the entries then run in rounds. A script that must act when a customer enrols is a customer pipeline element. A script that must choose the pass type is a pass updater. Recipes shows these patterns, and Best practices lists what to review before production.
FAQ
Last updated

