Edits history of script submission #23399 for ' Create Contact (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Contact
     * Create a contact from the common fields. Use Create CRM Object for any other property, or Create or Update Contact to upsert by email.
     */
    export async function main(
      auth: RT.Hubspot,
      company: string | undefined,
      email: string | undefined,
      firstname: string | undefined,
      lastname: string | undefined,
      phone: string | undefined,
      website: string | undefined
    ) {
      const properties: { [key: string]: string } = {}
      const fields = { company, email, firstname, lastname, phone, website }
      for (const [key, value] of Object.entries(fields)) {
        if (value != null && value !== "") properties[key] = value
      }
    
      const response = await fetch(
        "https://api.hubapi.com/crm/v3/objects/contacts",
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ properties }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 17 days ago