Recipes
Script patterns for common needs, written in full or pointing to the page that holds the example.
Most scripts are variations of a short list of patterns. Starting from a pattern that already handles the failure modes of its extension point is faster and safer than starting from a blank file, because the failure modes differ between extension points: a throw in a pass data provider fails the render, a throw in a pass updater notification is only logged, a throw in an email engine fails the send. Recipes 4 and 8 combine several features and are written in full here. The others point to the page that holds their example, so that each example exists once. Hosts, secrets and field names are fictitious and have to be replaced.
The registration key, the wiring and the full reference of each extension point are on the endpoint pages. The recipes assume the rules of How scripts run and Handlers.
1. Read data from an OAuth-protected API
The complete example, a pass data provider that reads a CRM protected by the OAuth 2.0 client credentials grant, with the secrets, a cache keyed by the customer id and a soft failure, is on fetch authentication.
2. Choose the pass type when a pass is created
The example, a lookup table with a neutral fallback in OnPassCreating, is the first example of the pass updater.
3. Mirror installs and uninstalls into a CRM
The example, with one field per wallet and an idempotent PUT, is the second example of the pass updater. The identifier is read as identifiers['crm.customerId'], keyed without the id. prefix, like everywhere else.
4. Push a registration to a CRM and map a consent flag
The goal is to create or update the customer in an external system as soon as the enrolment flow reaches the step. The script is a step of a flow, so its error fails the step; the example therefore lets a failure of the CRM reach the flow deliberately, since an enrolment that silently skipped the CRM would leave the two systems out of step. The consent may arrive on the account as a boolean or as text, and it is an enumeration in the CRM; the mapping is written once, in both directions, to avoid an inversion.
The step is added to the flow in server/flows.yml:
Notes. ThrowOnError is left at its default of true, so a non-success status throws and fails the step; a flow that must not depend on the CRM catches the failure and logs it instead. The contact id is written back on the entity as id.crm.contactId for the steps that follow in the flow. The consent key is consents_email, which may arrive as a boolean or as text, depending on the form (see Data objects). The consent mapping is a single object, so the inverse mapping, when needed, is derived from it rather than rewritten, and truthiness is never relied on for strings such as 'n'. A flow step with no script registered under its scriptId does nothing, without an error.
5. Update passes from a paged API on a schedule
The example, a paged API read with a hard cap and a logged truncation, is on Push updates, with the server/scheduler.yml task.
6. Send transactional emails through another provider
The example, which renders the template with buildEmail and forwards the result, is on the email engine page, with the server/emails.yml setting.
7. Choose, or skip, the enrolment confirmation email
The examples, a choice by customer and a choice by language, are on the email challenge template page.
8. Derive a business segment from data another provider read
The goal is to compute a label that layouts and filters can use, from values other providers wrote. The providers of a pass type run in rounds, as described in Pass data provider. The segment provider therefore returns false until its input exists.
The entry is listed after the provider that writes points:
Notes. A template reads memberSegment.label. The entity of a later round holds the keys as the other providers wrote them (points), not their namespaced form. The provider does not call any remote system, so it needs neither a cache nor a soft failure, but it keeps returning false rather than true when it computed nothing. The first round runs both providers side by side, so the segment provider necessarily finds nothing there; it computes in the second round, after memberLookup has found data. Without a provider that finds data in the first round, no second round happens.
9. Authenticate through a token endpoint with a custom exchange
The example, a CustomBearer token retriever for a token endpoint that is not a standard OAuth 2.0 one, is on fetch authentication.
FAQ
Last updated

