2
  1. I have successfully get access token via ssjs
  2. When passing it to POST /interaction/v1/events (https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/postEvent.htm) i'm getting error message "ContactKey is required"

SSJS Post Request:

var payload = {
            ContactKey: subscriberKey,
            EventDefinitionKey: EventDefinitionKey,
            EstablishContactKey: true, 
            Data: { 
                EmailAddress: emailAddress
            }
        }
        var url = 'https://www.exacttargetapis.com/interaction/v1/events';
        var auth = 'Bearer ' + accessToken;

        var req = new Script.Util.HttpRequest(url);
        req.emptyContentHandling = 0;
        req.retries = 2;
        req.continueOnError = true;
        req.contentType = "application/json"
        req.setHeader("Authorization", auth);
        req.method = "POST";
        req.postData = payload;

        var resp = req.send();

Sample Request:

{
    "ContactKey":"[email protected]",
    "EventDefinitionKey":t"APIEvent-xxx",
    "Data": {
        "field1":"value1" 
    }
}

Sample response:

{
    "message": "ContactKey is required.",
    "errorcode": 10000,
    "documentation": ""
}

Why I'm getting that error message? Any help would be appreciated.

5
  • 1
    Not sure if that is the error, but if you want to use an ampscript variable in ssjs you need to use the GetValue Function to do so.
    – DonL
    Jun 14, 2019 at 14:00
  • Hi @DonL, thank you for your response. I updated the parameter i used for contact key. My script is written in ssjs. I hardcoded the parameters i used to test but still getting the error “ContactKey is required”
    – Feux
    Jun 14, 2019 at 18:20
  • Can you post your full script (no need for the auth token generation part)? Jun 14, 2019 at 19:52
  • @JasonHanshaw, please see above, added post script on my question. Thank you
    – Feux
    Jun 15, 2019 at 5:43
  • The code you provided is inconsistent. Does your SSJS POST Request Payload have double quotes in the JSON or not? If not, please add them.
    – DonL
    Jun 21, 2019 at 14:11

4 Answers 4

1

What is the content type of your header? It should be 'application/json'. A different type will cause Salesforce to return this misleading error message.

1
  • I forgot to set Content-type -> Application-json on header. Great man! Aug 5, 2020 at 19:42
1

Does your Data Extension have field named "ContactKey"?
If yes, you should add "ContactKey":"[email protected]" in "Data".

Your request body should look like this:

{
    "ContactKey":"[email protected]",
    "EventDefinitionKey":"APIEvent-xxx",
    "Data": {
        "ContactKey":"[email protected]"
        "field1":"value1" 
    }
}
1
  • I believe this to be the correct answer here. I was able to replicate the error message, when I had "ContactKey" field in the DE. Sep 8, 2023 at 7:38
0

Use proper id posting request example here. {"EventDefinitionKey":"APIEvent-ba299b28-102a-d953-09bd-09bdf8e117e6","ContactKey":"ID601"}

0

Your json request is not in valid json format. Salesforce is trying to parse what you're providing, ContactKey is just the first thing - it's not where the actual problem is. Display or log your json in the console, copy it, download the free Postman HTTP application if you don't already have it, paste it in the 'Body' tab in Postman, and see if Postman shows an error. See if you can submit your request in Postman.

You must log in to answer this question.

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