verified
Create an AssetLearn how to create an asset from Content Builder in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include REST API methods.
Table of contents
Email asset
var payload = {
name: "My HTML Email",
channels: {
email: true,
web: false
},
views: {
html: {
content: "<h1>Hello World</h1>"
},
text: {},
subjectline: {
content: "Hello, this is my subject line"
},
preheader: {
content: "and this is my preheader"
}
},
assetType: {
name: "htmlemail",
id: 208
}
};
var endpoint = restInstanceUrl + "asset/v1/content/assets";
var request = HTTP.Post(endpoint, "application/json", Stringify(payload), ["Authorization"], ["Bearer " + accessToken]);
var result = Platform.Function.ParseJSON(String(request.Response));
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var payload = {
name: "My HTML Email",
channels: {
email: true,
web: false
},
views: {
html: {
content: "<h1>Hello World</h1>"
},
text: {},
subjectline: {
content: "Hello, this is my subject line"
},
preheader: {
content: "and this is my preheader"
}
},
assetType: {
name: "htmlemail",
id: 208
}
};
var endpoint = restInstanceUrl + "asset/v1/content/assets";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
request.contentType = "application/json";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"id": 998877,
"customerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"objectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"contentType": "application/vnd.etmc.email.Message; kind=paste",
"assetType": {
"id": 208,
"name": "htmlemail",
"displayName": "HTML Email"
},
"name": "My HTML Email",
"owner": {
"id": 0011223344,
"email": "",
"name": "Integration app user",
"userId": "0011223344"
},
"createdDate": "2023-09-01T09:05:20.937-06:00",
"createdBy": {
"id": 0011223344,
"email": "",
"name": "Integration app user",
"userId": "0011223344"
},
"modifiedDate": "2023-09-01T09:05:20.937-06:00",
"modifiedBy": {
"id": 0011223344,
"email": "",
"name": "Integration app user",
"userId": "0011223344"
},
"enterpriseId": 123456789,
"memberId": 987654321,
"status": {
"id": 1,
"name": "Draft"
},
"thumbnail": {
"thumbnailUrl": "/v1/assets/998877/thumbnail"
},
"category": {
"id": 112233,
"name": "Content Builder",
"parentId": 0
},
"views": {
"html": {
"thumbnail": {},
"content": "<h1>Hello World</h1>",
"modelVersion": 2
},
"text": {
"thumbnail": {},
"modelVersion": 2
},
"subjectline": {
"thumbnail": {},
"content": "Hello, this is my subject line",
"modelVersion": 2
},
"preheader": {
"thumbnail": {},
"content": "and this is my preheader",
"modelVersion": 2
}
},
"availableViews": [
"html",
"text",
"subjectline",
"preheader"
],
"channels": {
"email": true,
"web": false
},
"data": {
"email": {
"options": {
"characterEncoding": "us-ascii"
},
"legacy": {
"legacyId": 123456,
"legacyKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"legacyType": "email",
"legacyCategoryId": 3136
}
}
},
"legacyData": {
"legacyId": 123456,
"legacyKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"legacyType": "email",
"legacyCategoryId": 3136
},
"modelVersion": 2
}
Image asset
var payload = {
name: "MyNewImage",
assetType: {
name: "png",
id: 28
},
FileProperties: {
fileName: "MyNewImage.png"
},
file: "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAABGlBMVEUAAAD/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/w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l + "asset/v1/content/assets";
var request = HTTP.Post(endpoint, "application/json", Stringify(payload), ["Authorization"], ["Bearer " + accessToken]);
var result = Platform.Function.ParseJSON(String(request.Response));
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var payload = {
name: "MyNewImage",
assetType: {
name: "png",
id: 28
},
FileProperties: {
fileName: "MyNewImage.png"
},
file: "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAABGlBMVEUAAAD/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf/wQf///+9q6J7AAAAXHRSTlMAAAMLR4mrigozXW5jPhIoqfWqArTt/f70x2Emi+z4mQiikQc87/NOeawBG0xlv98na9X58UGDRVjwYvLlnjDWSNorj9L6F4LYsBYflX09ue78++oZDDRTO20yBRwJr24AAAABYktHRF2d3zABAAAAB3RJTUUH5gsIEwgRAqT8EgAAAQ5JREFUOMvNktlSwkAQRbtJBNxAiCQGNUiiASJEUUSUHfd9AUWl//87GKlIsCozz5ynrrqnqmumL8BcgRgKIfJTSV4IhyOyhIjRxaXlldXYrI0YX0sQI5GMK/J6SiVtQ09L6OebOnnoW9veZGRifwbuZGmK5k+m5Rm4u0dB2Dn0NuQpmILFUme/WHI5wsEhYLR8dEw8KidgVQ3iY59C7UyQk3EOLglxoS4W6qCKBRUaYqEBTbHQhFZblLdbgJ0uP+92kN2hd3F5FRinzN6kM6hcm0Gfrd0o0zpI5dvJcyv2jHB371cOnYf0YzL79Jx7eX17t3839s3B/wazTjusrx+fw+FXrRr5/hlxCz5/jAEyGrjm5WyNjQAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMi0xMS0wOFQxOTowODoxNyswMDowMOMaHlkAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjItMTEtMDhUMTk6MDg6MTcrMDA6MDCSR6blAAAAV3pUWHRSYXcgcHJvZmlsZSB0eXBlIGlwdGMAAHic4/IMCHFWKCjKT8vMSeVSAAMjCy5jCxMjE0uTFAMTIESANMNkAyOzVCDL2NTIxMzEHMQHy4BIoEouAOoXEXTyQjWVAAAAAElFTkSuQmCC"
};
var endpoint = restInstanceUrl + "asset/v1/content/assets";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
request.contentType = "application/json";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"id": 12345678,
"customerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"objectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"assetType": {
"id": 28,
"name": "png",
"displayName": "Image"
},
"fileProperties": {
"fileName": "MyNewImage.png",
"extension": "png",
"fileSize": 6578,
"fileCreatedDate": "2023-09-01T09:07:32.6926953-06:00",
"width": 500,
"height": 107,
"publishedURL": "https://mydomain.com/lib/S0M3-GU1D-K3Y-G03SR1G4T-H3R3/m/1/S0M3-GU1D-K3Y-G03SR1G4T-H3R3.png"
},
"name": "MyNewImage",
"owner": {
"id": 0011223344,
"email": "",
"name": "Integration app user",
"userId": "0011223344"
},
"createdDate": "2023-09-01T09:07:32.693-06:00",
"createdBy": {
"id": 0011223344,
"email": "",
"name": "Integration app user",
"userId": "0011223344"
},
"modifiedDate": "2023-09-01T09:07:32.693-06:00",
"modifiedBy": {
"id": 0011223344,
"email": "",
"name": "Integration app user",
"userId": "0011223344"
},
"enterpriseId": 123456789,
"memberId": 987654321,
"status": {
"id": 2,
"name": "Published"
},
"category": {
"id": 112233,
"name": "Content Builder",
"parentId": 0
},
"availableViews": [],
"modelVersion": 2
}
TIP
In order to transfer an image to the Content Builder, we first need to convert it to a Base64 encoded binary string.
WARNING
The maximum file size for files uploaded to Content Builder is 5 MB for all file types.
Category (folder)
var payload = {
"Name": "MyNewFolder",
"ParentId": 123456
};
var endpoint = restInstanceUrl + "asset/v1/content/categories";
var request = HTTP.Post(endpoint, "application/json", Stringify(payload), ["Authorization"], ["Bearer " + accessToken]);
var result = Platform.Function.ParseJSON(String(request.Response));
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var payload = {
"Name": "MyNewFolder",
"ParentId": 123456
};
var endpoint = restInstanceUrl + "asset/v1/content/categories";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
request.contentType = "application/json";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"id": 987654321,
"description": "",
"enterpriseId": 123456789,
"memberId": 987654321,
"name": "MyNewFolder",
"parentId": 123456,
"categoryType": "asset"
}
WARNING
When performing requests using HTTP.Get() the results are returned in the Content sub-object, but for HTTP.Post(), the results are returned in Response sub-object.
Reference
Ressources and references related to the current methods.
Official documentation