0
Search Contacts
One script reply has been approved by the moderators Verified
Created by aurelievets2t 681 days ago Viewed 4109 times
0
Submitted by adam186 Deno
Verified 503 days ago
1
import sendgrid from "npm:@sendgrid/client@^7.7.0";
2

3
/**
4
 * @param query The search query in SGQL format. An example query to get
5
 * all contacts with a Gmail address would look like the following:
6
 * `email LIKE '%gmail.com'`
7
 *
8
 * You can read more about SGQL at
9
 * https://docs.sendgrid.com/for-developers/sending-email/segmentation-query-language.
10
 *
11
 * You can read more about the endpoint at
12
 * https://docs.sendgrid.com/api-reference/contacts/search-contacts.
13
 */
14
type Sendgrid = {
15
  token: string;
16
};
17
export async function main(api_token: Sendgrid, query: string) {
18
  sendgrid.setApiKey(api_token.token);
19

20
  const request = {
21
    url: `/v3/marketing/contacts/search`,
22
    method: "POST",
23
    body: { query },
24
  };
25

26
  try {
27
    const [_, body] = await sendgrid.request(request);
28
    return body;
29
  } catch (error) {
30
    throw Error("\n" + JSON.stringify(error?.response?.body || error));
31
  }
32
}
33