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.

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:


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:


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

1 Like