简体   繁体   中英

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.

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 .

I recently went through the same thing so want to add what I did in the hopes that this helps anyone with the same issue.

This is quick and dirty code that I used to test out how to grab an entire contact list without actually needing to open the file outside the script.

request = { 
    "list_ids": [list_id]
}
response = sg.client.marketing.contacts.exports.post(
    request_body=request
)

export_json = response.body.decode('utf8').replace("'", '"')
export_data = json.loads(export_json)

export_id = export_data['id']

urls = []
while True:
    response = sg.client.marketing.contacts.exports._(export_id).get()
    export_status_json = response.body.decode('utf8').replace("'", '"')
    export_status_data = json.loads(export_status_json)
    urls = export_status_data['urls']
    
    if len(urls) == 0:
        time.sleep(10)
    else:
        break

for url in urls:
    response = requests.get(url)
    out_file = gzip.decompress(response.content).decode('utf8'))
    pd = pandas.read_csv(io.StringIO(out_file), dtype=str)

If you want json, add that to the request body and read it as

for url in urls:
    response = requests.get(url)
    out_file = gzip.decompress(response.content).decode('utf8').replace("'", '"')


    print(out_file)

For me, our company is using legacy marketing so i have to use another endpoint POST v3/contactdb/recipient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM