0

Get Project

by
Published Oct 31, 2023

Get the full record of a project by its gid. Use `opt_fields` (comma-separated) to choose the returned fields.

Script asana Verified

The script

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

3
/**
4
 * Get Project
5
 * Get the full record of a project by its gid. Use `opt_fields` (comma-separated) to choose the returned fields.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  project_gid: string,
10
  opt_fields: string | undefined
11
) {
12
  const url = new URL(`https://app.asana.com/api/1.0/projects/${project_gid}`)
13
  const params = { opt_fields }
14
  for (const [key, value] of Object.entries(params)) {
15
    if (value != null && value !== "") {
16
      url.searchParams.append(key, String(value))
17
    }
18
  }
19

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

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

31
  return await response.json()
32
}
33

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get a project
    6
     * Returns the complete project record for a single project.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      project_gid: string,
    11
      opt_pretty: string | undefined,
    12
      opt_fields: string | undefined
    13
    ) {
    14
      const url = new URL(`https://app.asana.com/api/1.0/projects/${project_gid}`);
    15
      for (const [k, v] of [
    16
        ["opt_pretty", opt_pretty],
    17
        ["opt_fields", opt_fields],
    18
      ]) {
    19
        if (v !== undefined && v !== "") {
    20
          url.searchParams.append(k, v);
    21
        }
    22
      }
    23
      const response = await fetch(url, {
    24
        method: "GET",
    25
        headers: {
    26
          Authorization: "Bearer " + auth.token,
    27
        },
    28
        body: undefined,
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36