Basic Request Router

<script runat="server">
Platform.Load("core", "1.1.5");

var method = Platform.Request.Method;
var rawBody = method === "POST" ? Platform.Request.GetPostData() : "";

if (method === "GET") {
    handleGet();
} else if (method === "POST") {
    handlePost(Platform.Function.ParseJSON(rawBody + ""));
} else {
    Platform.Response.SetResponseCode(405, "Method Not Allowed");
    Write(Stringify({ error: "Method not allowed" }));
}

function handleGet() {
    var id = Platform.Request.GetQueryStringParameter("id");
    if (Platform.Function.Empty(id)) {
        Platform.Response.SetContentType("application/json");
        Write(Stringify({ message: "Welcome to the API" }));
        return;
    }
    var record = Platform.Function.Lookup("Records", "data", "id", id);
    Platform.Response.SetContentType("application/json");
    if (Platform.Function.Empty(record)) {
        Platform.Response.SetResponseCode(404, "Not Found");
        Write(Stringify({ error: "Record not found" }));
    } else {
        Write(Stringify({ id: id, data: record }));
    }
}

function handlePost(body) {
    if (!body || !body.email) {
        Platform.Response.SetResponseCode(400, "Bad Request");
        Write(Stringify({ error: "email is required" }));
        return;
    }
    if (!Platform.Function.IsEmailAddress(body.email)) {
        Platform.Response.SetResponseCode(400, "Bad Request");
        Write(Stringify({ error: "Invalid email format" }));
        return;
    }
    Platform.Function.InsertData("Submissions",
        ["Email", "Name", "Timestamp"],
        [body.email, body.name || "", Platform.Function.Now()]
    );
    Platform.Response.SetContentType("application/json");
    Write(Stringify({ status: "ok", email: body.email }));
}
</script>

Session Management

<script runat="server">
var SESSION_COOKIE = "sfmc_session";
var SESSION_DE = "Sessions";

function createSession(userId, data) {
    var token = Platform.Function.GUID();
    var expires = Platform.Function.FormatDate(
        Platform.Function.DateAdd(Platform.Function.Now(), 30, "D"),
        "MM/DD/YYYY HH:mm:ss"
    );
    Platform.Function.InsertData(SESSION_DE,
        ["token", "userId", "data", "expires"],
        [token, userId, Stringify(data), expires]
    );
    Platform.Response.SetCookie(SESSION_COOKIE, token, expires, "/", "", true);
    return token;
}

function getSession() {
    var token = Platform.Request.GetCookieValue(SESSION_COOKIE);
    if (!token) return null;

    var row = Platform.Function.LookupRows(SESSION_DE, "token", token);
    if (!row || row.length === 0) return null;

    var session = row[0];
    // Check expiry
    if (new Date(session.expires) < new Date()) {
        return null;
    }
    return {
        userId: session.userId,
        data: Platform.Function.ParseJSON(session.data + "")
    };
}

function destroySession() {
    var token = Platform.Request.GetCookieValue(SESSION_COOKIE);
    if (token) {
        Platform.Function.DeleteData(SESSION_DE, ["token"], [token]);
        Platform.Response.SetCookie(SESSION_COOKIE, "",
            "Thu, 01 Jan 1970 00:00:00 GMT", "/", "", true);
    }
}

// Usage
var session = getSession();
if (!session) {
    Platform.Response.Redirect("/login");
}
Write("Hello, user " + session.userId);
</script>

Multi-Step Form

<script runat="server">
var step = parseInt(Platform.Request.GetQueryStringParameter("step") || "1", 10);
var sessionToken = Platform.Request.GetCookieValue("formSession");

// Initialize form session
if (!sessionToken) {
    sessionToken = Platform.Function.GUID();
    Platform.Response.SetCookie("formSession", sessionToken, "", "/");
    Platform.Function.InsertData("FormSessions",
        ["token", "step", "data", "created"],
        [sessionToken, "1", "{}", Platform.Function.Now()]
    );
}

if (Platform.Request.Method === "POST") {
    var fieldName = Platform.Request.GetFormField("fieldName");
    // Save step data
    var existing = Platform.Function.Lookup("FormSessions", "data", "token", sessionToken);
    var saved = Platform.Function.ParseJSON(existing + "");
    saved["step" + step] = { name: fieldName };
    Platform.Function.UpdateData("FormSessions",
        ["data", "step"],
        [Stringify(saved), String(step + 1)],
        ["token"], [sessionToken]
    );

    if (step < 3) {
        Platform.Response.Redirect("?step=" + (step + 1));
    } else {
        // Final submission
        // process complete form data...
        Write("Form complete!");
    }
}
</script>

See Also