How to attach an image in a zendesk ticket via the API

First step

Perform a call to /api/v2/uploads.json, e.g :

curl “https://{subdomain}.zendesk.com/api/v2/uploads.json?filename=myfile.dat&token={optional_token}” \
-v -u {email_address}:{password} \
-H “Content-Type: application/binary” \
–data-binary @file.dat -X POST

In response, you will receveive a token, e.g :
{
“upload”: {
“token”: “6bk3gql82em5nmf“,

}
}

Second step

Create your ticket via the API like this :

Call https://{subdomain}.zendesk.com/api/v2/tickets.json with the following headers :

Content-Type : application/json
Authorization: Basic [insert here your base64encoded token]

{
“ticket”: {
“subject”: “Hello”,
“description” : “Here is my ticket content”,
“priority”: “normal”,
“type” : “task”,
“comment”: {“uploads”: [“6bk3gql82em5nmf“]}
}
}

How to obtain your base64encoded token ?

  1. In Zendesk, go to Settings, Channels, API and create a new token (e.g : Zendesk generates : Q2oyV3NOdEFJOThpdER2cmlVRDFNQlNDNz)
  2. Now build this string : youremail@company.com/token:Q2oyV3NOdEFJOThpdER2cmlVRDFNQlNDNz
  3. Go to https://www.base64encode.org/ to encode your string; youremail@company.com/token:Q2oyV3NOdEFJOThpdER2cmlVRDFNQlNDNz becomes eW91cmVtYWlsQGNvbXBhbnkuY29tL3Rva2VuOlEyb3lWM05PZEVGSk9UaHBkRVIyY21sVlJERk5RbE5ETno=
  4. Your header must be Authorization: Basic eW91cmVtYWlsQGNvbXBhbnkuY29tL3Rva2VuOlEyb3lWM05PZEVGSk9UaHBkRVIyY21sVlJERk5RbE5ETno