Edits history of script submission #11452 for ' Find Contacts (hubspot)'

  • bun
    import { Client } from "@hubspot/api-client@^12.0.1";
    
    /**
     * This script searches for HubSpot Contacts using filters that you specify.
     * See links below for documentation/help:
     *  - https://developers.hubspot.com/docs/guides/api/crm/search#filter-search-results
     *  - https://github.hubspot.com/hubspot-api-nodejs/classes/crm_contacts.SearchApi.html
     */
    
    type Hubspot = {
      token: string;
    };
    
    type Filter = {
      propertyName: string; // https://developers.hubspot.com/docs/guides/api/crm/search#search-default-searchable-properties
      operator: 'LT' | 'LTE' | 'GT' | 'GTE' | 'EQ' | 'NEQ' | 'BETWEEN' | 'IN' | 'NOT_IN' | 'HAS_PROPERTY' | 'NOT_HAS_PROPERTY' | 'CONTAINS_TOKEN' | 'NOT_CONTAINS_TOKEN';
      value: string;
    };
    
    type Sort = {
      propertyName: string;
      direction: 'ASCENDING' | 'DESCENDING';
    };
    
    type SearchOptions = {
      // properties to return outside of default properties
      properties?: string[]
      // you could use just `filters` too but i'm not implementing it
      filterGroups?: {filters: Filter[]}[];
      // only one sort allowed per search, despite it being called `sorts`? hubspot wtf
      sorts?: Sort[];
      // default limit is 10, max. is 200
      limit?: number;
      // for pagination, pass in the `after` property in `paging.next.after` of prev. response
      after?: string;
    }
    
    type QueryResponse = {
      total: number;
      results: any[];
    }
    
    export async function main(auth: Hubspot, searchOptions: SearchOptions): Promise<QueryResponse> {
      const client = new Client({ accessToken: auth.token });
    
      try {
        return await client.crm.contacts.searchApi.doSearch(searchOptions as any);
      } catch (e: any) {
        throw Error(`
          Error Code: ${e?.code || 'UNKNOWN'}
          Message: ${e?.body?.message || e?.body || e}
        `);
      }
    }

    Submitted by pat m.436 491 days ago