How to request JIRA API with Python examples

Jira API is simple and powerful but you may have difficulties using it with google account. You will find many solutions on the web most of which nor working anymore as deprecated. In this article you will find information how to run requests against the API in 2018 using python for public and private projects(google account and atlassian accounts).

Request JIRA API with module jira

Your first option to request information and update the JIRA API is by using module jira. It can be installed by:

pip install jira

and used by:

# This script shows how to use the client in anonymous mode
# against jira.atlassian.com.
from jira import JIRA
import re

# By default, the client will connect to a JIRA instance started from the Atlassian Plugin SDK
# (see https://developer.atlassian.com/display/DOCS/Installing+the+Atlassian+Plugin+SDK for details).
# Override this with the options parameter.
options = {
    'server': 'https://jira.atlassian.com'}
jira = JIRA(options)

# Get all projects viewable by anonymous users.
projects = jira.projects()

# Sort available project keys, then return the second, third, and fourth keys.
keys = sorted([project.key for project in projects])[2:5]
print(keys)

# Get an issue.
issue = jira.issue('JRA-1330')
print(issue)

# Find all comments made by Atlassians on this issue.
atl_comments = [comment for comment in issue.fields.comment.comments
                if re.search(r'@atlassian.com$', comment.author.emailAddress)]
print(atl_comments)

result:

['BAMJ', 'BLOGIT', 'BON']
JRASERVER-1330
[]

As you can see this code is working for public projects.

More information about module JIRA:

I wasn't able to log into private project with google accound by using jira module. But using direct request and JIRA access token I was able to request information. It's visible in the next section.

Request JIRA API with requests

If you want to request information from private projects you can with google account then you can try second option which is:

import requests

r = requests.get('https://myproject.atlassian.net/rest/api/2/search?jql=project="myproject"&maxResults=20', auth=('myuser@mydomain.com', 'your_token_access_key'))


for k in response.json().keys():
    #print(k)
    #print(response.json()[k])
    if k == 'fields':
        print(response.json()[k]['issuetype'])
        print(response.json()[k]['description'])
        print(response.json()[k]['lastViewed'])

print(r.json())

where:

Note: JIRA API authentication with password is not working in 2018 as password is deprecated. More information below: Basic auth for REST APIs:

Using passwords with Jira REST API basic authentication

Support for passwords in REST API basic authentication is deprecated and will be removed in the future. While the Jira REST API currently accepts your Atlassian account password in basic auth requests, we strongly recommend that you use API tokens instead. We expect that support for passwords will be deprecated in the future and advise that all new integrations be created with API tokens.

If you have problems accessing the API with code you can try to get this URL in the browser while you are loged in your JIRA account:

https://myproject.atlassian.net/rest/api/latest/issue/MY_ISSUE_KEY?expand=names,renderedFields	

For more information about the API you can visit this links:

Related Article