How to add users to Bitbucket Server using Rest API
Because every environment is different there will be a few assumptions made in this example. If the assumptions are not true in the environment that the API will be run then you may need to make modifications or you will get errors. The script example is provided as is and is not guaranteed meet all possible situations. The script provided is meant only as an example and is not formally supported.
Adding many users to the Internal Bitbucket Server Directory is a task that is easier to accomplish with the REST API rather than trying to manually add each user one at a time via the UI
The purpose of this KB is to provide a working example of how to import a large number of users.
- Bitbucket Server is running on localhost
- Bitbucket Server is running on HTTP using the default port
- Bitbucket Server is running with a context path called Bitbucket
- The Sys Admin user is defined as Admin
- The Sys Admin user password is defined as Admin
This would then be the URL for accessing Bitbucket Server: http://localhost:7990/bitbucket
Add A User
curl to add a user to Bitbucket Server
curl -u Admin:Admin -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=<USERNAME_TO_ADD>&password=<PASSWORD_FOR_USER>&displayName=<DISPLAY_NAME_FOR_USER>&emailAddress=<EMAIL_ADDRESS_FOR_USER>&addToDefualtGroup=true¬ify=false"
<USERNAME_TO_ADD>- is replaced with the user name that you want to add
<PASSWORD_FOR_USER>- is replaced with the password the new user will use. The user can edit this password when they login but they will not be forced to change the password
<DISPLAY_NAME_FOR_USER>- is replaced with the full name of the user that you are adding
<EMAIL_ADDRESS_FOR_USER>- is replaced with the email address of the user that you are addingWorking example
curl -u Admin:Admin -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=Marty&password=P-ssw0rd&displayName=Marty%20Mouse&emailAddressfirstname.lastname@example.org&addToDefualtGroup=true¬ify=false"
Below is an example data file and bash script for adding any number of users from a file with the name of users.txtusers.txt
marty P-ssw0rd Marty%20Mouse email@example.com pluto P-ssw0rd1 Plutto%20Dog firstname.lastname@example.org goofy P-ssw0rd2 Goofy%20Dog email@example.com
(Make sure there is no blank lines at the end of the file)AddUsers.sh
#!/bin/bash ABORT="N" case "$1" in "") echo "Usage: AddUsers.sh Admin_UserName Admin_Password" ABORT="Y";; * ) USERNAME=$1;; esac if [ "$ABORT" == 'N' ]; then case "$2" in "") echo "Usage: AddUsers.sh Admin_UserName Admin_Password" ABORT="Y";; * ) PASSWORD=$2;; esac fi if [ "$ABORT" == 'N' ]; then clear while read -r a b c d; do echo "User: $a" curl -u $USERNAME:$PASSWORD -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=$a&password=$b&displayName=$c&emailAddress=$d&addToDefualtGroup=true¬ify=false" done < users.txt fi