PureBasic Zendesk: Create a Ticket with an Attachment (a previously uploaded file)

Back to Index

Creates a new Zendesk ticket with an attachment. The file attachment is indicated by an array of attachment tokens contained within the comment. (The attachment should be uploaded beforehand, and the token can be obtained from the JSON response of the attachment file upload.)

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 application crashed.  Please help.",
   "comment":  { "body": "Here is a screen shot", "uploads":  ["W7Y11FCxfQlzOFPDbVDUEE45C"] }
  }
}' \
  -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 application crashed.  Please help.")
    CkJsonObject::ckUpdateString(json,"ticket.comment.body","Here is a screen shot")
    CkJsonObject::ckUpdateString(json,"ticket.comment.uploads[0]","W7Y11FCxfQlzOFPDbVDUEE45C")

    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
    url.s
    file_name.s
    content_url.s
    mapped_content_url.s
    content_type.s
    size.i
    width.s
    height.s
    inline.i
    k.i
    count_k.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)
            url = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].url")
            id = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].attachments[j].id")
            file_name = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].file_name")
            content_url = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].content_url")
            mapped_content_url = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].mapped_content_url")
            content_type = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].content_type")
            size = CkJsonObject::ckIntOf(jsonResponse,"audit.events[i].attachments[j].size")
            width = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].width")
            height = CkJsonObject::ckStringOf(jsonResponse,"audit.events[i].attachments[j].height")
            inline = CkJsonObject::ckBoolOf(jsonResponse,"audit.events[i].attachments[j].inline")
            k = 0
            count_k = CkJsonObject::ckSizeOfArray(jsonResponse,"audit.events[i].attachments[j].thumbnails")
            While k < count_k
                CkJsonObject::setCkK(jsonResponse, k)
                k = k + 1
            Wend
            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/11.json",
    "id": 11,
    "external_id": null,
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2018-04-15T20:29:49Z",
    "updated_at": "2018-04-15T20:29:49Z",
    "type": null,
    "subject": "My application crashed.  Please help.",
    "raw_subject": "My application crashed.  Please help.",
    "description": "Here is a screen shot",
    "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": 391509606014,
    "ticket_id": 11,
    "created_at": "2018-04-15T20:29:49Z",
    "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": 391509606034,
        "type": "Comment",
        "author_id": 361801727573,
        "body": "Here is a screen shot",
        "html_body": "<div class=\"zd-comment\"><p dir=\"auto\">Here is a screen shot</p></div>",
        "plain_body": "Here is a screen shot",
        "public": true,
        "attachments": [
          {
            "url": "https://chilkat.zendesk.com/api/v2/attachments/360887735934.json",
            "id": 360887735934,
            "file_name": "sample.pdf",
            "content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
            "mapped_content_url": "https://chilkat.zendesk.com/attachments/token/Q7wAubpKB2KERnNFj89JmdvS1/?name=sample.pdf",
            "content_type": "application/pdf",
            "size": 178399,
            "width": null,
            "height": null,
            "inline": false,
            "thumbnails": [
            ]
          }
        ],
        "audit_id": 391509606014
      },
      {
        "id": 391509606394,
        "type": "Create",
        "value": "My application crashed.  Please help.",
        "field_name": "subject"
      },
      {
        "id": 391509606414,
        "type": "Create",
        "value": "361801727573",
        "field_name": "requester_id"
      },
      {
        "id": 391509606434,
        "type": "Create",
        "value": "open",
        "field_name": "status"
      },
      {
        "id": 391509606454,
        "type": "Create",
        "value": null,
        "field_name": "priority"
      },
      {
        "id": 391509606474,
        "type": "Create",
        "value": null,
        "field_name": "type"
      },
      {
        "id": 391509606494,
        "type": "Create",
        "value": "361801727573",
        "field_name": "assignee_id"
      },
      {
        "id": 391509606514,
        "type": "Create",
        "value": "360000681553",
        "field_name": "group_id"
      },
      {
        "id": 391509606534,
        "type": "Create",
        "value": "360015071793",
        "field_name": "organization_id"
      },
      {
        "id": 391509606554,
        "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": 391509606574,
        "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
      }
    }
  }
}