0

Create a project template from a project

by
Published Oct 31, 2023

Creates and returns a job that will asynchronously handle the project template creation. Note that while the resulting project template can be accessed with the API, it won't be visible in the Asana UI until Project Templates 2.0 is launched in the app. See more in [this forum post](https://forum.asana.com/t/a-new-api-for-project-templates/156432).

Script asana Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Create a project template from a project
6
 * Creates and returns a job that will asynchronously handle the project template creation. Note that
7
while the resulting project template can be accessed with the API, it won't be visible in the Asana
8
UI until Project Templates 2.0 is launched in the app. See more in [this forum post](https://forum.asana.com/t/a-new-api-for-project-templates/156432).
9
 */
10
export async function main(
11
  auth: Asana,
12
  project_gid: string,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined,
15
  body: {
16
    data?: {
17
      name: string;
18
      public: boolean;
19
      team?: string;
20
      workspace?: string;
21
      [k: string]: unknown;
22
    };
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://app.asana.com/api/1.0/projects/${project_gid}/saveAsTemplate`
28
  );
29
  for (const [k, v] of [
30
    ["opt_pretty", opt_pretty],
31
    ["opt_fields", opt_fields],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51