Retrieve an Asset verified

Learn how to retrieve an asset from Content Builder in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include simple and advanced REST API methods.

Table of contents

Asset query by Id

var id = "123456";

var endpoint = restInstanceUrl + "asset/v1/content/assets/" + id;

var request = HTTP.Get(endpoint, ["Authorization"], ["Bearer " + accessToken]);

var result = Platform.Function.ParseJSON(String(request.Content));
<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 id = "123456";

        var endpoint = restInstanceUrl + "asset/v1/content/assets/" + id;

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "GET";
            request.contentType = "application/json";

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }	

</script>
{
    "id": 123456,
    "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": 987654321,
        "email": "",
        "name": "Integration app user",
        "userId": "987654321"
    },
    "createdDate": "2023-09-01T09:05:20.937-06:00",
    "createdBy": {
        "id": 987654321,
        "email": "",
        "name": "Integration app user",
        "userId": "987654321"
    },
    "modifiedDate": "2023-09-01T09:05:20.937-06:00",
    "modifiedBy": {
        "id": 987654321,
        "email": "",
        "name": "Integration app user",
        "userId": "987654321"
    },
    "enterpriseId": 123456789,
    "memberId": 123456789,
    "status": {
        "id": 1,
        "name": "Draft"
    },
    "thumbnail": {
        "thumbnailUrl": "/v1/assets/537587/thumbnail"
    },
    "category": {
        "id": 112233,
        "name": "Content Builder",
        "parentId": 0
    },
    "views": {
        "html": {
            "content": "<h1>Hello World</h1>",
            "thumbnail": {},
            "modelVersion": 2
        },
        "text": {
            "thumbnail": {},
            "modelVersion": 2
        },
        "subjectline": {
            "content": "Hello, this is my subject line",
            "thumbnail": {},
            "modelVersion": 2
        },
        "preheader": {
            "content": "and this is my preheader",
            "thumbnail": {},
            "modelVersion": 2
        }
    },
    "availableViews": [
        "html",
        "text",
        "subjectline",
        "preheader"
    ],
    "channels": {
        "email": true,
        "web": false
    },
    "data": {
        "email": {
            "options": {
                "characterEncoding": "us-ascii"
            },
            "legacy": {
                "legacyId": 987654,
                "legacyKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "legacyType": "email",
                "legacyCategoryId": 112233
            }
        }
    },
    "legacyData": {
        "legacyId": 987654,
        "legacyKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
        "legacyType": "email",
        "legacyCategoryId": 112233
    },
    "modelVersion": 2
}

File binary query by Id

var id = "123456";

var endpoint = restInstanceUrl + "asset/v1/content/assets/" + id + "/file";

var request = HTTP.Get(endpoint, ["Authorization"], ["Bearer " + accessToken]);

var result = request.Content;
<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 id = "123456";

        var endpoint = restInstanceUrl + "asset/v1/content/assets/" + id + "/file";

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "GET";
            request.contentType = "application/json";

        var results = request.send();

        var result = results.content;

        Write(result);
		
	} catch(error) {

        Write(Stringify(error));
        
    }	

</script>
"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQA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AAldEVYdGRhdGU6bW9kaWZ5ADIwMjItMTEtMDhUMTk6MDg6MTcrMDA6MDCSR6blAAAAV3pUWHRSYXcgcHJvZmlsZSB0eXBlIGlwdGMAAHic4/IMCHFWKCjKT8vMSeVSAAMjCy5jCxMjE0uTFAMTIESANMNkAyOzVCDL2NTIxMzEHMQHy4BIoEouAOoXEXTyQjWVAAAAAElFTkSuQmCC"

TIP

There is no need to stringify the response of an image binary request.

Category (folder) query by Id

var id = "123456";

var endpoint = restInstanceUrl + "asset/v1/content/categories/" + id;

var request = HTTP.Get(endpoint, ["Authorization"], ["Bearer " + accessToken]);

var result = Platform.Function.ParseJSON(String(request.Content));
<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 id = "123456";

        var endpoint = restInstanceUrl + "asset/v1/content/categories/" + id;

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "GET";
            request.contentType = "application/json";

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }	

</script>
{
    "id": 123456,
    "description": "",
    "enterpriseId": 123456789,
    "memberId": 123456789,
    "name": "MyNewFolder",
    "parentId": 3134,
    "categoryType": "asset"
}

Simple query

var endpoint = restInstanceUrl + "asset/v1/content/assets?$page=1&$pagesize=10&$orderBy=name asc&$filter=assetType.name=png&$fields=id,customerKey,fileProperties";

var request = HTTP.Get(endpoint, ["Authorization"], ["Bearer " + accessToken]);

var result = Platform.Function.ParseJSON(String(request.Content));
<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 endpoint = restInstanceUrl + "asset/v1/content/assets?$page=1&$pagesize=10&$orderBy=name asc&$filter=assetType.name=png&$fields=id,customerKey,fileProperties";

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "GET";
            request.contentType = "application/json";

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }	

</script>
{
    "count": 343,
    "page": 1,
    "pageSize": 10,
    "links": {},
    "items": [
        {
            "id": 123456,
            "customerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
            "assetType": {
                "id": 28,
                "name": "png",
                "displayName": "Image"
            },
            "fileProperties": {
                "fileName": "1.png",
                "extension": "png",
                "fileSize": 1615,
                "fileCreatedDate": "2021-01-19T06:05:16.430124-06:00",
                "width": 72,
                "height": 72,
                "publishedURL": "https://mydomain.com/lib/S0M3-GU1D-K3Y-G03SR1G4T-H3R3/m/1/S0M3-GU1D-K3Y-G03SR1G4T-H3R3.png"
            },
            "name": "1.png",
            "modelVersion": 2
        }
    ]
}

Advanced query

var payload = {
    page: {
        page: 1,
        pageSize: 50
    },
    query: {
        property: "assetType.id",
        simpleOperator: "IN",
        value: ["20", "21", "22", "23", "28"]
    },
    sort: [
        {
            property: "id",
            direction: "ASC"
        }
    ],
    fields: [
        "thumbnail",
        "category",
        "content",
        "data"
    ]
}

var endpoint = restInstanceUrl + "asset/v1/content/assets/query";

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 = {
            page: {
                page: 1,
                pageSize: 50
            },
            query: {
                property: "assetType.id",
                simpleOperator: "IN",
                value: ["20", "21", "22", "23", "28"]
            },
            sort: [
                {
                    property: "id",
                    direction: "ASC"
                }
            ],
            fields: [
                "thumbnail",
                "category",
                "content",
                "data"
            ]
        }

        var endpoint = restInstanceUrl + "asset/v1/content/assets/query";

        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>
{
    "count": 654,
    "page": 1,
    "pageSize": 50,
    "links": {},
    "items": [
        {
            "id": 1234,
            "customerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
            "assetType": {
                "id": 23,
                "name": "jpg",
                "displayName": "Image"
            },
            "name": "new-arrivals-hero.jpg",
            "thumbnail": {
                "thumbnailUrl": "/v1/assets/627/thumbnail"
            },
            "category": {
                "id": 9876,
                "name": "Assets",
                "parentId": 1234
            },
            "modelVersion": 2
        }
    ]
}

TIP

Each asset type has an Id: GIF (20), JPE (21), JPEG (22), JPG (23), PNG (28). Please find the full list in the references below.

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.

DANGER

Although including sort parameters in the payload is not mandatory, excluding it will produce duplicates and missed results.

Reference

Ressources and references related to the current methods.

Official documentation
List of asset types

Last Updated: