Retrieve all Data Extensions across all running journeys

marketing-cloudmarketingcloudapirestssjs

I found out on this forum there is an option with Marketing Cloud API to retrieve all the Data Extensions associated with Journeys.

When using Postman, the request below works perfectly

GET /interaction/v1/eventDefinitions/
Host: xxx.rest.marketingcloudapis.com
Authorization: Bearer xxx

It returns an object that I can query without issues.

However, when trying to run the same script as part of a SSJS Activity in Automation Studio, it just fails.

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

        var authEndpoint = 'https://xxx.auth.marketingcloudapis.com';
        var payload = {
            clientId: "xxx",
            clientSecret: "xxx"
        };
        var authurl = authEndpoint + '/v1/requestToken';  
        var contentType = 'application/json';   

        var accessTokenRequest = HTTP.Post(authurl, contentType, Stringify(payload));
        
        if(accessTokenRequest.StatusCode == 200) {
            var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
            var accessToken = tokenResponse.accessToken;
            var auth = "Bearer " + accessToken;

        }
    
    }

    /* Get the Journeys details */
    
    try {
        
        var deEndpoint  = "https://xxx.rest.marketingcloudapis.com/interaction/v1/eventDefinitions/";
        
        var HeaderNames = ["Authorization"];
        var HeaderValues = [Auth]

        var deRequest       = HTTP.Get(deEndpoint, HeaderNames, HeaderValues);
        var requestResponse = Platform.Function.ParseJSON(deRequest.Response[0]);

        Write("Object: "+ requestResponse);
        
    } catch (err) {
        Write(Stringify(err));
    }
    </script>

After executing the Activity, it returns Object: null

I couldn't find any information in the documentation about this API though:
GET /interaction/v1/eventDefinitions/, only from comments on this forum.

How to get Data Extension ID using Journey ID

Is there an alternative way to run the same request? Thank you

Best Answer

this code will return all the journeys. I will share the code to fetch DE names later. Please refer to the documentation link: https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/getInteractionCollection.html

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

    var payload = '{"grant_type":"client_credentials",';
            payload += '"client_id":"xxxx",';
            payload += '"client_secret":"xxxx",';
            payload += '"account_id":"xxxx"}';
      var OAuth2URL = "https://xxxx.auth.marketingcloudapis.com/v2/token";
      var contentType = 'application/json';
      var accessTokenResult = HTTP.Post(OAuth2URL, contentType, payload);
      var tokenObj = Platform.Function.ParseJSON(accessTokenResult["Response"][0]);
      var accessToken = tokenObj.access_token;
      var auth = "Bearer " + accessToken;

/* Get the Journeys details */

try {
    
    var deEndpoint  = "https://xxxx.rest.marketingcloudapis.com/interaction/v1/interactions";
  
  var req = new Script.Util.HttpRequest(deEndpoint);
      req.emptyContentHandling = 0;
      req.retries = 2;
      req.continueOnError = true;
      req.contentType = "application/json";
 req.setHeader("Authorization", auth);
      req.method = "GET";
      var resp = req.send();
 var resultString = String(resp.content);
 var resultJSON = Platform.Function.ParseJSON(String(resp.content));
 Write(resultString);
    
} catch (err) {
    Write(Stringify(err));
}
</script>
Related Topic