0

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

1
  • What is the value of the variable deRequest? also take into account that auth variable is lowercase when you retrieve the token and you wrote "var HeaderValues = [Auth]" with uppercase.
    – SebOB
    Mar 31, 2022 at 10:06

2 Answers 2

1

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>
2
  • Thank you Dennon - You are right, I saw this API in the Documentation, but when I was looking at the response, I couldn't find the associated Data Extension. If you have the code to fetch the DE name, that would be fantastic! Cheers
    – nerran
    Mar 31, 2022 at 21:11
  • Happy to know that the code worked for you. Thanks for the vote :)
    – Dennon
    Apr 5, 2022 at 12:47
0

Thank you Dennon - Actually, I have changed the API with /interaction/v1/eventDefinitions and your code works like a charm!!!

Might be useful for other developers

try {
                
    var apiURL      = "https://xxx.rest.marketingcloudapis.com/interaction/v1/eventDefinitions";
    var req = new Script.Util.HttpRequest(apiURL);
    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 (apiError) {
    
    Write(Stringify(apiError));
}
1
  • Please mark Dennon's answer as solving your issue, resp. upvote it, instead of praising it in another answer. Mar 31, 2022 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .