How to change (modify) a username of a user by CLI with Gitlab API

Good morning all ! :grinning: :christmas_tree:

First of all, sorry for my level of English, it is low.

My goal is to be able to change (modify) the username of an existing account on my Gitlab self-managed but in command line to then automate it.

I looked at the Gitlab documentation and here are the links I checked:

with this documentation, here is what I concluded but unfortunately does not work and I can not understand how to change username with API:

The account is ā€œmonicaā€ and i want to modify that by ā€œjulieā€

curl --request PUT --header "PRIVATE-TOKEN: ITISASECRET" "https://my.gitlab.server/api/v4/users?username=**monica**" --data "username=**julie**"

Here ! I hope you can help me !

The URL endpoint needs to specify the target userā€™s ID. Use https://my.gitlab.server/api/v4/users to find all usernames, then extract the ID (id key in responses) of existing username monica and send the PUT request to https://my.gitlab.server/api/v4/users/ID.

Also, what roleā€™s credential are you using? To alter usernames, the personal authentication token needs to be from an admin role user, and the api scope must be enabled when generating the token.

1 Like

Hello, first of all, thanks you ! it works ! :smiley: :smiley:

For future people who would need to do the same :

the monica account is the Administrator account which is the only admin of the instance. I generated an ā€œAccess tokenā€ with the api and sudo scopes.
after, I launched the following command:

curl --request GET --header "PRIVATE-TOKEN: ITSASECRET" "https://my.gitlab.server/api/v4/user"

the result:

{ā€œidā€:1,ā€œnameā€:ā€œAdministratorā€,ā€œusernameā€:ā€œmonicaā€,ā€œstateā€:ā€œactiveā€,ā€œavatar_urlā€:"https://seā€¦

I get the ID which is 1. As soon as I run the following command, it doesnā€™t work, I have a 404 error.

curl --request PUT --header ā€œPRIVATE-TOKEN: ITSASECRETā€ ā€œhttps://my.gitlab.server/api/v4/users/1ā€ --data ā€œusername=julieā€

the result:

{ā€œidā€:1,ā€œnameā€:ā€œAdministratorā€,ā€œusernameā€:ā€œjulieā€,ā€œstateā€:ā€œactiveā€,ā€œavatar_urlā€:"https://seā€¦

Thanks for the help :yum:. See you soon and merry christmas :christmas_tree: :christmas_tree:

1 Like