2

I want to use SendinBlue API on my website but I am facing two problems. I want to create a new contact from an email and add this new contact to a contact list on the SendinBlue website. In order to do it, I am following this page https://developers.sendinblue.com/reference/createcontact

First problem: When I am trying to execute the code, an error occured telling me that the key is already in the dictionnary on this line (I obviously replaced "YOUR API KEY" by v3 API Key) :

Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

Second problem: After that, when I am addign the new created contact to the API, I have an error on this line telling me that "One or mutliple erros occured" :

CreateUpdateContactModel result = apiInstance.CreateContact(createContact);

Here is my code:

// I replaced "YOUR API KEY" with my private api key
sib_api_v3_sdk.Client.Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");
var apiInstance = new ContactsApi();
JObject attributes = new JObject();
List<long?> listIds = new List<long?>();
listIds.Add(2);
bool emailBlacklisted = false;
bool smsBlacklisted = false;
bool updateEnable = true;
List<string> smtpBlacklistSender = new List<string>();
try
{
    var createContact = new CreateContact(email, attributes, emailBlacklisted,
                            smsBlacklisted, listIds, updateEnable, smtpBlacklistSender);
    CreateUpdateContactModel result = apiInstance.CreateContact(createContact);
    Debug.WriteLine(result.ToJson());
    Console.WriteLine(result.ToJson());
    Console.ReadLine();
}
catch (Exception exc)
{
    Debug.WriteLine(exc.Message);
    Console.WriteLine(exc.Message);
    Console.ReadLine();
}

Does anyone already faced one of this problem ?

Thank you

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
ElliotCK
  • 21
  • 2

1 Answers1

1

First issue: You are trying to add new value to the dictionary, where the value in dictionary already exists. It could be initialized from config file, or input from any previous run? You should check if the value is present, if missing - you will add it:

// Instead of this
Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

// You should do something like this
// Note that there might be different method to be called, you have to check what is available
if(!Configuration.Default.ApiKey.HasKey("api-key"))
    Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

Second issue:

One ore multiple errors occured

Usually this exception is thrown as Multi-exception, that groupes them together. What you need is to check what is inside the InnerException property or get StackTrace. From that you should be able to see details, what is the problem.


Note for the end, I would recommend following examples on the github since it seems you are using their library for C#.

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47