Get Build Number with REST API

Article ID:360028147532
2 minute readKnowledge base


Using the REST API, how do I trigger a new build for a job, and get the build number for that new build?


1. Modify JENKINS_URL, USER, and API_TOKEN in the following script to match your specific environment to start a non-parameterized build from a shell script:


JENKINS_CRUMB=$(curl -q -u ${USER}:${API_TOKEN} "${JENKINS_URL}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")

curl -u ${USER}:${API_TOKEN} -H "${JENKINS_CRUMB}" -i -X POST ${JENKINS_URL}/job/${BUILD}/build

This will yield the following output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    46  100    46    0     0   5270      0 --:--:-- --:--:-- --:--:--  5750
HTTP/1.1 201 Created
Date: Wed, 15 May 2019 15:52:26 GMT
X-Content-Type-Options: nosniff
Content-Length: 0
Server: Jetty(9.4.z-SNAPSHOT)
The queue URL (in this example will expire 5 minutes after the queue item is assigned a build number for the job so the URL will stay valid even if the build sits in the build queue for a long time.

2. Use curl to query api/ of the Location URL from Step 1 to get the build number

curl -u ${USER}:${API_TOKEN} -H "${JENKINS_CRUMB}" ''

This will yield the following output:

"executable" : {
"_class" : "hudson.model.FreeStyleBuild",
"number" : 5
"url" : ""

3. The URL (in this example will be there once the queue item gets assigned a build number.