0

I am thinking of performing an API call to external webservice(I will collect the payload from a DE) once API call is done and response is received I would be saving the API response to the data extension, I have 2 options.

  1. I can update a field on the source DE
  2. I can pull all the values from source DE and update them on the target DE.

I have not built it yet, wanted to know if this is even possible, I mean what should be the workflow, it would kind of you if you can point out some platform functions too.

Thanks

2

This is definitely do-able! Short answer, you would use the HTTP Request functions to call your API and return it as a variable. You then can try to parse that returned rowset/JSON to individual values or just load the entire Stringified value to a Data Extension field.

Long version. First I would recommend using SSJS as likely your API response is going to be a JSON. (if it is XML, then I would instead recommend AMPScript since it has BuildRowsetFromXML() function.)

For AMPscript though, you would be limited to the built in functions of HTTPGET AND HTTPPOST. These exist inside of SSJS as well, but SSJS has what I consider the best method to make API calls.

To make the call, I would recommend these two articles (this and this) from my blog for the details, but you essentially want to build out an HTTP Request call using whichever method required (GET, POST, PUT, etc) via the Script.Util

example GET function:

function getUser(host,token,userID) {

    var url = host + '/api/users/' + userID;
    var req = new Script.Util.HttpRequest(url);
    req.emptyContentHandling = 0;
    req.retries = 2;
    req.continueOnError = true;
    req.contentType = "application/json"
    req.method = "GET";
    req.setHeader("Authorization", token);

    var resp = req.send();

    var resultStr = String(resp.content);
    var resultJSON = Platform.Function.ParseJSON(String(resp.content));

    return resultJSON;
}

Which should give you a response (assuming it is a JSON) of something like:

{
  "key": "value",
  "key2":"value2",
  "array": ["value3","value4"]
}

Now, realize that this is returned as an Object (or Array), not a String, so if you try to Write it or otherwise output it, it will return as undefined. So to write out the whole thing, or push it to a data extension as a whole, you will need to Stringify() it first. Like so:

var respStr = Stringify(myResp);

Note, that above uses the Core library version of the function, which requires you to load it via Platform.Load("Core","1.1.1"). If you don't need the Core library, you can call Stringify via the Platform version like so: Platform.Function.Stringify() instead.

If you need to parse through it instead, you will need to use the following to collect information from the JSON.

For JSON Arrays var arr = [z,b,c] you will need to call based off of ordinal value (starting at 0). For instance, var f = arr[0] would output z.

For JSON Objects var obj = {"key":"value","key2":"value2"} you will need to call based off of the 'key' value (or name of the object property) to get the value. For instance, var f = obj.key would output value.

You can also combine this, to something like (based off the example output from our get function: var f = resp.array[1] which would output value4.

There are some other functions out there to manipulate, sort or otherwise interact with JSON - but keep in mind that SFMS SSJS is run on ECMAScript 3 - so a lot of the more advanced ones will not work in it.

To upsert into a DE, I would use UpsertData() if you are using AMPScript, or the SSJS version of UpsertData here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.