Search code examples
jqueryjsonrestmarketo

JSON format when using Marketo REST API to create Custom Object record


I'm trying to create a custom object record using the rest API, but I am not sure what my JSON object should look like. I have two fields I need to dedupe (personID and emailAddress) and then few fields (question01, question02)

Looking here I can see where to send the JSON, but I don't understand what the format should look like. I'll be posting the data using jQuery's ajax function http://developers.marketo.com/rest-api/endpoint-reference/lead-database-endpoint-reference/#!/Custom_Objects/syncCustomObjectsUsingPOST

I also don't understand when or how to get a new token. Maybe that should be for another thread though.


Solution

  • First thing first:
    Unless you are using jQuery under node.js, you won't be able to use client side javascript to access Marketo REST API, as these requests will be blocked due to CORS. Also, your Client Secret needed to generate an Access Token would not remain secret any more at the client side. So, you have to do this from your server.

    The payload:
    The payload in question is better documented under the REST API / Lead Database / Custom Objects section of the API documentation.
    In your case it would look something like this:

    {
        'action'    : 'createOrUpdate',// optional
        'dedupeBy'  : 'dedupeFields',// optional
        // input is an array of objects containing the custom object fields
        'input'     : [
            {
                'personID'      : 'personID',
                'emailAddress'  : 'emailAddress',
                'question01'    : 'question01 value',
                'question02'    : 'question02 value'
            },
            // …Other items…
        ]
    }
    

    which should be in the body of the request. The sample code below is an illustration how you would do it with jQuery, but again, it won't work from the client side.

    var instanceId = '123-ABC-456',
        accessToken = 'ACCESS_TOKEN',
        customObjectName = 'customObjectName_c',
        payload = payloadFromAbove;
    
    $.ajax({
        // Constructing url with ES6 String Interpolation
        url: `https://${instanceId}.mktorest.com/rest/v1/customobjects/${customObjectName}.json?access_token=${accessToken}`,
        method: 'POST',
        data: JSON.stringify(payload),
        dataType: 'json',
    })
    .done(function(response) {
        console.log(response);
    });