0

Get File Public URL

by
Published Jun 6, 2022

Get a temporary signed URL to download a file from the HubSpot file manager.

Script hubspot Verified

The script

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

3
/**
4
 * Get File Public URL
5
 * Get a temporary signed URL to download a file from the HubSpot file manager. Requires the files scope (or files.ui_hidden.read).
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  file_id: string,
10
  size: "" | "thumb" | "icon" | "medium" | "preview" | undefined,
11
  expiration_seconds: number | undefined,
12
  upscale: boolean | undefined
13
) {
14
  const url = new URL(
15
    `https://api.hubapi.com/files/v3/files/${file_id}/signed-url`
16
  )
17
  if (size != null && size !== "") url.searchParams.append("size", size)
18
  if (expiration_seconds != null)
19
    url.searchParams.append("expirationSeconds", String(expiration_seconds))
20
  if (upscale) url.searchParams.append("upscale", String(upscale))
21

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

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

33
  return await response.json()
34
}
35

Other submissions
  • Submitted by adam186 Deno
    Created 526 days ago
    1
    import { Client } from "npm:@hubspot/api-client@^8.1.0";
    2
    
    
    3
    type Hubspot = {
    4
      token: string;
    5
    };
    6
    export async function main(
    7
      auth: Hubspot,
    8
      file_id: string,
    9
      size?: "" | "thumb" | "icon" | "medium" | "preview",
    10
      expiration_seconds?: number,
    11
      upscale?: boolean,
    12
    ) {
    13
      const client = new Client({
    14
        accessToken: auth.token,
    15
      });
    16
    
    
    17
      try {
    18
        return await client.files.filesApi.getSignedUrl(
    19
          file_id,
    20
          size || undefined,
    21
          expiration_seconds || undefined,
    22
          upscale || undefined,
    23
        );
    24
      } catch (e) {
    25
        throw Error(`
    26
          ${e.code}\n
    27
          Message: ${e.body.message || e.body}\n
    28
        `);
    29
      }
    30
    }
    31
    
    
  • Submitted by hugo989 Bun
    Created 134 days ago
    1
    import { Client } from "@hubspot/api-client@^8.1.0";
    2
    
    
    3
    type Hubspot = {
    4
      token: string;
    5
    };
    6
    export async function main(
    7
      auth: Hubspot,
    8
      file_id: string,
    9
      size?: "" | "thumb" | "icon" | "medium" | "preview",
    10
      expiration_seconds?: number,
    11
      upscale?: boolean,
    12
    ) {
    13
      const client = new Client({
    14
        accessToken: auth.token,
    15
      });
    16
    
    
    17
      try {
    18
        return await client.files.filesApi.getSignedUrl(
    19
          file_id,
    20
          size || undefined,
    21
          expiration_seconds || undefined,
    22
          upscale || undefined,
    23
        );
    24
      } catch (e) {
    25
        throw Error(`
    26
          ${e.code}\n
    27
          Message: ${e.body.message || e.body}\n
    28
        `);
    29
      }
    30
    }
    31