15

I'm trying to update my SendGrid contacts and can't figure out why my attempts to update my contacts' custom fields are not working. My reserved fields (first_name, last_name, email) update, but my custom fields do not. Any ideas why?

Documentation here: https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

    try:
        headers = {
            'authorization': f"Bearer {settings.SENDGRID_API_KEY}",
        }
        data = {
            "list_ids": [
                # "Users" list
                "7c2...d20"
            ],
            "contacts": [{
                "email": user.email,
                "first_name": user.first_name,
                "last_name": user.last_name,
                "custom_fields": {
                    "educator_role": user.educator_role,
                }
            }]
        }

        response = requests.put("https://api.sendgrid.com/v3/marketing/contacts", headers=headers, data=json.dumps(data))
        if(response.status_code != 202):
            capture_message(f"Could not add user with email {user.email} to Sendgrid.", level="error")
    except:
        capture_message(f"Adding/updating SendGrid contact failed for {user.email}.", level="error")```
Nicole Hemenway
  • 563
  • 5
  • 13
  • 4
    It's startling that this is not mentioned AT ALL in the docs. Found this github thread on the same topic, cross-linking... https://github.com/sendgrid/sendgrid-nodejs/issues/953 – doublejosh Jul 08 '21 at 21:36

2 Answers2

27

Unlike reserved fields, updating a custom field requires you pass the custom field id instead of the field name in your call. So instead of educator_role, use the id, it will be something random like e1_T.

You can get the id via the /marketing/field_definitions endpoint.

Matt
  • 2,329
  • 1
  • 21
  • 23
  • Ahh amazing, can't test it now but throwing that on my Jira board for another day. Thank you! – Nicole Hemenway Mar 03 '21 at 04:10
  • 1
    What an odd API. So I define a custom field by name in the Sendgrid dashboard, en then it doesn't show me an id anywhere and now I have to call an endpoint to find out. I really don't understand why they didn't allow the use of field names and map it to the id internally. Seems like a bad design choice to have to expose the internal details even though there can be only one target when you specify the name. – Thijs Koerselman Oct 28 '22 at 15:38
  • I have a production and a development account. Both will have different ids for the field names depending on in what order the fields were defined. Brilliant :\ – Thijs Koerselman Oct 28 '22 at 16:00
  • 1
    Good grief I am so sick of Sendgrid's API being backwards, confusing, or just plain lying. I found a completely different API page to get custom fields and this was not it. Thank you. – Richard Nov 07 '22 at 23:51
  • 1
    Luckily, you can also look in the browser console when on the Custom Fields page and find the request to https://api.sendgrid.com/v3/marketing/field_definitions and you will see the custom field IDs in the response data. – Spencer Williams Jan 15 '23 at 21:39
1

As said by @Matt, to update a custom field value via SendGrid API, we need to refer to the custom field by its ID, not by the field name.
To get a list with your custom field IDs, do:

from sendgrid import SendGridAPIClient


SENDGRID_API_KEY="print-your-key-here"

sg = SendGridAPIClient(SENDGRID_API_KEY)
response = sg.client.marketing.field_definitions.get()

print(response.body)

Also, take a look at the docs: https://docs.sendgrid.com/api-reference/custom-fields/get-all-field-definitions.

The custom field ID has logic

For custom fields, the unique ID always starts with the suffix e. Followed by an integer number that represents the creation order of the custom fields on the SendGrid platform, p.e. 1, 2, 3. Followed by underscore _. Followed by a letter that represents the custom field type:

  • N - Number
  • T - Text
  • D - Date

Here is an example of a custom field ID list:

{
    "custom_fields":[
        {"id":"e1_N","name":"number_field_test","field_type":"Number"},
        {"id":"e2_T","name":"text_field_test","field_type":"Text"},
        {"id":"e3_D","name":"data_field_test","field_type":"Date"}
    ]
}
claudius
  • 747
  • 1
  • 10
  • 24