0
Create a portfolio
One script reply has been approved by the moderators Verified

Creates a new portfolio in the given workspace with the supplied name.

Note that portfolios created in the Asana UI may have some state (like the “Priority” custom field) which is automatically added to the portfolio when it is created. Portfolios created via our API will not be created with the same initial state to allow integrations to create their own starting state on a portfolio.

Created by hugo697 576 days ago Viewed 22478 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 576 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Create a portfolio
6
 * Creates a new portfolio in the given workspace with the supplied name.
7

8
Note that portfolios created in the Asana UI may have some state
9
(like the “Priority” custom field) which is automatically added
10
to the portfolio when it is created. Portfolios created via our
11
API will *not* be created with the same initial state to allow
12
integrations to create their own starting state on a portfolio.
13
 */
14
export async function main(
15
  auth: Asana,
16
  opt_pretty: string | undefined,
17
  opt_fields: string | undefined,
18
  body: {
19
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
20
      name?: string;
21
      [k: string]: unknown;
22
    }) & {
23
      color?:
24
        | "dark-pink"
25
        | "dark-green"
26
        | "dark-blue"
27
        | "dark-red"
28
        | "dark-teal"
29
        | "dark-brown"
30
        | "dark-orange"
31
        | "dark-purple"
32
        | "dark-warm-gray"
33
        | "light-pink"
34
        | "light-green"
35
        | "light-blue"
36
        | "light-red"
37
        | "light-teal"
38
        | "light-brown"
39
        | "light-orange"
40
        | "light-purple"
41
        | "light-warm-gray";
42
      [k: string]: unknown;
43
    }) & {
44
      members?: string[];
45
      public?: boolean;
46
      workspace?: string;
47
      [k: string]: unknown;
48
    };
49
    [k: string]: unknown;
50
  }
51
) {
52
  const url = new URL(`https://app.asana.com/api/1.0/portfolios`);
53
  for (const [k, v] of [
54
    ["opt_pretty", opt_pretty],
55
    ["opt_fields", opt_fields],
56
  ]) {
57
    if (v !== undefined && v !== "") {
58
      url.searchParams.append(k, v);
59
    }
60
  }
61
  const response = await fetch(url, {
62
    method: "POST",
63
    headers: {
64
      "Content-Type": "application/json",
65
      Authorization: "Bearer " + auth.token,
66
    },
67
    body: JSON.stringify(body),
68
  });
69
  if (!response.ok) {
70
    const text = await response.text();
71
    throw new Error(`${response.status} ${text}`);
72
  }
73
  return await response.json();
74
}
75