0

Get Contact

by
Published Jun 6, 2022

Retrieve a contact by record ID, with the properties you list.

Script hubspot Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 20 days ago
1
//native
2

3
/**
4
 * Get Contact
5
 * Retrieve a contact by record ID, with the properties you list.
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  contact_id: string,
10
  properties: string[] | undefined,
11
  properties_with_history: string[] | undefined
12
) {
13
  const url = new URL(
14
    `https://api.hubapi.com/crm/v3/objects/contacts/${contact_id}`
15
  )
16
  if (properties?.length)
17
    url.searchParams.append("properties", properties.join(","))
18
  if (properties_with_history?.length)
19
    url.searchParams.append(
20
      "propertiesWithHistory",
21
      properties_with_history.join(",")
22
    )
23

24
  const response = await fetch(url, {
25
    headers: {
26
      Authorization: `Bearer ${auth.token}`,
27
      Accept: "application/json",
28
    },
29
  })
30

31
  if (!response.ok) {
32
    throw new Error(`${response.status} ${await response.text()}`)
33
  }
34

35
  return await response.json()
36
}
37

Other submissions
  • Submitted by adam186 Deno
    Created 544 days ago
    1
    import { Client } from "npm:@hubspot/api-client@^8.1.0";
    2
    
    
    3
    /**
    4
     * @param properties List of the properties to be returned in the response.
    5
     *
    6
     * @param properties_with_history List of the properties to be returned
    7
     * along with their history of previous values.
    8
     */
    9
    type Hubspot = {
    10
      token: string;
    11
    };
    12
    export async function main(
    13
      auth: Hubspot,
    14
      contact_id: string,
    15
      properties?: string[],
    16
      properties_with_history?: string[],
    17
    ) {
    18
      const client = new Client({
    19
        accessToken: auth.token,
    20
      });
    21
    
    
    22
      try {
    23
        return await client.crm.contacts.basicApi.getById(
    24
          contact_id,
    25
          getProp(properties),
    26
          getProp(properties_with_history),
    27
        );
    28
      } catch (e) {
    29
        throw Error(`
    30
          ${e.code}\n
    31
          Message: ${e.body.message || e.body}\n
    32
        `);
    33
      }
    34
    }
    35
    
    
    36
    function getProp(prop?: string[]) {
    37
      return Array.isArray(prop) && prop.length ? prop : undefined;
    38
    }
    39
    
    
  • Submitted by hugo989 Bun
    Created 152 days ago
    1
    import { Client } from "@hubspot/api-client@^8.1.0";
    2
    
    
    3
    /**
    4
     * @param properties List of the properties to be returned in the response.
    5
     *
    6
     * @param properties_with_history List of the properties to be returned
    7
     * along with their history of previous values.
    8
     */
    9
    type Hubspot = {
    10
      token: string;
    11
    };
    12
    export async function main(
    13
      auth: Hubspot,
    14
      contact_id: string,
    15
      properties?: string[],
    16
      properties_with_history?: string[],
    17
    ) {
    18
      const client = new Client({
    19
        accessToken: auth.token,
    20
      });
    21
    
    
    22
      try {
    23
        return await client.crm.contacts.basicApi.getById(
    24
          contact_id,
    25
          getProp(properties),
    26
          getProp(properties_with_history),
    27
        );
    28
      } catch (e) {
    29
        throw Error(`
    30
          ${e.code}\n
    31
          Message: ${e.body.message || e.body}\n
    32
        `);
    33
      }
    34
    }
    35
    
    
    36
    function getProp(prop?: string[]) {
    37
      return Array.isArray(prop) && prop.length ? prop : undefined;
    38
    }
    39