0

Create Ticket

by
Published Nov 7, 2023

Create a ticket. The comment becomes the ticket's first (public) message. A requester given by email is created as an end user if they don't exist yet; without one the ticket is requested by the authenticated agent. Set any other ticket field through `fields`, e.g. `{ "collaborator_ids": [123], "due_at": "2026-12-01T00:00:00Z", "brand_id": 456 }`.

Script zendesk Verified

The script

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

3
export type DynSelect_group_id = string
4

5
// Resolver: the account's groups (first 100).
6
export async function group_id(auth: RT.Zendesk) {
7
  const response = await fetch(
8
    `https://${auth.subdomain}.zendesk.com/api/v2/groups?page[size]=100`,
9
    {
10
      headers: {
11
        Authorization: auth.token
12
          ? `Bearer ${auth.token}`
13
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
14
        Accept: "application/json",
15
      },
16
    }
17
  )
18
  if (!response.ok) {
19
    throw new Error(`${response.status} ${await response.text()}`)
20
  }
21
  const { groups } = await response.json()
22
  return groups.map((g: { id: number; name: string }) => ({
23
    value: String(g.id),
24
    label: g.name,
25
  }))
26
}
27

28
/**
29
 * Create Ticket
30
 * Create a ticket. The comment becomes the ticket's first (public) message. A requester given by email is created as an end user if they don't exist yet; without one the ticket is requested by the authenticated agent. Set any other ticket field through `fields`, e.g. `{ "collaborator_ids": [123], "due_at": "2026-12-01T00:00:00Z", "brand_id": 456 }`.
31
 */
32
export async function main(
33
  auth: RT.Zendesk,
34
  subject: string,
35
  comment: string,
36
  comment_is_html: boolean,
37
  requester_email: string | undefined,
38
  requester_name: string | undefined,
39
  status: "new" | "open" | "pending" | "hold" | "solved" | "closed" | undefined,
40
  priority: "urgent" | "high" | "normal" | "low" | undefined,
41
  type: "problem" | "incident" | "question" | "task" | undefined,
42
  assignee_id: number | undefined,
43
  group_id: DynSelect_group_id | undefined,
44
  tags: string[] | undefined,
45
  custom_fields: { id: number; value: any }[] | undefined,
46
  fields: { [key: string]: any } | undefined
47
) {
48
  const ticket: { [key: string]: any } = {
49
    ...fields,
50
    subject,
51
    comment: comment_is_html ? { html_body: comment } : { body: comment },
52
    status: status || undefined,
53
    priority: priority || undefined,
54
    type: type || undefined,
55
    assignee_id: assignee_id ?? undefined,
56
    group_id: group_id ? Number(group_id) : undefined,
57
    tags: tags?.length ? tags : undefined,
58
    custom_fields: custom_fields?.length ? custom_fields : undefined,
59
  }
60
  if (requester_email) {
61
    ticket.requester = {
62
      email: requester_email,
63
      name: requester_name || undefined,
64
    }
65
  }
66

67
  const response = await fetch(
68
    `https://${auth.subdomain}.zendesk.com/api/v2/tickets`,
69
    {
70
      method: "POST",
71
      headers: {
72
        Authorization: auth.token
73
          ? `Bearer ${auth.token}`
74
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
75
        "Content-Type": "application/json",
76
        Accept: "application/json",
77
      },
78
      body: JSON.stringify({ ticket }),
79
    }
80
  )
81

82
  if (!response.ok) {
83
    throw new Error(`${response.status} ${await response.text()}`)
84
  }
85

86
  return await response.json()
87
}
88

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 533 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * Create Ticket
    8
     * Create Ticket
    9
     */
    10
    export async function main(
    11
      auth: Zendesk,
    12
      body: {
    13
        ticket?: {
    14
          additional_collaborators?: {
    15
            email?: string;
    16
            name?: string;
    17
            [k: string]: unknown;
    18
          }[];
    19
          assignee_email?: string;
    20
          assignee_id?: string;
    21
          attribute_value_ids?: string[];
    22
          brand_id?: string;
    23
          collaborator_ids?: string;
    24
          collaborators?: string;
    25
          comment?: {
    26
            attachments?: {
    27
              content_type?: string;
    28
              content_url?: string;
    29
              deleted?: string;
    30
              file_name?: string;
    31
              height?: string;
    32
              id?: string;
    33
              inline?: string;
    34
              malware_access_override?: string;
    35
              malware_scan_result?: string;
    36
              mapped_content_url?: string;
    37
              size?: string;
    38
              thumbnails?: {
    39
                content_type?: string;
    40
                content_url?: string;
    41
                deleted?: string;
    42
                file_name?: string;
    43
                height?: string;
    44
                id?: string;
    45
                inline?: string;
    46
                malware_access_override?: string;
    47
                malware_scan_result?: string;
    48
                mapped_content_url?: string;
    49
                size?: string;
    50
                url?: string;
    51
                width?: string;
    52
                [k: string]: unknown;
    53
              }[];
    54
              url?: string;
    55
              width?: string;
    56
              [k: string]: unknown;
    57
            }[];
    58
            audit_id?: string;
    59
            author_id?: string;
    60
            body?: string;
    61
            created_at?: string;
    62
            html_body?: string;
    63
            id?: string;
    64
            metadata?: {
    65
              commodo_7f?: string;
    66
              dolore6b?: number;
    67
              [k: string]: unknown;
    68
            };
    69
            plain_body?: string;
    70
            public?: string;
    71
            type?: string;
    72
            uploads?: string[];
    73
            via?: {
    74
              channel?: string;
    75
              source?: { anim3?: number; [k: string]: unknown };
    76
              [k: string]: unknown;
    77
            };
    78
            [k: string]: unknown;
    79
          };
    80
          custom_fields?: string;
    81
          custom_status_id?: string;
    82
          due_at?: string;
    83
          email_cc_ids?: string;
    84
          email_ccs?: {
    85
            action?: string;
    86
            user_email?: string;
    87
            user_id?: string;
    88
            user_name?: string;
    89
            [k: string]: unknown;
    90
          }[];
    91
          external_id?: string;
    92
          follower_ids?: string;
    93
          followers?: {
    94
            action?: string;
    95
            user_email?: string;
    96
            user_id?: string;
    97
            [k: string]: unknown;
    98
          }[];
    99
          group_id?: string;
    100
          macro_ids?: string;
    101
          organization_id?: string;
    102
          priority?: string;
    103
          problem_id?: string;
    104
          raw_subject?: string;
    105
          recipient?: string;
    106
          requester_id?: string;
    107
          safe_update?: string;
    108
          sharing_agreement_ids?: string[];
    109
          status?: string;
    110
          subject?: string;
    111
          submitter_id?: string;
    112
          tags?: string;
    113
          ticket_form_id?: string;
    114
          type?: string;
    115
          updated_stamp?: string;
    116
          via?: {
    117
            channel?: string;
    118
            source?: {
    119
              from?: {
    120
                address?: string;
    121
                id?: string;
    122
                name?: string;
    123
                title?: string;
    124
                [k: string]: unknown;
    125
              };
    126
              rel?: string;
    127
              to?: { address?: string; name?: string; [k: string]: unknown };
    128
              [k: string]: unknown;
    129
            };
    130
            [k: string]: unknown;
    131
          };
    132
          via_followup_source_id?: string;
    133
          [k: string]: unknown;
    134
        };
    135
        [k: string]: unknown;
    136
      }
    137
    ) {
    138
      const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/tickets`);
    139
    
    
    140
      const response = await fetch(url, {
    141
        method: "POST",
    142
        headers: {
    143
          "Content-Type": "application/json",
    144
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    145
        },
    146
        body: JSON.stringify(body),
    147
      });
    148
      if (!response.ok) {
    149
        const text = await response.text();
    150
        throw new Error(`${response.status} ${text}`);
    151
      }
    152
      return await response.json();
    153
    }
    154