1

Using Sendinblue API, I get a Bad Request response: {"code":"invalid_parameter","message":"Invalid emails format"}when I'm trying to use the functionAddContactToList`.

I have no problem when trying to create contact. Contact is sent with emails and attributes. I want now to add existing contacts in a list.

This is the code of the add contact to list page:

// Configure API key authorization: api-key
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'xxxxxxxxxxxxxxxxxx');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api-key', 'Bearer');
// Configure API key authorization: partner-key
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', 'xxxxxxxxxxxxxxxxxxxxxxx');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('partner-key', 'Bearer');

$apiInstance = new SendinBlue\Client\Api\ContactsApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);

$row = 0;
if (($handle = fopen("contactlist.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {

            $row++;

            usleep(1000);

            if($row>70){

                $listId = 42; // int | Id of the list
                $contactEmails = new \SendinBlue\Client\Model\AddContactToList(); // \SendinBlue\Client\Model\AddContactToList | Emails addresses of the contacts
                $contactEmails['emails'] =$data[1];
                echo $data[1];echo '<br>';
                try {
                    $result = $apiInstance->addContactToList($listId, $contactEmails);
                    print_r($result);
                } catch (Exception $e) {
                    echo 'Exception when calling ContactsApi->addContactToList: ', $e->getMessage(), PHP_EOL;
                }

                echo '<br>';echo '<br>';echo '<br>';
            }

            if($row==400){
                die();
            }
    }
    fclose($handle);
}

?>

This is the result I get:

xxxxx@example.com Exception when calling ContactsApi->addContactToList:

[400] Client error: POST https://api.sendinblue.com/v3/contacts/lists/42/contacts/add resulted in a 400 Bad Request response: {"code":"invalid_parameter","message":"Invalid emails format"}

Thank you for your help.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76

1 Answers1

0

Here is my working code :

require_once(__DIR__ . '/vendor/autoload.php');

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR-APIKEY');
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', 'YOUR-APIKEY');

$apiInstance = new SendinBlue\Client\Api\ContactsApi(
  new GuzzleHttp\Client(),
  $config
);

$createAttribute = new \SendinBlue\Client\Model\CreateAttribute([
  'name'      => 'Your Name',
  'type'      => 'email' // email, sms
]);

$createContact = new \SendinBlue\Client\Model\CreateContact([
  'email' => 'test@email.be',
  'attributes' => $createAttribute,
  'listIds' => [28], // Int() | Number of your list in your account
]); // \SendinBlue\Client\Model\CreateContact | Values to create a contact

try {
  $result = $apiInstance->createContact($createContact);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling ContactsApi->createContact: ', $e->getMessage(), PHP_EOL;
}
Rem
  • 1
  • 2