Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all contacts from sendgrid via api

I'm working on a app where I add the user to sendgrid during sign up using the api:

PUT /marketing/contacts

I also add the user to specific list based on the status(DB data) of the user during signup.

Say I've 3 lists in sendgrid List A, List B, List C

Now I run a cron on every hour and check the status of the user and based on the condition I want to move the user from say List A to List B

As I checked I can add the user to the new list using the same api:

PUT /marketing/contacts

And I can remove the user from the previous list I can use the api:

DELETE /marketing/lists/{id}/contacts

But to in which list the user was previously added and to get the contact id from sendgrid I need to get the contacts from sendgrid, I'm using the api:

GET /marketing/contacts

But this api is only returning last 50 data and pagination option is also not there.

I also tried the api:

GET https://api.sendgrid.com/v3/contactdb/recipients?page_size=100&page=1

But this api is also returning me error

error:
{
  "errors": [
     {
       "field": null,
       "message": "access forbidden"
     }
  ]
}

But the api key is fine because the marketing apis works with the same api key and the api key is generated with full access.

https://sendgrid.com/docs/API_Reference/api_v3.html https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html

Can someone please help me to get all contacts from sendgrid via any other api or if there is any params I'm missing in the above mentioned apis.

like image 460
Indranil Mondal Avatar asked Sep 19 '25 02:09

Indranil Mondal


1 Answers

After contacting support, this is what I got:

In order to fetch all the existing contacts you can use this endpoint: https://api.sendgrid.com/v3/marketing/contacts/exports
POST /marketing/contacts/exports

This will return id, which you use to GET /v3/marketing/contacts/exports/<id>, receiving a file. So download it, open and read csv. Not exactly what I wanted, but it might do the job for others.

Docs about this endoint here.

like image 161
nenadp Avatar answered Sep 23 '25 09:09

nenadp