1

I am trying to send e-mail campaign mails with the SMTP API of Sendinblue.com, a cloud-based marketing communication software suite. I am running my configuration of the template script provided by Sendinblue as seen on the screenshot: Sendinblue error: key not found

My initial script was as follows:

# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-.................................' # full api key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "------@gmail.com"}, # personal email with which I was registered @ sendinblue
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()
)
# Make the call to the client\
try:
    api_response = api_instance.create_email_campaign(email_campaigns)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
    

I referred to Sendinblue Documentation in the cases where I had doubt. Most of the process seems self-explanatory, just the line with configuration.api_key['api-key'] = 'YOUR API KEY' was ambiguous as it is not quite clearly stated how exactly the api-key should be passed and the attribute of api_key I assumed api_key was holding the name of the API as defined in the SMTP & API Advanced tab. Even if I assume it should be holding some other values, it is not clear what they should be, what I can tell for sure is that sib_api_v3_sdk.configuration.api_key['api-key'] also resulted in AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key'.

After initially getting the error AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key' I researched a dozen of articles on StackOverflow, one of them on the Kubernetes topic where the script seemed to throw a similar error like the one I was getting, so I followed the advices described in Python kubernetes module has no attribute 'api_key'. Therefore, I tried reassigning the class attributes as follows:

configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()

The fact that now I wasn't getting the error of a missing key but facing an 'unauthorized' message cheered me up for a short time, until I spent several hours trying to get past this. So the script that's now throwing the HTTP response body: {"message":"Key not found","code":"unauthorized"} is now this:

# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-c..............' # key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "mail....@gmail.com"}, # personal gmail with which I was initially registered @ sendinblue.com
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()      
)
# Make the call to the client\
try:
    api_response = api_instance.create_email_campaign(email_campaigns)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
    

My 1st question is: was my 1st script wrong and in what way? Secondly: Is configuration.api_key['api-sb'] = myapikey the correct syntax, assuming that 'api-sb' is the name of my API key as shown on the screenshot? And third: In the myapikey variable, when assigning the API key itself, should there be anything else as a prefix to the key?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
apingaway
  • 33
  • 1
  • 1
  • 17

2 Answers2

1

Try this sample transactional email script. Worked for me in python.

https://developers.sendinblue.com/reference/sendtransacemail

extensa5620
  • 681
  • 2
  • 8
  • 22
0

Instead of this:

sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()

do this:

configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-sb'] = myapikey

api_instance = sib_api_v3_sdk.EmailCampaignsApi(sib_api_v3_sdk.ApiClient(configuration))

as shown in https://developers.sendinblue.com/reference/createemailcampaign-1.

aaron
  • 39,695
  • 6
  • 46
  • 102