Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the API Token for Jenkins

Tags:

jenkins

I am trying to use the jenkins rest api. In the instructions it says I need to have the api key. I have looked all over the configuration pages to find it. How do i get the API key for jenkins?

like image 794
Luke101 Avatar asked Aug 02 '17 16:08

Luke101


People also ask

How do I find my Jenkins API token?

The API token is available in your personal configuration page. Click your name on the top right corner on every page, then click "Configure" to see your API token.

Where are Jenkins API tokens stored?

The Jenkins master authenticates the user against a users' database, in case of a username:password combination, or by comparing tokens with the local user's API token, stored in the user's config. xml file, in case of username:APItoken authentication.

How do I authenticate Jenkins API?

If your Jenkins server requires authentication (and it SHOULD), you'll see a message saying "Authentication Required". The Jenkins API uses HTTP BASIC authentication and requires a username as well as an API token to connect. Then click the box named "Show API Token", and copy the token to your clipboard.


2 Answers

Since Jenkins 2.129 the API token configuration has changed:

You can now have multiple tokens and name them. They can be revoked individually.

  1. Log in to Jenkins.
  2. Click you name (upper-right corner).
  3. Click Configure (left-side menu).
  4. Use "Add new Token" button to generate a new one then name it.
  5. You must copy the token when you generate it as you cannot view the token afterwards.
  6. Revoke old tokens when no longer needed.

Before Jenkins 2.129: Show the API token as follows:

  1. Log in to Jenkins.
  2. Click your name (upper-right corner).
  3. Click Configure (left-side menu).
  4. Click Show API Token.

The API token is revealed.

You can change the token by clicking the Change API Token button.

like image 133
Brian Walker Avatar answered Oct 20 '22 17:10

Brian Walker


The non UI way to do this post Jenkins 2.129 is:

curl 'https://<jenkinsURL>/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken' \ --data 'newTokenName=foo' \ --user username:Password 

which returns:

{   "status": "ok",   "data": {     "tokenName": "foo",     "tokenUuid": "<uuid>",     "tokenValue": "<redacted>"   } } 

Pre Jenkins 2.129

curl http://<username>:<password>@<jenkins-url>/me/configure  
like image 31
RaGe Avatar answered Oct 20 '22 16:10

RaGe