Sendgrid-python: Doesn't work with new Marketing Campaigns?

Created on 18 Jul 2019  路  10Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

Trying to insert new contacts using:


sg = SendGridAPIClient(EnvironmentAPI.get_env_var('SENDGRID_API_KEY'))

response = sg.client.mail.send.post(request_body=mail_data)

That ends up in the "legacy" marketing campaigns side. I opened a support ticket asking about that and it seems there's no support for the new marketing campaign with this library? I'd really rather not have 2 different ways of contacting sendgrid's api. It works just fine for basically everything else, like actually sending emails, but this one thing doesn't seem to work properly, and its a pretty big thing.

Technical details:

  • sendgrid-python Version: 6.0.5
  • Python Version: 2.7
unknown or a waiting for feedback question

Most helpful comment

Sendgrid implements a Fluent interface, hence the API for new marketing campaigns should work just fine if you use the appropriate endpoints/verbs. You can find them in official, language-agnostic documentation for the API.

The endpoint you're looking for:
https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

Examples:

Add a new contact:

sg.client.marketing.contacts.put(
    request_body=dict(
        contacts=[{'email': '[email protected]'}]
    )
)

Add contact to a list:

sg.client.marketing.contacts.put(
    request_body=dict(
        list_ids=[list_id], 
        contacts=[{'email': '[email protected]'}]
    )
)

and so on


Still, none of that is mentioned in this repository - neither in the readme, nor in examples section.

Documentation for this library needs to be updated to include information about the new API and how to use it with python.

All 10 comments

Hello @jer-tx,

I believe you may be trying to do this? Please confirm.

Thanks!

With best regards,

Elmer

Dear @thinkingserious, I have tried to add a recipient to the list the way that is written in the link you attached, but I get this error
LIST ID must be integer

I get a _403: forbidden_ error when using .contactdb.recipients.post(). Same story using Postman. If I use the new _/marketing/_ endpoints, they do respond. So I'm not totally sure if this library currently supports the new endpoints. The docs only mention _/contactdb/_ ones.

Can confirm. Still not solved.

Sendgrid implements a Fluent interface, hence the API for new marketing campaigns should work just fine if you use the appropriate endpoints/verbs. You can find them in official, language-agnostic documentation for the API.

The endpoint you're looking for:
https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

Examples:

Add a new contact:

sg.client.marketing.contacts.put(
    request_body=dict(
        contacts=[{'email': '[email protected]'}]
    )
)

Add contact to a list:

sg.client.marketing.contacts.put(
    request_body=dict(
        list_ids=[list_id], 
        contacts=[{'email': '[email protected]'}]
    )
)

and so on


Still, none of that is mentioned in this repository - neither in the readme, nor in examples section.

Documentation for this library needs to be updated to include information about the new API and how to use it with python.

Documentation for this library needs to be updated to include information about the new API and how to use it with python.

I agree that documentation lacks any examples. Right now I'm struggling with

sg.client.marketing.contacts.search.post(request_body= dict(query=[{'email': '[email protected]'}]))

And get json: invalid format. Any ideas how to search correctly?

SendGrid V3 Search

@Urazbaev The query value needs to a a string (SGQL string according to the docs). An example string in the docs: email LIKE 'ENTER_COMPLETE_OR_PARTIAL_EMAIL_ADDRESS_HERE%' AND CONTAINS(list_ids, 'YOUR_LIST_IDs')

So maybe:

sg.client.marketing.contacts.search.post(request_body= dict(query='email = "[email protected]"'))

sg.client.marketing.contacts.search.post(request_body= dict(query='email = "[email protected]"'))

It worked! Thanks @childish-sambino

I'm having trouble understanding how we should be structuring the Fluent Interface calls with a parameter in the url, like this API call: https://sendgrid.api-docs.io/v3.0/lists/remove-contacts-from-a-list

How can I structure the client to call this /marketing/lists/{id}/contacts?

@aaronn Some examples here: https://github.com/sendgrid/python-http-client#quick-start

sg.client.marketing.lists._(id).contacts.delete()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sohamnavadiya picture sohamnavadiya  路  4Comments

MadReal picture MadReal  路  4Comments

thinkingserious picture thinkingserious  路  3Comments

danielghurley picture danielghurley  路  4Comments

DougCal picture DougCal  路  3Comments