0

Get Portfolio

by
Published Oct 31, 2023

Get a portfolio by its gid. Portfolios are available on Asana's paid business plans.

Script asana Verified

The script

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

3
/**
4
 * Get Portfolio
5
 * Get a portfolio by its gid. Portfolios are available on Asana's paid business plans.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  portfolio_gid: string,
10
  opt_fields: string | undefined
11
) {
12
  const url = new URL(
13
    `https://app.asana.com/api/1.0/portfolios/${portfolio_gid}`
14
  )
15
  const params = { opt_fields }
16
  for (const [key, value] of Object.entries(params)) {
17
    if (value != null && value !== "") {
18
      url.searchParams.append(key, String(value))
19
    }
20
  }
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 hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get a portfolio
    6
     * Returns the complete portfolio record for a single portfolio.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      portfolio_gid: string,
    11
      opt_pretty: string | undefined,
    12
      opt_fields: string | undefined
    13
    ) {
    14
      const url = new URL(
    15
        `https://app.asana.com/api/1.0/portfolios/${portfolio_gid}`
    16
      );
    17
      for (const [k, v] of [
    18
        ["opt_pretty", opt_pretty],
    19
        ["opt_fields", opt_fields],
    20
      ]) {
    21
        if (v !== undefined && v !== "") {
    22
          url.searchParams.append(k, v);
    23
        }
    24
      }
    25
      const response = await fetch(url, {
    26
        method: "GET",
    27
        headers: {
    28
          Authorization: "Bearer " + auth.token,
    29
        },
    30
        body: undefined,
    31
      });
    32
      if (!response.ok) {
    33
        const text = await response.text();
    34
        throw new Error(`${response.status} ${text}`);
    35
      }
    36
      return await response.json();
    37
    }
    38