I am trying to test this API endpoint to add a contact.
There is no example JSON for this endpoint, and this description is not clear to me:
list_ids - array[string] -The string for the contact list you want to add this contact to. See the POST or GET all lists API - optional
contacts - array[object] - The ID for the contact list where you want to UPSERT the contact. Use GET Contacts method to see Contact List IDs.
This is the JSON I am trying, which comes back as invalid. What is the valid JSON format for this endpoint? Do I need to add the list ID in the contacts array?
{
"list_ids": [
"a-list-id"
],
"contacts": [
{
"email": "test@example.com"
}
]
}
The above always returns:
{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}
I figured it out: Sendgrid's example code is wrong and using the Try it out tab of their API documentation you get this incorrect error message about the JSON being invalid.
Using the curl example here:
curl --request PUT \
--url https://api.sendgrid.com/v3/marketing/contacts \
--header 'authorization: Bearer <<YOUR_API_KEY>>' \
--data '{"list_ids":["string"],"contacts":[{"address_line_1":"string (optional)","address_line_2":"string (optional)","alternate_emails":["string"],"city":"string (optional)","country":"string (optional)","email":"string (required)","first_name":"string (optional)","id":"string (optional)","last_name":"string (optional)","postal_code":"string (optional)","state_province_region":"string (optional)","custom_fields":{}}]}'
The following header needs to be added:
--header 'content-type: application/json' \
So the full request looks like:
curl --request PUT \
--url https://api.sendgrid.com/v3/marketing/contacts \
--header 'authorization: Bearer <<YOUR_API_KEY>>' \
--header 'content-type: application/json' \
--data '{"list_ids":["string"],"contacts":[{"address_line_1":"string (optional)","address_line_2":"string (optional)","alternate_emails":["string"],"city":"string (optional)","country":"string (optional)","email":"string (required)","first_name":"string (optional)","id":"string (optional)","last_name":"string (optional)","postal_code":"string (optional)","state_province_region":"string (optional)","custom_fields":{}}]}'
The same thing applies to the PHP curl example:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"$thing\"],\"contacts\":[{\"email\":\"testphp2@example.com\",\"first_name\":\"string (optional)\",\"last_name\":\"string (optional)\"}]}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer <<API_KEY>>",
"content-type: application/json"
),
));