DEV Community

Arief Warazuhudien
Arief Warazuhudien

Posted on

How to call Jenkins API

Jenkins is a popular automation server that provides an API for interacting with its features programmatically. By making HTTP requests to Jenkins API endpoints, you can automate various tasks and integrate Jenkins with other systems or scripts. In this guide, we'll explore how to use the curl command-line tool to interact with Jenkins API.

Before making API requests, you'll need to obtain an API token from Jenkins. The API token acts as a credential to authenticate your requests. Here's how you can generate an API token:

  1. Log in to Jenkins using your Jenkins credentials.
  2. Click on your username or the user icon in the top-right corner of the Jenkins interface to access your user profile.
  3. Locate the "Configure" or "Configure User" option and click on it.
  4. Look for the section related to API tokens, typically labeled as "API Token" or similar.
  5. Click on the "Add new Token" or similar button to generate a new API token.
  6. Provide any necessary authentication or password to proceed.
  7. Once the API token is generated, copy and securely store it as it may not be displayed again.

With the API token in hand, you can now use curl to make API requests to Jenkins. Here's an example command to call a Jenkins API endpoint:

curl -s -X GET -u ${USERNAME}:${API_TOKEN} ${JENKINS_URL}/${API_ENDPOINT}
Enter fullscreen mode Exit fullscreen mode

Let's break down the command:

  • -s makes curl silent, disabling the progress meter for script-friendly output.
  • -X GET specifies the HTTP request method as GET. You can replace GET with other HTTP methods like POST, PUT, or DELETE depending on the desired action.
  • -u ${USERNAME}:${API_TOKEN} provides the basic authentication credentials. Replace ${USERNAME} with your Jenkins username and ${API_TOKEN} with the API token you generated earlier.
  • ${JENKINS_URL} should be replaced with the base URL of your Jenkins instance.
  • ${API_ENDPOINT} represents the specific API endpoint you want to access. For example, to retrieve information about a job, you could use /job/{jobName}/api/json.

By executing this curl command, you can retrieve data or perform actions through Jenkins API. The response from Jenkins will be displayed in the command output.

Remember to consult the Jenkins API documentation or your Jenkins administrator for details about available endpoints, request parameters, and response formats specific to your Jenkins installation.

In summary, by leveraging curl and the Jenkins API token, you can easily automate and integrate Jenkins with your scripts or external applications.

Top comments (0)