marketo

How do i add a lead (email, name) against a list in Marketo via API


Marketo API is so confusing for a new user like myself. I have an email and name and would love to pass that to marketo. How do I do that?


Solution

  • To push a lead record to Marketo via the REST API, there are a couple of endpoints you can choose from:

    Out of these three, the Sync Leads might be the easiest to use, as that requires the least amout of additional parameters.
    Basically, you have to make a POST request to the
    https://<MUNCHKIN_ID>.mktorest.com/rest/v1/leads.json?access_token=<ACCESS_TOKEN> url with your data sent in the body of the request.

    You will find your MUNCHKIN_ID under Admin > Integration > Munchkin tab in your instance. While still in the admin area, you should also create an API user (or allow API access for your own user), set up a LaunchPoint service with that user for the REST API and finally request an –temporary, valid for 1 hour– access token to test the connection. The whole process is described in detail under the Authentication chapter of the REST API documentation.

    Once you have a –still valid– access token, you can make the call above with your lead information provided in the following data structure:

    {  
       "action":"createOrUpdate",
       "lookupField":"email",
       "input":[  
            {  
                "email":"collizo@4sky.com",
                "firstName":"Collizo4sky"
            },
            // …more leads (up to 300) if needed
        ]
    }
    

    In case you use php, here is an example code:

    $munchkinId = '123-ABC-456';
    $accessToken = 'abcdefgh-1234-5678-abcd-12345678abcd:lon';
    
    $url = "https://{$munchkinId}.mktorest.com/rest/v1/leads.json?access_token={$accessToken}";
    $dataJSON = [
        'action'      => 'createOrUpdate',
        'lookupField' => 'email',
        'input'       => [  
            [
                'email'     => 'collizo@4sky.com',
                'firstName' => 'Collizo4sky',
            ],
        ],
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataJSON));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($ch);
    curl_close($ch);
    
    var_dump($response);
    
    /**
     * Which should result in a response like this:
     * / 
    {
        "requestId":"4033#16612d185ad",
        "result":[
            {
                "id":1042016,
                "status":"created"
            }
        ],
        "success":true
    }
    /**/