Benefit code generator
Compute the code of each benefit issued from a benefit template whose code mode is custom, with runtime.scriptable.benefitTemplateCodeGenerator.
A benefit template can give the benefits it issues a code that staff scan or type: the same code for everyone, a random code drawn from a pattern, or a code computed by a script. The last option, the custom code mode, runs a short script each time a benefit is issued from the template, typically to reserve a unique code in a partner system or to build a code from the brand's own rules. This page explains how that script is written, stored and run; the endpoint behind it is runtime.scriptable.benefitTemplateCodeGenerator.{templateId}.
The custom mode is the right tool when each benefit needs a code that only another system can produce. When the codes can be random, the autogenerated mode does the same without a script; when the codes of an external system's own benefits must be shown, the benefit provider returns them with each benefit.
Registration
Unlike the other endpoints, this one is not written as a file. The script is the body of a function, entered in the code settings of the benefit template, in the admin console or through the benefit template API, with the code mode set to custom:
{
"code": {
"valueMode": "custom",
"script": "return `WELCOME-${Math.floor(Math.random() * 2176782336).toString(36).padStart(6, '0').toUpperCase()}`;"
}
}The script member is required in the custom mode; the optional format member chooses how the code is rendered: ean13, code128, qrCode, pdf417, aztec, dataMatrix, code39, or raw for text only. When the benefit template is saved, The Wallet Crew wraps the body into a script file, server/scripts/benefit-template-code-{templateId}.js, which registers runtime.scriptable.benefitTemplateCodeGenerator.{templateId} with the body as its Generate handler:
export default function (context) {
context.register("runtime.scriptable.benefitTemplateCodeGenerator.welcome-offer", {
Generate: async function () {
// the body entered on the benefit template
}
});
}The file is rewritten each time the benefit template is saved and deleted when the template leaves the custom mode or is deleted. It should not be edited by hand, and no other file should register the same key.
When it runs
Generate runs each time a benefit is issued from the benefit template to a pass that does not hold one yet, inside the operation that grants it: a scheduled task or campaign, an import row with a benefit template, an API call. Re-applying the template to a pass that already holds its benefit updates that benefit without calling the script.
Handlers
Generate
none
a promise of a string or null
Required
The body runs inside an async function: it ends with return, and can use await with the globals, such as fetch and console. It receives no information about the pass or the customer. Because it is a function body and not a file, it cannot contain import statements.
Return value
A string
The benefit is issued with that code, rendered in the template's code format.
null or undefined
The benefit is issued without a code.
Errors and edge cases
The body throws or its promise rejects (including a
fetchthat throws on a non-2xx answer, its default): the benefit is not issued to that pass, and the operation that granted it reports the error.The body does not parse: the benefit template is still saved, and the admin console warns about the syntax error. The generated file then breaks every script of the tenant, not only this one, until the body is fixed. Test the body on a copy of the benefit template before saving it on a live one.
The file is missing, for example after a manual deletion: a warning is logged and benefits are issued without a code.
The code mode is changed away from custom: the file is deleted and the script no longer runs.
Example
The body reserves a unique code in a promotion platform. The platform answers 201 with the code; any other answer throws, so that no benefit is issued without the code the holder needs.
Checking the result
Granting the benefit template to a test pass, then listing the pass's benefits with GET /api/{tenantId}/passes/{passId}/benefits, shows the generated code on the new benefit. A benefit issued without a code means the body returned nothing; a benefit that is not issued at all means it threw, which the platform's monitoring records with the error.
Common pitfalls
The body has no
return, so every benefit is issued without a code.A code computed from
Date.now()alone collides when two benefits are issued in the same millisecond. Add a random part, as the registration example does, or reserve the code in a system that guarantees uniqueness.
FAQ
Last updated

