PureBasic Zendesk: Create a Ticket

Back to Index

Creates a new Zendesk ticket.

Documentation: https://developer.zendesk.com/rest_api/docs/core/tickets#create-ticket

CURL Command

curl -X POST https://subdomain.zendesk.com/api/v2/tickets.json \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
  "ticket": {
    "subject": "My printer is on fire!",
    "comment": {
      "body": "The smoke is very colorful."
    }
  }
}' \
  -u zendesk_email:zendesk_password

PureBasic Example

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://subdomain.zendesk.com/api/v2/tickets.json
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"subdomain.zendesk.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::ckSetAuthBasic(rest,"zendesk_email","zendesk_password")

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"ticket.subject","My printer is on fire!")
    CkJsonObject::ckUpdateString(json,"ticket.comment.body","The smoke is very colorful.")

    CkRest::ckAddHeader(rest,"Content-Type","application/json")
    CkRest::ckAddHeader(rest,"Accept","application/json")

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/api/v2/tickets.json",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)

    ticketUrl.s
    ticketId.i
    ticketExternal_id.s
    ticketViaChannel.s
    ticketViaSourceRel.s
    ticketCreated_at.s
    ticketUpdated_at.s
    ticketType.s
    ticketSubject.s
    ticketRaw_subject.s
    ticketDescription.s
    ticketPriority.s
    ticketStatus.s
    ticketRecipient.s
    ticketRequester_id.i
    ticketSubmitter_id.i
    ticketAssignee_id.i
    ticketOrganization_id.i
    ticketGroup_id.i
    ticketForum_topic_id.s
    ticketProblem_id.s
    ticketHas_incidents.i
    ticketIs_public.i
    ticketDue_at.s
    ticketSatisfaction_rating.s
    ticketBrand_id.i
    ticketAllow_channelback.i
    auditId.i
    auditTicket_id.i
    auditCreated_at.s
    auditAuthor_id.i
    auditMetadataSystemIp_address.s
    auditMetadataSystemLocation.s
    auditMetadataSystemLatitude.i
    auditMetadataSystemLongitude.i
    auditViaChannel.s
    auditViaSourceRel.s
    i.i
    count_i.i
    id.i
    type.s
    author_id.i
    body.s
    html_body.s
    plain_body.s
    public.i
    audit_id.i
    value.s
    field_name.s
    viaChannel.s
    viaSourceFromDeleted.i
    viaSourceFromTitle.s
    viaSourceFromId.i
    viaSourceRel.s
    subject.s
    j.i
    count_j.i
    intVal.i

    ticketUrl = CkJsonObject::ckStringOf(jsonResponse,"ticket.url")
    ticketId = CkJsonObject::ckIntOf(jsonResponse,"ticket.id")
    ticketExternal_id = CkJsonObject::ckStringOf(jsonResponse,"ticket.external_id")
    ticketViaChannel = CkJsonObject::ckStringOf(jsonResponse,"ticket.via.channel")
    ticketViaSourceRel = CkJsonObject::ckStringOf(jsonResponse,"ticket.via.source.rel")
    ticketCreated_at = CkJsonObject::ckStringOf(jsonResponse,"ticket.created_at")
    ticketUpdated_at = CkJsonObject::ckStringOf(jsonResponse,"ticket.updated_at")
    ticketType = CkJsonObject::ckStringOf(jsonResponse,"ticket.type")
    ticketSubject = CkJsonObject::ckStringOf(jsonResponse,"ticket.subject")
    ticketRaw_subject = CkJsonObject::ckStringOf(jsonResponse,"ticket.raw_subject")
    ticketDescription = CkJsonObject::ckStringOf(jsonResponse,"ticket.description")
    ticketPriority = CkJsonObject::ckStringOf(jsonResponse,"ticket.priority")
    ticketStatus = CkJsonObject::ckStringOf(jsonResponse,"ticket.status")
    ticketRecipient = CkJsonObject::ckStringOf(jsonResponse,"ticket.recipient")
    ticketRequester_id = CkJsonObject::ckIntOf(jsonResponse,"ticket.requester_id")
    ticketSubmitter_id = CkJsonObject::ckIntOf(jsonResponse,"ticket.submitter_id")
    ticketAssignee_id = CkJsonObject::ckIntOf(jsonResponse,"ticket.assignee_id")
    ticketOrganization_id = CkJsonObject::ckIntOf(jsonResponse,"ticket.organization_id")
    ticketGroup_id = CkJsonObject::ckIntOf(jsonResponse,"ticket.group_id")
    ticketForum_topic_id = CkJsonObject::ckStringOf(jsonResponse,"ticket.forum_topic_id")
    ticketProblem_id = CkJsonObject::ckStringOf(jsonResponse,"ticket.problem_id")
    ticketHas_incidents = CkJsonObject::ckBoolOf(jsonResponse,"ticket.has_incidents")
    ticketIs_public = CkJsonObject::ckBoolOf(jsonResponse,"ticket.is_public")
    ticketDue_at = CkJsonObject::ckStringOf(jsonResponse,"ticket.due_at")
    ticketSatisfaction_rating = CkJsonObject::ckStringOf(jsonResponse,"ticket.satisfaction_rating")
    ticketBrand_id = CkJsonObject::ckIntOf(jsonResponse,"ticket.brand_id")
    ticketAllow_channelback = CkJsonObject::ckBoolOf(jsonResponse,"ticket.allow_channelback")
    auditId = CkJsonObject::ckIntOf(jsonResponse,"audit.id")
    auditTicket_id = CkJsonObject::ckIntOf(jsonResponse,"audit.ticket_id")
    auditCreated_at = CkJsonObject::ckStringOf(jsonResponse,"audit.created_at")
    auditAuthor_id = CkJsonObject::ckIntOf(jsonResponse,"audit.author_id")
    auditMetadataSystemIp_address = CkJsonObject::ckStringOf(jsonResponse,"audit.metadata.system.ip_address")
    auditMetadataSystemLocation = CkJsonObject::ckStringOf(jsonResponse,"audit.metadata.system.location")
    auditMetadataSystemLatitude = CkJsonObject::ckIntOf(jsonResponse,"audit.metadata.system.latitude")
    auditMetadataSystemLongitude = CkJsonObject::ckIntOf(jsonResponse,"audit.metadata.system.longitude")
    auditViaChannel = CkJsonObject::ckStringOf(jsonResponse,"audit.via.channel")
    auditViaSourceRel = CkJsonObject::ckStringOf(jsonResponse,"audit.via.source.rel")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.collaborator_ids")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.follower_ids")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.email_cc_ids")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.tags")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.custom_fields")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.sharing_agreement_ids")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.fields")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"ticket.followup_ids")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"audit.events")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        id = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].id")
        type = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].type")
        author_id = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].author_id")
        body = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].body")
        html_body = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].html_body")
        plain_body = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].plain_body")
        public = CkJsonObject::ckBoolOf(jsonResponse,"audit.events[i].public")
        audit_id = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].audit_id")
        value = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].value")
        field_name = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].field_name")
        viaChannel = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].via.channel")
        viaSourceFromDeleted = CkJsonObject::ckBoolOf(jsonResponse,"audit.events[i].via.source.from.deleted")
        viaSourceFromTitle = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].via.source.from.title")
        viaSourceFromId = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].via.source.from.id")
        viaSourceRel = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].via.source.rel")
        subject = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].subject")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jsonResponse,"audit.events[i].attachments")
        While j < count_j
            CkJsonObject::setCkJ(jsonResponse, j)
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jsonResponse,"audit.events[i].recipients")
        While j < count_j
            CkJsonObject::setCkJ(jsonResponse, j)
            intVal = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].recipients[j]")
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "ticket": {
    "url": "https://chilkat.zendesk.com/api/v2/tickets/7.json",
    "id": 7,
    "external_id": null,
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2018-04-15T18:13:15Z",
    "updated_at": "2018-04-15T18:13:15Z",
    "type": null,
    "subject": "My printer is on fire!",
    "raw_subject": "My printer is on fire!",
    "description": "The smoke is very colorful.",
    "priority": null,
    "status": "open",
    "recipient": null,
    "requester_id": 361801727573,
    "submitter_id": 361801727573,
    "assignee_id": 361801727573,
    "organization_id": 360015071793,
    "group_id": 360000681553,
    "collaborator_ids": [
    ],
    "follower_ids": [
    ],
    "email_cc_ids": [
    ],
    "forum_topic_id": null,
    "problem_id": null,
    "has_incidents": false,
    "is_public": true,
    "due_at": null,
    "tags": [
    ],
    "custom_fields": [
    ],
    "satisfaction_rating": null,
    "sharing_agreement_ids": [
    ],
    "fields": [
    ],
    "followup_ids": [
    ],
    "brand_id": 360000471533,
    "allow_channelback": false
  },
  "audit": {
    "id": 391248331553,
    "ticket_id": 7,
    "created_at": "2018-04-15T18:13:15Z",
    "author_id": 361801727573,
    "metadata": {
      "system": {
        "ip_address": "52.25.83.238",
        "location": "Boardman, OR, United States",
        "latitude": 45.86959999999999,
        "longitude": -119.688
      },
      "custom": {}
    },
    "events": [
      {
        "id": 391248331653,
        "type": "Comment",
        "author_id": 361801727573,
        "body": "The smoke is very colorful.",
        "html_body": "<div class=\"zd-comment\"><p dir=\"auto\">The smoke is very colorful.</p></div>",
        "plain_body": "The smoke is very colorful.",
        "public": true,
        "attachments": [
        ],
        "audit_id": 391248331553
      },
      {
        "id": 391248331833,
        "type": "Create",
        "value": "My printer is on fire!",
        "field_name": "subject"
      },
      {
        "id": 391248331973,
        "type": "Create",
        "value": "361801727573",
        "field_name": "requester_id"
      },
      {
        "id": 391248332053,
        "type": "Create",
        "value": "open",
        "field_name": "status"
      },
      {
        "id": 391248332193,
        "type": "Create",
        "value": null,
        "field_name": "priority"
      },
      {
        "id": 391248332313,
        "type": "Create",
        "value": null,
        "field_name": "type"
      },
      {
        "id": 391248332353,
        "type": "Create",
        "value": "361801727573",
        "field_name": "assignee_id"
      },
      {
        "id": 391248332373,
        "type": "Create",
        "value": "360000681553",
        "field_name": "group_id"
      },
      {
        "id": 391248332393,
        "type": "Create",
        "value": "360015071793",
        "field_name": "organization_id"
      },
      {
        "id": 391248332413,
        "type": "Notification",
        "via": {
          "channel": "rule",
          "source": {
            "from": {
              "deleted": false,
              "title": "Notify requester of received request",
              "id": 360018516913
            },
            "rel": "trigger"
          }
        },
        "subject": "[Request received] {{ticket.title}}",
        "body": "Your request ({{ticket.id}}) has been received and is being reviewed by our support staff.\n\nTo add additional comments, reply to this email.\n\n{{ticket.comments_formatted}}",
        "recipients": [
          361801727573
        ]
      },
      {
        "id": 391248332433,
        "type": "Notification",
        "via": {
          "channel": "rule",
          "source": {
            "from": {
              "deleted": false,
              "title": "Notify all agents of received request",
              "id": 360018517033
            },
            "rel": "trigger"
          }
        },
        "subject": "[{{ticket.account}}] {{ticket.title}}",
        "body": "A ticket (#{{ticket.id}}) by {{ticket.requester.name}} has been received. It is unassigned.\n\n{{ticket.comments_formatted}}",
        "recipients": [
          361801727573
        ]
      }
    ],
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    }
  }
}