How to create Jira issues using Python script with PAT (Personal Access Token) as the authentication mechanism
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
The purpose of this article is to describe a way to interact with Jira via a Python script using Personal Access Token (PAT) as the authentication mechanism.
We will use the Python library jira
for this use case:
More information about this Python library can be found at https://pypi.org/project/jira/
Detailed documentation can be found at: https://jira.readthedocs.io/
Please note here that this python library is not developed or managed by Atlassian and due to the same fact, it falls outside of the Atlassian support purview. Nevertheless, we have seen many users who use Python scripts and make use of this library to interact with the Jira.
Environment
Jira Data Center on any version from 9.0.0.
Solution
Prerequisites:
Python must be installed on the machine where the script should run.
Python library jira must be installed.
To install Python, users can refer to the python website: https://www.python.org/downloads/ and the appropriate distribution can be downloaded and installed as per the Operating System.
Once the Python setup is complete, users can install the python Jira library using below commands:
1
2
pip install jira -- This is in case Python2 is installed
pip3 install jira -- This is in case Python3 is installed
This library support variety of actions and details of the same can be found in the documentation of this library but for academic purposes, we will use it to just create a Jira issue and will use the PAT token for authentication purpose.
Python Script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from jira import JIRA
# Replace with your Jira instance URL and Personal Access Token (PAT)
host = "<JIRA_BASE_URL>"
pat = "<YOUR-PERSONAL-ACCESS-TOKEN>"
# Create a Jira connection using the JIRA library
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
headers["Authorization"] = f"Bearer {pat}"
jira = JIRA(server=host, options={"headers": headers})
# Issue data (replace with your own data)
issue_data = {
"project": {"key": "<YOUR_PROJECT_KEY>"},
"summary": "New issue created via Python",
"description": "This is a sample issue created using Python script.",
"issuetype": {"name": "Task"},
}
try:
# Create the issue
new_issue = jira.create_issue(fields=issue_data)
# Print the key of the created issue
print("Issue created successfully!")
print("Issue Key:", new_issue.key)
except Exception as e:
print("Failed to create issue:", str(e))
<JIRA_BASE_URL>: Replace this with the URL of your Jira instance.
<YOUR-PERSONAL-ACCESS-TOKEN>: Replace this with your Personal Access Token.
<YOUR_PROJECT_KEY>: Replace this with the key of the Jira project where you want to create the issue.
Disclaimer
Please note that scripting/development work falls outside of the Atlassian support scope, however we can use the above script as a starting point to understand, how we can create Python script to perform desired actions and use PAT mechanism to authenticate to Jira.
Was this helpful?