Practical, copy-paste-ready patterns for the most common SSJS development scenarios.

In This Section

Recipe Description
CloudPage Apps Request routing, session management, multi-step forms
AMPscript Bridge Passing data between AMPscript and SSJS
REST API Calls OAuth2 tokens, GET/POST patterns, error handling
DE CRUD Patterns Complete CRUD with both Platform.Function and Core
Subscriber Management Subscribe, unsubscribe, update preferences
Date and Time Formatting, calculations, timezone handling

Quick Patterns

Get current subscriber info in email

var email = Platform.Variable.GetValue("@_emailaddr")
         || Platform.Variable.GetValue("@emailaddr");
var subKey = Platform.Variable.GetValue("@_subscriberkey")
          || Platform.Variable.GetValue("@subscriberKey");

Redirect to login if no session

var session = Platform.Request.GetCookieValue("session");
if (!session) {
    Platform.Response.Redirect("/login?next="
        + Platform.Function.URLEncode(Platform.Request.GetResolvedURL()));
}

JSON API response boilerplate

Platform.Response.SetContentType("application/json");
var method = Platform.Request.Method;
if (method !== "POST") {
    Platform.Response.SetResponseCode(405, "Method Not Allowed");
    Write(Stringify({ error: "POST required" }));
    return;
}
var body = Platform.Function.ParseJSON(Platform.Request.GetPostData() + "");