Workflows Reference
Middleware Workflows
Run custom logic in a defined sequence before every MonetizationOS decision. Middleware workflows can:
- Resolve the user's identity, allowing you to use any method to derive the user's authentication state and identifier.
- Reject the request, allowing you to block requests from bots with minimal resource use.
- Add bot tags to the request, allowing you to extend MonetizationOS' bot classification with your own detection methods.
- Add arbitrary properties to the request, which can be accessed by all downstream workflows.
Usage
const : = async ({
,
,
,
}: ): <[]> => {
return [];
};
export default ;Resolving Identity with Custom Inputs
Middleware workflows can use custom decision inputs to resolve the user's identity.
const : = async ({
,
,
,
}: ): <[]> => {
// Skip processing if identity has already been set.
if () {
return [];
}
// Extract identity details from the request
const = .["x-session-token"];
// If no details present, do nothing. Identity may be handled by another middleware workflow,
// or the request may get to the end of the sequence with no identity set, which will cause
// the request to be rejected.
if (!) {
return [];
}
// Lookup session in auth system
const = await (
`https://my-auth-system.com/sessions/${}`,
{
: { : `Bearer ${.}` },
},
);
const = . ? await .() : null;
// Fail if no user found
if (!?.id) {
return [
{ : "fail", : 401, : "Session not found" },
];
}
// Return an action to set identity
return [
{
: "set_identifier",
: {
: .isRegistered ? "authenticated" : "anonymous",
: .id,
},
},
];
};
export default ;Setting properties
Middleware workflows can set arbitrary properties that can then be consumed in other workflows via context.properties.
const : = async ({
,
,
,
}: ): <[]> => {
if (
. &&
new (.)..("testMode") === "true"
) {
return [
{ : "set_properties", : { : true } },
];
}
return [];
};
export default ;Invocation
Middleware workflows are always run, in the order defined by the Request Sequence, at the start of every decision or offer redemption request. Middleware workflows are not run for Endpoint or Action workflows.
Arguments
Prop
Type
Return Type
The workflow returns a list of MiddlewareWorkflowResult. Each item in the list is one action to be performed.
Common Use Cases
- Resolve the user's identifier using custom inputs, or information from a third-party system.
- Identify bots using custom logic that is not handled by your CDN.
- Fail requests early, without incurring the additional costs of reading or writing data.
- Extract information from the request and share it with downstream workflows using custom properties.