[SalesForce] How to create a contact through the marketing cloud REST API

Every request I make to SFMC's API for contact creation seems to result in a 500. I am following the documentation provided to us and I have included the example requests below. The only thing I could think of is that there is a bug or a permission issue. I can't fathom another reason to respond with a 500.

Note: I am focusing on the contact create/update (upsert if you will) call here and I have tried this same request with a real email and get the same 500 response. Also note that the link they provide in the response is dead smh :/

Attempt – Using /contacts/v1/contacts

Example request:

    Host: https://www.exacttargetapis.com
    POST /contacts/v1/contacts
    Content-Type: application/json
    Authorization: Bearer ${AUTH_TOKEN}

    {
      "contactKey": "local-97005",
      "attributeSets": [
        {
          "name": "Email Addresses",
          "items": [
            {
              "values": [
                {
                  "name": "Email Address",
                  "value": "sfmctest2@mailinator.com"
                }
              ]
            }
          ]
        }
      ]
    }

    Example response:

    HTTP 500
    {
      "message":"Internal Server Error",
      "errorcode":0,
      "documentation":"https://code.docs.exacttarget.com/rest/errors/500"
    }

cURL Request

    curl -X POST -H "Authorization: Bearer <token removed for posting>" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 5fd50d70-f8ec-814a-d01b-c15649d24681" -d '{
        "contactKey": "local-123452",
        "attributeSets": [{ 
            "name": "Email Addresses", 
                "items": [{
                    "values": [{
                        "name": "Email Address",
                        "value": "testmcapi@mailinator.com"
                    }]
                }]
        }]
    }' "https://www.exacttargetapis.com/contacts/v1/contacts"

EDIT:

Here are data access configurations for the token
Data

Second EDIT

I tried adding HTML Enabled to the request as it is noted as a required field in the documentation, however, I now recieve a 400 Bad Request.

Request

curl -X POST -H "Authorization: Bearer <Token Omitted>" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 797d8be2-a14b-5b25-a620-25540a264de6" -d '{ "contactKey": "local-123456", "attributeSets": [{ "name": "Email Addresses", "items": [{ "values": [{ "name": "Email Address", "value": "sfmctest2@mailinator.com" },{ "name": "HTML Enabled", "value": true }] }] } ] }' "https://www.exacttargetapis.com/contacts/v1/contacts"

Response

  {
    "operationStatus": "FAIL",
    "rowsAffected": 0,
    "requestServiceMessageID": "cebddc10-69ab-4399-92b8-ced05df00a1b",
    "hasErrors": true,
    "resultMessages": [
      {
        "formatStringParams": [
          "ValueDefinition",
          null,
          null,
          "HTML Enabled"
        ],
        "messageFormatString": "Unable to resolve schema element of type: {0} by reference. [Id: {1}, Key: {2}, Name: {3}]",
        "resultType": "Validation",
        "resultClass": "Error",
        "resultCode": "SchemaObjectNotFoundByReference",
        "message": "Unable to resolve schema element of type: ValueDefinition by reference. [Id: , Key: , Name: HTML Enabled]"
      }
    ],
    "serviceMessageID": "075ac816-e226-4698-a969-e082efdcf5c0"
  }

Best Answer

As Mohith comments, HTML Enabled is a required attribute, however you shouldn't receive a 500 error. Here's a working request:

Host: https://www.exacttargetapis.com
POST /contacts/v1/contacts
Content-Type: application/json
Authorization: Bearer {{accessToken}}

{  
   "contactKey":"6dcb271e-06db-4100-bebb-b141469cd4d1",
   "attributeSets":[  
      {  
         "name":"Email Addresses",
         "items":[  
            {  
               "values":[  
                  {  
                     "name":"Email Address",
                     "value":"simon@sausage.com"
                  },
                  {  
                     "name":"HTML Enabled",
                     "value":true
                  }
               ]
            }
         ]
      }
   ]
}

This returns the response payload:

{
  "operationStatus": "OK",
  "rowsAffected": 1,
  "contactKey": "6dcb271e-06db-4100-bebb-b141469cd4d1",
  "contactID": 12345678,
  "contactTypeID": 0,
  "isNewContactKey": true,
  "requestServiceMessageID": "23ba75f2-ab1c-4b67-93dd-08de25f28c42",
  "hasErrors": false,
  "resultMessages": [],
  "serviceMessageID": "673451c6-ce6b-4329-b1bf-6e1e1664803e"
}

If you are still having issues, please double-check that you have Write permission enabled in Contacts > List And Subscribers in your app (in App Center).

Related Topic