DEV Community

loading...

Adventures in Zendesk and Jira REST APIs

brunojuliao profile image Bruno Julião ・3 min read

We migrated from Jira's cloud version to on-premises. This forced me to update some of our code as the endpoint changed, the custom fields and also authentication.

On Jira's cloud, our C# tool uses "Atlassian.Net SDK" (https://bitbucket.org/farmas/atlassian.net-sdk/src/master/), which I recommend. To access its api, you are required to create a token (https://id.atlassian.com/manage/api-tokens). The authentication is as simple as using your username and the token as password:

var jira = Jira.CreateRestClient(jiraUrl, username, password);

Done. You have access to everything. At least everything I needed.
Although, as I said, we changed to on-premises. The auth is a little different. We have a specific user only for programmatic access. This user is not in our domain. Instead of using a token, I use only the user's password. Same line than before. The difference is just password's content. Probably an application was also created and the user assigned to it, although I can't confirm as I don't have access.

One tip, if you already have some code querying/using Jira, pay special attention to the custom fields. For example, in Python, the object returned has them in this fashion:

import jira

cf_team = 123
conn = jira.JIRA(host, auth = (username, password))

#Query using a custom field. Usually cf[123]
issues = conn.search_issues("cf[{cf_team}] = 'My Team'".format(cf_team = cf_team))

#Regular property usage
temp = issues[0].fields.customfield_123

#More flexible
temp = issues[0].raw['fields']['customfield_{cf_team}'.format(cf_team = cf_team)]

That "123" is the field's id. The lib used is this: https://pypi.org/project/jira/

As you might have noticed, it's easy when moving from one installation to the other have the IDs changing. The code above have ways to work around and keep the code easier to maintain. You can have all the cfs somewhere else and just use/reference it. If the IDs change, you need to change only a single place. I admit it's not usual to move often Jira instalations. A simple search/replace could've fixed it. True.

Alright. Enough Jira. What about Zendesk?

On Zendeskt side, I only have a Python sample. Although, I haven't used any Zendesk specific lib. Just regular HTTP requests using the requests lib. It's as simple as:

import requests

subdomain = "mycompany"
username = "user@domain.com"
api_key = "xyz"
id = 123

url = 'https://{subdomain}.zendesk.com/api/v2/tickets/{id}.json'.format(subdomain = subdomain, id = id)
headers = {'Accept': 'application/json'}
r = requests.get(url, auth = ('{username}/token'.format(username = username), api_key), headers = headers)
ticket_dict = r.json()
print(ticket_dict['ticket']['id'])

Zendesk's API: https://developer.zendesk.com/rest_api/docs/zendesk-apis/resources
Types of authentication: https://developer.zendesk.com/rest_api/docs/support/requests

Observe that "/token" appended to the username. In the docs they say {enduser_email_address}/token:{api_token} . In the Authorization (HTTP) header, usually it's Authorization: Basic {user_plus_token_base64encoded} (assuming your are using the Basic authentication). You can find several references about basic auth saying the content to be encoded is just username:password (where password can be a token). It took me a good amount of time to notice and properly append the /token to the username... Silly, right?

I hope the content shared here help :)

Discussion (0)

pic
Editor guide