Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating SendGrid contact custom fields via SendGrid API. Why isn't this working?

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")```
like image 539
Nicole Hemenway Avatar asked Jan 24 '26 13:01

Nicole Hemenway


1 Answers

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.

like image 119
Matt Avatar answered Jan 26 '26 20:01

Matt