Sendgrid-php: Methods for work with marketing

Created on 4 Dec 2019  路  5Comments  路  Source: sendgrid/sendgrid-php

API has method https://api.sendgrid.com/v3/contactdb/lists
And this method always return in Postman (i used API key)
{ "errors": [ { "field": null, "message": "access forbidden" } ] }

And the bundle does the same. May be it is because it's legacy endpoint.

Also API has method https://api.sendgrid.com/v3/marketing/lists
In Postman it works well.
But bundle don't have a method for work with it.
How I can manage marketings with this bundle?

Technical details:

  • sendgrid-php Version: "sendgrid/sendgrid": "^7.3",
  • PHP Version: 7.3

Most helpful comment

Thank you. I did not guess to build requests on the fly, even if they are not in the class Client.
$sg->client->marketing()->lists()->get() work well.

All 5 comments

This can be an issue with API key permissions.
Maybe it has selected option 'Restricted access' and 'Marketing Campaigns' isn't enabled.

Tried myself. After creating a new API key without the permission enabled, I got the same error. After enabling I got {"lists":[]} with HTTP status 200.
The requests for /marketing/lists don't work for me. Also when having 'Full access'.

Its definitely because of legacy. I checked and I have no access to legacy endpoints that haven't been carried forward. What I don't understand is why SendGrid is still on v3/ even though v3 is technically a completely different API. There are no GitHub docs for the current API. You have to look it up on their reference page and use the fluent accessors to make a call like

$sg->client->marketing()->contacts()->get()

Nice that it works, but stupid that I spent so long trying to figure out nothing worked but just to find out the API docs don't represent the API at all.

Worse still. there appears to be a tonne of stuff missing from the new marketing endpoints. I have no way of accessing all contacts by a given list. I want to retrieve all contacts within list A, but I cannot do that, I have to retrieve ALL contacts, then just filter through them manually.

And ideas why this is such a mess?

omg I found it:

/marketing/contacts/search

They want you to run a query yourself:

{
  "query": "email LIKE 'ENTER_COMPLETE_OR_PARTIAL_EMAIL_ADDRESS_HERE%' AND CONTAINS(list_ids, 'YOUR_LIST_IDs')"
}

better still that's list ID not list name. So you have to pull the list of lists, loop it to get the item containing the list you want, grab it's ID then make a request to marketing/contacts/search and use the ID in the query to get your list.

Thank you. I did not guess to build requests on the fly, even if they are not in the class Client.
$sg->client->marketing()->lists()->get() work well.

If its any help. Here is what I did to get a list by it's name, followed by all contacts in that list:

$name = 'App Users';

// get lists, search by name, and use the index to fetch the list item.
$lists = $sg->marketing()->lists()->get();
$index = array_search($name, array_column($lists, 'name'));
$list = $lists[$index];

// get the list item ID and use it to generate a query for all contacts within that list.
$query = "CONTAINS(list_ids, '" . $list['id'] . "')";
$contacts = $sg->marketing()->contacts()->search()->post(compact('query'));

Note, this does not include formatting the response.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

peluprvi picture peluprvi  路  5Comments

bjornmann picture bjornmann  路  3Comments

moontrv picture moontrv  路  3Comments

FilipLukac picture FilipLukac  路  4Comments

izhukovich picture izhukovich  路  4Comments