Edits history of script submission #23455 for ' List Form Submissions (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_form_id = string
    
    // Dropdown of the account's forms.
    export async function form_id(auth: RT.Hubspot) {
      const response = await fetch(
        "https://api.hubapi.com/marketing/v3/forms?limit=100",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { results } = (await response.json()) as {
        results: { id: string; name: string }[]
      }
      return results.map((f) => ({ value: f.id, label: f.name }))
    }
    
    /**
     * List Form Submissions
     * List the submissions of a form, most recent first (max 50 per page). Requires the forms scope.
     */
    export async function main(
      auth: RT.Hubspot,
      form_id: DynSelect_form_id,
      limit: number | undefined,
      after: string | undefined
    ) {
      const url = new URL(
        `https://api.hubapi.com/form-integrations/v1/submissions/forms/${form_id}`
      )
      if (limit != null) url.searchParams.append("limit", String(limit))
      if (after != null && after !== "") url.searchParams.append("after", after)
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago