Edits history of script submission #23423 for ' Create or Update Contact (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create or Update Contact
     * Create a contact, or update it when a contact with this email already exists. `properties` maps internal property names to values (firstname, lastname, phone, ...).
     */
    export async function main(
      auth: RT.Hubspot,
      email: string,
      properties: { [key: string]: any } | undefined
    ) {
      const response = await fetch(
        "https://api.hubapi.com/crm/v3/objects/contacts/batch/upsert",
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            inputs: [
              { id: email, idProperty: "email", properties: properties ?? {} },
            ],
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { results } = (await response.json()) as { results: any[] }
      return results[0]
    }
    

    Submitted by hugo989 5 hours ago