3

I am trying to learn how to use the Journey Builder API to enter a record from a Cloud Page into a Journey. I am able to get my access token from the auth endpoint but when I make the API request to the Journey Builder API I get a 400 Bad Request error.

This attempt is super simple. The Data Extension has just 1 field, ContactKey. I've ensured that the Event Definition Key in the Journey matches what is in my code. Finally, the installed package has an API Integration Component that is of type Server-to-Server with scope of "journeys_read" and "list_and_subscribers_read"

Can someone please tell me what I am missing or doing wrong?

%%[

    set @url = 'https://mySubdomainHere.auth.marketingcloudapis.com/v2/token'
    set @content = 'application/json'
    set @payload = '{
                      "grant_type": "client_credentials",
                      "client_id": "xxxxxxxxxxxx", 
                      "client_secret": "yyyyyyyyyyyy"
                    }'
    var @response, @callstatus

    set @HTTP1 = HTTPPost2(@url, @content, @payload, true, @response, @callstatus)

]%%

<script runat="server" language="javascript">
  var str = Platform.Variable.GetValue('@response');
  var obj = Platform.Function.ParseJSON(str);
  var accessToken = obj.access_token;

  Platform.Variable.SetValue("@accessToken", accessToken);
</script>

%%[
    set @accessToken = Concat('Bearer ', @accessToken)

    set @url2 = 'https://mySubdomainHere.rest.marketingcloudapis.com/interaction/v1/events/'
    set @content2 = 'application/json'
    set @payload2 = '{
                        "ContactKey": "emailAddress@example.com",
                        "EventDefinitionKey": "APIEvent-yada-yada-yada", 
                        "EstablishContactKey": true
                     }'
    var @response2, @callstatus2

    set @HTTP2 = HTTPPost2(@url2, @content2, @payload2, true, @callstatus2, @response2, 'Authorization', @accessToken)

]%%
1

Figured it out.

Because the API Integration Package was installed in a parent business unit and a "account_id" was not defined in the token payload, the API Request the Journey Builder API assumed that the Journey was in the parent business unit. Since the Journey was in a child, a 400 occurred.

| improve this answer | |
0

ContactKey in the payload is always required and I do not believe it will map it to a DE with a similar name field automatically.

Try firing the event with ContactKey specified in the payload Data.

 set @payload2 = '{
                    "ContactKey": "emailAddress@example.com",
                    "EventDefinitionKey": "APIEvent-yada-yada-yada", 
                    "EstablishContactKey": true,
                    "Data": {
                         "ContactKey": "emailAddress@example.com"
                     }
                 }'
| improve this answer | |
  • Hi Mike, this continues to return a (400) Bad Request. It is my understanding that the API Request adds the record to the DE, correct? Or does the record have to already exist for it to map to a Data field? – Zach Sep 11 '19 at 14:32
  • It adds it. There needs to be a property in the Data property of the request for each non-nullable column in the Data Extension. So if you have other columns in your DE, you'll need to have properties for those, as well. I'll take another look at your request. – Mike Sep 11 '19 at 14:34
  • fwiw, the full error message is: {"message":"An error occurred when attempting to evaluate a HTTPPost2 function call. See inner exception for details.","description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost2 function call. See inner exception for details.\r\n Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n --- inner exception 1---\r\n\r\nSystem.Net.WebException: The remote server returned an error: (400) Bad Request. - from System\r\n\r\n\r\n\r\n"} – Zach Sep 11 '19 at 15:03

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.