Add contact in a list of SendGrid via API

Following script will add the contact in to the sendgrid list.

$ListId = 'YOUR-LIST-ID';
$reciepent_id = 'YOUR-CONTACT-ID';
$curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.sendgrid.com/v3/contactdb/lists/".$ListId."/recipients/".$reciepent_id,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_SSL_VERIFYPEER=>0,
        CURLOPT_SSL_VERIFYHOST=>0,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "{\r\n  \"list_id\": \"".$ListId."\",\r\n  \"recipient_id\": \"".$reciepent_id."\"\r\n}\r\n",
        CURLOPT_HTTPHEADER => array(
            "authorization: Bearer SG.{TOKEN}",
            "cache-control: no-cache",
            "content-type: application/json",
            "postman-token: dbde70ca-f735-d7c6-0fca-e0f9db188fec"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }

Above script require $reciepent_id first. For that you need to create the contact first in your sendgrid then you will have the contact unique id in return. You can follow my tutorial of how to create a contact in sendgrid simply by clicking on the link below.

Add contact in SendGrid via API