Replies: 4 comments 4 replies
-
To answer my own question, SendGrid support explained to me that this is not possible. The legacy API gives you the ability to fetch all contacts from a list. For new customers who don't have access to the legacy API, they have to export the contacts into CSV. |
Beta Was this translation helpful? Give feedback.
-
You're right to be warry of using search for a large number of contacts. SendGrid says the request will timeout if longer than 20 seconds. I queried a list of 1K contacts and it took ~4 seconds. I think I found the solution by using the following query:
The above will return all contacts with email starting with "A". The idea is paginate the query by looping through the alphabet. I've confirmed this query works with Postman, but can't express it using StrongGrid. How can I use the "CONTAINS" function to query list_ids? Or is there a way to pass a string query to the Contacts.SearchAsync() function? (The "IN" or "=" operators will not work with the list_ids field) |
Beta Was this translation helpful? Give feedback.
-
I have reviewed the 'Export Contacts' documentation and it seems that SendGrid may have implemented some changes since the last time I looked at it. Seems like the "we'll send you an email with a link that you must click on to download the CSV file(s)" step is now optional. It was a required step back in 2018. This means we can automate the export process from start to finish. Here's what I have in mind: var exportJobId = await client.Contacts.ExportAsync(FileType.Csv, null, null, default, cancellationToken).ConfigureAwait(false);
var elapsed = Stopwatch.StartNew();
var jobCompleted = false;
while (true)
{
var job = await client.Contacts.GetExportJobAsync(exportJobId, cancellationToken).ConfigureAwait(false);
if (job.Status == JobStatus.Ready)
{
jobCompleted = true;
break;
}
else
{
await Task.Delay(500, cancellationToken).ConfigureAwait(false);
}
// Make sure we don't loop indefinetly.
// I arbitrarilly decided to wait no more than 5 seconds, because it seems like a reasonable amount of time.
// In your case, due to the large number of contacts, I imagine it will take much longer than 5 seconds.
if (elapsed.Elapsed >= TimeSpan.FromSeconds(5))
{
elapsed.Stop();
break;
}
}
if (jobCompleted)
{
var tempPath = System.IO.Path.GetTempPath();
await client.Contacts.DownloadExportFilesAsync(exportJobId, tempPath, cancellationToken).ConfigureAwait(false);
} Note 1: SendGrid can decide the break up an export job into several CSV files to keep each one to a manageable size |
Beta Was this translation helpful? Give feedback.
-
FYI: new version (0.90.0) has been released with support for the CONTAINS search criteria and also the ability to automatically decompress GZIP'ed files when exporting contacts. Let me know if this helps. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to get all the contacts for a specific list, without using the legacy API or fetching all the contacts? I plan to have over 100K contacts and want to use the API to synchronize list memberships with my CRM.
Beta Was this translation helpful? Give feedback.
All reactions