SonarQube Web API call

HI, I have created a project through web api call in sonarqube and i want to “configure analysis” for my project with “other ci” option and use the existing token.

for ex: https://sonarqube_url/api/api/projects/create" “name=test-project” -d “key=test-project” -d “project=test-project” -d “mainBranch=develop”

jus tlike above to create project, any api to add token for the above created project?
I don’t know which api needs to be added here.

This needs to be done over web api call, if any help is provided it would be great.

Thanks in advance

Welcome :slight_smile:

the web api call for creating a token for a specific project is api/user_tokens/generate
with the parameters projektKey=yourprojectKey and type=PROJECT_ANALYSIS_TOKEN

see https://<yoursonarhost>/web_api/api/user_tokens/generate

Gilbert

I have a global token created and i wanted to use that token to my project, how can i do this?
I am using community edition -9.9.2 version.

Is there a way to add Existing “Global_Token” to add for my project. If yes please provide the web API

ok.
when using Sonarqube 9.9 LTS
see https://docs.sonarsource.com/sonarqube/9.9/user-guide/user-account/generating-and-using-tokens/

note the property name is sonar.login

when using Sonarqube 10.x

note the property name has changed to sonar.token

Thanks for the response Rebse , i need bit more clarification and can you provide by giving an example.

In my case now,

I have created the “GLOBAL_ANALYSIS_TOKEN” using below command:
curl -u $USERNAME:$PASSWORD “http://$SONAR_IP:9000/api/user_tokens/generate” -d “name=sonarqube_token” -d “type=GLOBAL_ANALYSIS_TOKEN” -d “expirationDate=$(date --date=”+90 days" +%Y-%m-%d)"

and I have created project using:
curl -u $USERNAME:$PASSWORD -X POST “http://$SONAR_IP:9000/api/projects/create” -d “name=demoproject” -d “key=demoproject” -d “project=demoproject” -d “mainBranch=develop”

Now, How can I use my existing Global token which is created above in “demoproject”. Can you please provide the Web API with example.

It’s simply using <token>: instead of username:password for your web api calls, means just the token with separator and a blank password.

WRT to the CI configuration
there’s normally a technical user with full admin permissions.
Its token of type usertoken is then configured for the use in your pipelines / build jobs.

We use Jenkins and there is a Sonarqube Jenkins extension available that takes the usertoken.
You didn’t mention your CI system, but there are integrations for other CI systems too.

After all you may set an environment variable for the admin usertoken and use that.

I am performing “configure analysis” with “Other CI” option and i need to update the previously created token for my “demoproject”.

Can you please provide webAPI for this “Other CI” → Add “existing token”

For the analysis use your new token for the property

sonar.login when using Sonarqube 9
sonar.token when using Sonarqube 10

or token: and BasicAuth for web api calls

can i pass this “sonar.login” while creating project itself through web API, please provide as example to pass this while creating project using my existing “GLOBAL_ANAYSIS_TOKEN”.

Please provide example like:

curl -u $USERNAME:$PASSWORD -X POST “http://$SONAR_IP:9000/api/projects/create” -d “name=demoproject” -d “key=demoproject” -d “project=demoproject” -d “mainBranch=develop”

So, it will be easy to understand and implement the same

Thanks in advance.

as i wrote

means use <token>: instead of $USERNAME:$PASSWORD

curl -u <token>: -X POST “http://$SONAR_IP:9000/api/projects/create” -d “name=demoproject” -d “key=demoproject” -d “project=demoproject” -d “mainBranch=develop”

Thanks for the quick response Gilbert, i have tested with above command but i got below response. It didn’t work:

Error message : curl: (3) URL rejected: Port number was not a decimal number between 0 and 65535

Ah, sorry forgot to mention the syntax is wrong, this should be

curl -u <token>: -X POST “http://$SONAR_IP:9000/api/projects/create?name=demoproject&project=demoproject&mainBranch=develop"

and the web api has no parameter named key, see
https://<yoursonarhost>/web_api/api/projects/create

Sonarqube 9.9 LTS

Sonarqube 10.3 latest

2 Likes

Thank you so much Reb,

It is working.

curl -u $EXISTING_TOKEN: -X POST “http://$SONAR_IP:9000/api/projects/create?name=demoproject&project=demoproject&mainBranch=develop"

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.