Marketo REST API - Smart Campaign

464 views Asked by At

I can create a new Marketo lead using an html form and the attached script (PHP + Rest API). It mostly works, the leads show up in Marketo.

The problem is, my Marketo setup requires new leads to be added to a particular "Smart Campaign".

On Marketo's Rest API documentation, I found an endpoint for adding leads to a List, but not for adding to a Campaign. Have you run into this problem?

class UpsertLeads{
    //these are the CIN Marketo credentials
    public $host = "####";//CHANGE ME
    public $clientId = "####";//CHANGE ME
    public $clientSecret = "####";//CHANGE ME
    public $input; //an array of lead records as objects
    public $lookupField; //field used for deduplication
    public $action; //operation type, createOnly, updateOnly, createOrUpdate, createDuplicate

    public function postData(){
        $url = $this->host . "/rest/v1/leads.json?access_token=" . $this->getToken();
        $ch = curl_init($url);
        $requestBody = $this->bodyBuilder();
        //commenting out
        //dont need to output this stuff in production
        //print_r($requestBody);
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
        curl_getinfo($ch);
        $response = curl_exec($ch);
        return $response;
    }

    private function getToken(){
        $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        $token = $response->access_token;
        return $token;
    }

    private function bodyBuilder(){
        $body = new stdClass();
        if (isset($this->action)){
            $body->action = $this->action;
        }
        if (isset($this->lookupField)){
            $body->lookupField = $this->lookupField;
        }
        $body->input = $this->input;
        $json = json_encode($body);
        return $json;
    }

    private static function csvString($fields){
        $csvString = "";
        $i = 0;
        foreach($fields as $field){
            if ($i > 0){
                $csvString = $csvString . "," . $field;
            }elseif ($i === 0){
                $csvString = $field;
            }
        }
        return $csvString;
    }
}
1

There are 1 answers

0
dferenc On BEST ANSWER

It is not possible to put your new Leads directly to a Smart Campaign via the API.
However, –as you noted– you can push them to a List, which is halfway to success. From there, the only thing you need to do is to configure the Smart Campaign in question to pick up the Leads from the List you populate.

You can do that on the Smart List tab of the Smart Campaign by setting up the Added to List trigger and point it to your List. Screenshot attached.

This way the new Lead will be immediately added to the Smart List as well.

'Added to List' trigger of Smart Campaign