How to bulk create Users in Jira
The content on this page relates to platforms which are supported; however, the content is out of scope of our Atlassian Support Offerings. Consequently, Atlassian cannot guarantee support. Please be aware that this material is provided for your information only and you may use it at your own risk.
Purpose
This article discusses how you can leverage Create user Rest API endpoint to create bulk users in Jira. This approach is targeted for all the Jira versions, where /rest/api/2/user endpoint is supported.
Solution
The CSV file with the user information or supported payload(emailAddress, displayName, name, password) is to be prepared prior to script execution.
Option 1 Bash command line
Create a CSV file(without headers) as in the following example:
# cat test.csv test1,Test User 1,test1@sample.com test2,Test User 2,test2@sample.com test3,Test User 3,test3@sample.com test4,Test User 4,test4@sample.com test5,Test User 5,test5@sample.com
Prepare the script by replacing the Jira base URL, username and password in the following script:
# cat bulkuser.sh #!/bin/bash while read i ; do emailAddress=$(echo $i | cut -d "," -f3); name=$(echo $i | cut -d "," -f1); displayName=$(echo $i | cut -d "," -f2); data='{"emailAddress": "'${emailAddress}'","displayName": "'${displayName}'","name": "'${name}'"}'; echo $data; curl -X POST --url '<JIRA_BASE_URL>/rest/api/2/user' --user 'username:password' --header 'Accept: application/json' --header 'Content-Type: application/json' --data "${data}" ; echo $command; done < test.csv
- Now, execute the bash script to create users in bulk
Option 2 Using Python script
Create a CSV file as in the following example, separated with ",":
# cat bulkuser.py name,displayName,emailAddress,password test31,Test User 31,123@sample.com,test@123 test32,Test User 32,123@sample.com,test@123 test33,Test User 33,123@sample.com,test@123 test34,Test User 34,123@sample.com,test@123 test35,Test User 35,123@sample.com,test@123
Prepare the script as follows:
import sys import csv import os from optparse import OptionParser import requests from requests.auth import HTTPBasicAuth import json parser = OptionParser() parser.add_option("--username", dest="username", help="API username") parser.add_option("--password", dest="password", help="API password") parser.add_option("--url", dest="weburl", help="Endpoint hostname") parser.add_option("--input-file", dest="INPUT_FILE", help="CSV values of emailAddress name displayName") parser.add_option("--csv-delimiter", dest="dlimit", help="CSV seperator used") (options, args) = parser.parse_args() if not options.INPUT_FILE: parser.error("INPUT_FILE must be specified") if not options.username or not options.weburl or not options.password or not options.dlimit : parser.error("--username <username> --password <password> --url <endpoint uri> --input-file <input csv file with header emailAddress name displayName> --csv-delimiter <; or ,>") url = "https://"+options.weburl+"/jira/rest/api/2/user" auth = HTTPBasicAuth(options.username, options.password) headers = {"Accept": "application/json", "Content-Type": "application/json"} with open(options.INPUT_FILE) as csvfile: reader = csv.DictReader(csvfile, delimiter=options.dlimit) for row in reader: emailAddress = row['emailAddress'] displayName = row['displayName'] name = row['name'] password = row['password'] payload = json.dumps( {"emailAddress": emailAddress, "displayName": displayName, "name": name, "password": password } ) response = requests.request("POST", url, data=payload, headers=headers, auth=auth) print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
Execute the script as below to create users in bulk(--url should be just hostname and without https):
python bulkuser.py --username admin --password admin --url jira.atlassian.com --input-file ./test.csv --csv-delimiter ,
- Script usage: