0
Create a project in a workspace
One script reply has been approved by the moderators Verified

Returns the compact project records for all projects in the workspace.

If the workspace for your project is an organization, you must also supply a team to share the project with.

Returns the full record of the newly created project.

Created by hugo697 388 days ago Viewed 8992 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 388 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Create a project in a workspace
6
 * Returns the compact project records for all projects in the workspace.
7

8
If the workspace for your project is an organization, you must also
9
supply a team to share the project with.
10

11
Returns the full record of the newly created project.
12
 */
13
export async function main(
14
  auth: Asana,
15
  workspace_gid: string,
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
      archived?: boolean;
24
      color?:
25
        | "dark-pink"
26
        | "dark-green"
27
        | "dark-blue"
28
        | "dark-red"
29
        | "dark-teal"
30
        | "dark-brown"
31
        | "dark-orange"
32
        | "dark-purple"
33
        | "dark-warm-gray"
34
        | "light-pink"
35
        | "light-green"
36
        | "light-blue"
37
        | "light-red"
38
        | "light-teal"
39
        | "light-brown"
40
        | "light-orange"
41
        | "light-purple"
42
        | "light-warm-gray";
43
      created_at?: string;
44
      current_status?: (({
45
        gid?: string;
46
        resource_type?: string;
47
        [k: string]: unknown;
48
      } & { title?: string; [k: string]: unknown }) & {
49
        color: "green" | "yellow" | "red" | "blue";
50
        html_text?: string;
51
        text: string;
52
        [k: string]: unknown;
53
      }) & {
54
        author?: {
55
          gid?: string;
56
          resource_type?: string;
57
          [k: string]: unknown;
58
        } & { name?: string; [k: string]: unknown };
59
        created_at?: string;
60
        created_by?: {
61
          gid?: string;
62
          resource_type?: string;
63
          [k: string]: unknown;
64
        } & { name?: string; [k: string]: unknown };
65
        modified_at?: string;
66
        [k: string]: unknown;
67
      };
68
      current_status_update?: {
69
        gid?: string;
70
        resource_type?: string;
71
        [k: string]: unknown;
72
      } & {
73
        resource_subtype?:
74
          | "project_status_update"
75
          | "portfolio_status_update"
76
          | "goal_status_update";
77
        title?: string;
78
        [k: string]: unknown;
79
      };
80
      custom_field_settings?: (({
81
        gid?: string;
82
        resource_type?: string;
83
        [k: string]: unknown;
84
      } & { [k: string]: unknown }) & {
85
        custom_field?: ((({
86
          gid?: string;
87
          resource_type?: string;
88
          [k: string]: unknown;
89
        } & {
90
          date_value?: {
91
            date?: string;
92
            date_time?: string;
93
            [k: string]: unknown;
94
          };
95
          display_value?: string;
96
          enabled?: boolean;
97
          enum_options?: ({
98
            gid?: string;
99
            resource_type?: string;
100
            [k: string]: unknown;
101
          } & {
102
            color?: string;
103
            enabled?: boolean;
104
            name?: string;
105
            [k: string]: unknown;
106
          })[];
107
          enum_value?: ({
108
            gid?: string;
109
            resource_type?: string;
110
            [k: string]: unknown;
111
          } & {
112
            color?: string;
113
            enabled?: boolean;
114
            name?: string;
115
            [k: string]: unknown;
116
          }) & { [k: string]: unknown };
117
          multi_enum_values?: ({
118
            gid?: string;
119
            resource_type?: string;
120
            [k: string]: unknown;
121
          } & {
122
            color?: string;
123
            enabled?: boolean;
124
            name?: string;
125
            [k: string]: unknown;
126
          })[];
127
          name?: string;
128
          number_value?: number;
129
          resource_subtype?:
130
            | "text"
131
            | "enum"
132
            | "multi_enum"
133
            | "number"
134
            | "date"
135
            | "people";
136
          text_value?: string;
137
          type?: "text" | "enum" | "multi_enum" | "number";
138
          [k: string]: unknown;
139
        }) & {
140
          asana_created_field?:
141
            | "a_v_requirements"
142
            | "account_name"
143
            | "actionable"
144
            | "align_shipping_link"
145
            | "align_status"
146
            | "allotted_time"
147
            | "appointment"
148
            | "approval_stage"
149
            | "approved"
150
            | "article_series"
151
            | "board_committee"
152
            | "browser"
153
            | "campaign_audience"
154
            | "campaign_project_status"
155
            | "campaign_regions"
156
            | "channel_primary"
157
            | "client_topic_type"
158
            | "complete_by"
159
            | "contact"
160
            | "contact_email_address"
161
            | "content_channels"
162
            | "content_channels_needed"
163
            | "content_stage"
164
            | "content_type"
165
            | "contract"
166
            | "contract_status"
167
            | "cost"
168
            | "creation_stage"
169
            | "creative_channel"
170
            | "creative_needed"
171
            | "creative_needs"
172
            | "data_sensitivity"
173
            | "deal_size"
174
            | "delivery_appt"
175
            | "delivery_appt_date"
176
            | "department"
177
            | "department_responsible"
178
            | "design_request_needed"
179
            | "design_request_type"
180
            | "discussion_category"
181
            | "do_this_task"
182
            | "editorial_content_status"
183
            | "editorial_content_tag"
184
            | "editorial_content_type"
185
            | "effort"
186
            | "effort_level"
187
            | "est_completion_date"
188
            | "estimated_time"
189
            | "estimated_value"
190
            | "expected_cost"
191
            | "external_steps_needed"
192
            | "favorite_idea"
193
            | "feedback_type"
194
            | "financial"
195
            | "funding_amount"
196
            | "grant_application_process"
197
            | "hiring_candidate_status"
198
            | "idea_status"
199
            | "ids_link"
200
            | "ids_patient_link"
201
            | "implementation_stage"
202
            | "insurance"
203
            | "interview_area"
204
            | "interview_question_score"
205
            | "itero_scan_link"
206
            | "job_s_applied_to"
207
            | "lab"
208
            | "launch_status"
209
            | "lead_status"
210
            | "localization_language"
211
            | "localization_market_team"
212
            | "localization_status"
213
            | "meeting_minutes"
214
            | "meeting_needed"
215
            | "minutes"
216
            | "mrr"
217
            | "must_localize"
218
            | "name_of_foundation"
219
            | "need_to_follow_up"
220
            | "next_appointment"
221
            | "next_steps_sales"
222
            | "num_people"
223
            | "number_of_user_reports"
224
            | "office_location"
225
            | "onboarding_activity"
226
            | "owner"
227
            | "participants_needed"
228
            | "patient_date_of_birth"
229
            | "patient_email"
230
            | "patient_phone"
231
            | "patient_status"
232
            | "phone_number"
233
            | "planning_category"
234
            | "point_of_contact"
235
            | "position"
236
            | "post_format"
237
            | "prescription"
238
            | "priority"
239
            | "priority_level"
240
            | "product"
241
            | "product_stage"
242
            | "progress"
243
            | "project_size"
244
            | "project_status"
245
            | "proposed_budget"
246
            | "publish_status"
247
            | "reason_for_scan"
248
            | "referral"
249
            | "request_type"
250
            | "research_status"
251
            | "responsible_department"
252
            | "responsible_team"
253
            | "risk_assessment_status"
254
            | "room_name"
255
            | "sales_counterpart"
256
            | "sentiment"
257
            | "shipping_link"
258
            | "social_channels"
259
            | "stage"
260
            | "status"
261
            | "status_design"
262
            | "status_of_initiative"
263
            | "system_setup"
264
            | "task_progress"
265
            | "team"
266
            | "team_marketing"
267
            | "team_responsible"
268
            | "time_it_takes_to_complete_tasks"
269
            | "timeframe"
270
            | "treatment_type"
271
            | "type_work_requests_it"
272
            | "use_agency"
273
            | "user_name"
274
            | "vendor_category"
275
            | "vendor_type"
276
            | "word_count";
277
          currency_code?: string;
278
          custom_label?: string;
279
          custom_label_position?: "prefix" | "suffix";
280
          description?: string;
281
          enum_options?: ({
282
            gid?: string;
283
            resource_type?: string;
284
            [k: string]: unknown;
285
          } & {
286
            color?: string;
287
            enabled?: boolean;
288
            name?: string;
289
            [k: string]: unknown;
290
          })[];
291
          format?: "currency" | "identifier" | "percentage" | "custom" | "none";
292
          has_notifications_enabled?: boolean;
293
          is_global_to_workspace?: boolean;
294
          precision?: number;
295
          [k: string]: unknown;
296
        }) & {
297
          created_by?: {
298
            gid?: string;
299
            resource_type?: string;
300
            [k: string]: unknown;
301
          } & { name?: string; [k: string]: unknown };
302
          people_value?: ({
303
            gid?: string;
304
            resource_type?: string;
305
            [k: string]: unknown;
306
          } & { name?: string; [k: string]: unknown })[];
307
          [k: string]: unknown;
308
        }) & { [k: string]: unknown };
309
        is_important?: boolean;
310
        parent?: ({
311
          gid?: string;
312
          resource_type?: string;
313
          [k: string]: unknown;
314
        } & { name?: string; [k: string]: unknown }) & { [k: string]: unknown };
315
        project?: ({
316
          gid?: string;
317
          resource_type?: string;
318
          [k: string]: unknown;
319
        } & { name?: string; [k: string]: unknown }) & { [k: string]: unknown };
320
        [k: string]: unknown;
321
      })[];
322
      default_view?: "list" | "board" | "calendar" | "timeline";
323
      due_date?: string;
324
      due_on?: string;
325
      html_notes?: string;
326
      is_template?: boolean;
327
      members?: ({
328
        gid?: string;
329
        resource_type?: string;
330
        [k: string]: unknown;
331
      } & { name?: string; [k: string]: unknown })[];
332
      modified_at?: string;
333
      notes?: string;
334
      public?: boolean;
335
      start_on?: string;
336
      workspace?: ({
337
        gid?: string;
338
        resource_type?: string;
339
        [k: string]: unknown;
340
      } & { name?: string; [k: string]: unknown }) & { [k: string]: unknown };
341
      [k: string]: unknown;
342
    }) & {
343
      custom_fields?: { [k: string]: string };
344
      followers?: string;
345
      owner?: string;
346
      team?: string;
347
      [k: string]: unknown;
348
    };
349
    [k: string]: unknown;
350
  }
351
) {
352
  const url = new URL(
353
    `https://app.asana.com/api/1.0/workspaces/${workspace_gid}/projects`
354
  );
355
  for (const [k, v] of [
356
    ["opt_pretty", opt_pretty],
357
    ["opt_fields", opt_fields],
358
  ]) {
359
    if (v !== undefined && v !== "") {
360
      url.searchParams.append(k, v);
361
    }
362
  }
363
  const response = await fetch(url, {
364
    method: "POST",
365
    headers: {
366
      "Content-Type": "application/json",
367
      Authorization: "Bearer " + auth.token,
368
    },
369
    body: JSON.stringify(body),
370
  });
371
  if (!response.ok) {
372
    const text = await response.text();
373
    throw new Error(`${response.status} ${text}`);
374
  }
375
  return await response.json();
376
}
377