Getting started with Jenkins Rest API

Jenkins-Logo

Before we can access Jenkins API, we need grab the API key associated with your account.

  • Login into your Jenkins instance.
  • Click on Manage Jenkins
  • Scroll down to find Manage Users and click on the link.
  • Under your username, click on the gear.
  • Under API token, click on the “Show API Token…”
  • Other way to get to the Configure link is using this URL : http://[Jenkins Instance]:8080/me/configure
  • Copy the API token, you would need this information while making API calls to Jenkins Rest API
  • Now your are ready to test the API and run few command.

Oh! One other thing you need to do before you could curl up. Navigate to Manage Jenkins>>Configure Global Security and uncheck the box “Prevent Cross Site Request Forgery exploits”. I don’t know why but you need to uncheck this before you could run the POST commands below. Some useful information here – http://stackoverflow.com/questions/16738441/how-to-request-for-crumb-issuer-for-jenkins

Lets open the Shell or command prompt. I am using SampleFreeStyleJob for the Jenkins Project.

Trigger a build:
curl -X POST http://[Jenkins_Instance]:8080/job/SampleFreeStyleJob/build --user [USER_NAME]:[API_TOKEN]

Retrieve a project config.xml file
curl http://[Jenkins_Instance]:8080/job/SampleFreeStyleJob/config.xml --user [USER_NAME]:[API_TOKEN]

Disable a project
curl -X POST http://[Jenkins_Instance]:8080/job/SampleFreeStyleJob/disable --user [USER_NAME]:[API_TOKEN]

Enable a project
curl -X POST http://[Jenkins_Instance]:8080/job/SampleFreeStyleJob/enable --user [USER_NAME]:[API_TOKEN]

More Examples

> curl http://[Jenkins_Instance]:8080/api/json?pretty=true --user [USER_NAME]:[API_TOKEN]
> curl -g http://[Jenkins_Instance]:8080/api/json?pretty=true&tree=jobs[name,color] --user [USER_NAME]:[API_TOKEN]
> curl -g http://[Jenkins_Instance]:8080/job/[Job Name]/config.xml -o config.xml [USER_NAME]:[API_TOKEN]