Add Image CMS Content to Marketing Cloud

Use the Marketing Cloud API’s content assets resource to add image CMS content to Marketing Cloud in a connected app. If the request is successful, the content is then available in Content Builder.

Before You Begin 

To form the API request, you need these items.

  • A Marketing Cloud access token
  • The Base64-encoded image downloaded from CMS that you want to import to Marketing Cloud
  • The category ID of the folder in Content Builder where you want Marketing Cloud to save the image

API Request Details 

To send an API call to add an image to Marketing Cloud, make an HTTP POST request to the /asset/v1/content/assets resource.

Headers 

Include an Authorization header with the value Bearer {access_token}, where access_token is your CMS access token.

Request Body 

In the request body, create a JSON object with these fields.

  • name—A string that contains the name of the image.

  • assetType—An object containing the id field with a value that matches the asset type of the image format, such as GIF or JPEG. For a list of possible types, see Marketing Cloud API: List of Asset Types.

  • file—A string that contains the Base64-encoded image.

  • category—An object that contains the id field with a string value. Set the value to the category ID of the Content Builder folder where you want Marketing Cloud to save the image.

For more information on the request and response format for this resource, see Marketing Cloud API: REST API v1 Reference.

Sample Request 

This Node.js sample downloads an image file from CMS and then adds the image to Marketing Cloud. The currentNode object contains the CMS content.

1async function moveImageToMC(name, currentNode, mcAuthResults, cmsAuthResults) {
2  return new Promise(async (resolve, reject) => {
3    const imageUrl = `${cmsAuthResults.instance_url}${currentNode.resourceUrl}`;
4
5    const base64ImageBody = await downloadBase64FromURL(imageUrl, cmsAuthResults.access_token);
6
7    console.log(`Uploading Image to MC: ${name} - ${imageUrl}`);
8
9    let imageAssetBody = {
10      name: name,
11      assetType: {
12        id: getImageAssetType(currentNode.fileName),
13      },
14      file: base64ImageBody,
15      category: {
16        id: "1520210",
17      },
18    };
19    // Create MC Asset
20    await createMCAsset(mcAuthResults.access_token, imageAssetBody);
21    resolve();
22  });
23}
24
25function getImageAssetType(imageName) {
26  let assetTypeResults = "8";
27
28  let fileNameChunks = imageName.split(".");
29
30  let imageExtension = fileNameChunks[fileNameChunks.length - 1];
31
32  switch (imageExtension.toLowerCase()) {
33    case "gif":
34      assetTypeResults = 20;
35      break;
36    case "jpeg":
37      assetTypeResults = 22;
38      break;
39    case "jpg":
40      assetTypeResults = 23;
41      break;
42    case "png":
43      assetTypeResults = 28;
44      break;
45    default:
46      break;
47  }
48
49  return assetTypeResults;
50}
51
52async function downloadBase64FromURL(url, access_token, callback) {
53  return new Promise((resolve, reject) => {
54    https
55      .get(url, { headers: { Authorization: "Bearer " + access_token } }, (resp) => {
56        resp.setEncoding("base64");
57        let imageBody = "";
58        resp.on("data", (data) => {
59          imageBody += data;
60        });
61        resp.on("end", () => {
62          console.log("end");
63          resolve(imageBody);
64        });
65      })
66      .on("error", (e) => {
67        reject(`Got error: ${e.message}`);
68      });
69  });
70}
71
72async function createMCAsset(access_token, assetBody) {
73  return new Promise((resolve, reject) => {
74    request.post(
75      mc_host + mc_assets_api_path,
76      {
77        headers: {
78          Authorization: "Bearer " + access_token,
79        },
80        json: assetBody,
81      },
82      (error, res, body) => {
83        if (error) {
84          console.error(error);
85          console.log({ assetBody });
86          reject({ error });
87        } else {
88          console.log(`statusCode: ${res.statusCode}`);
89          console.log(body);
90          resolve(res);
91        }
92      },
93    );
94  });
95}

API Response 

In response to the API request, the API returns an HTTP response code and a JSON body with an object representing the newly created asset in Marketing Cloud. For details on the response object and possible HTTP response codes, see Marketing Cloud API: REST API v1 Reference.

See Also