Awesome stuff coming soon!
logo

Snippets

SSJS snippet to create Email

Have you ever imagined creating HTML emails within Marketing cloud from an External application was easy!




<script runat = "server">
Platform.Load("Core", "1.1.1");
var acc_tok = "";
var x = "500";
var url = "https://xxxx.auth.marketingcloudapis.com/v2/token";
var payload = {
    client_id: "xxxxxxxx",
    client_secret: "xxxxxxxx",
    grant_type: "client_credentials"
};
var contentType = "application/json";

try {
    var request = HTTP.Post(url, contentType, Stringify(payload));
    if (request.StatusCode == 200) {
        var token_res = Platform.Function.ParseJSON(request.Response[0]);
        acc_tok = token_res.access_token;
        Write("Success " + acc_tok);
    }
} catch (error) {
    Write("error occured");
    Write(error);
} 
</script>

<script runat = "server" >
    Platform.Load("Core", "1.1.1");

var headerNames = ["Authorization"];
var headerValues = ["Bearer " + acc_tok];
var url2 = "https://xxxxxx.rest.marketingcloudapis.com/asset/v1/content/assets";
var payload2 = {
    Authorization: AT,
    name: "SSJS_created_email",
    channels: {
        "email": true,
        "web": false
    },
    views: {
        "html": {
            "content": "I am a SSJS REST API email!"
        },
        text: {},
        subjectline: {"content": "Welcome to SFMC-NINJA"},
        preheader: {"content": "My Pre-header"}
    },
    assetType: {
        name: "htmlemail",
        id: 208
    }
};
var contentType = "application/json";

try {
    var request2 = HTTP.Post(url2, contentType, Stringify(payload2), headerNames, headerValues);
    if (request2.StatusCode == 201) {
        Write("Created email" + request2);
    }
} catch (error) {
    Write("error occured");
    Write(request2);
} 
</script>

Just follow the below steps to see the above code in action!



  1. Create a Landing page and paste the above code.
  2. UPDATE REST and AUTH URLs, Client ID and Client secrete in the code.
  3. Publish the code and go to Conent builder, you will see an email created programatically with the name "SSJS_created_email"