DEV Community

Ido Green for JFrog

Posted on • Originally published at greenido.wordpress.com on

JFrog Artifactory REST API in 5min

A good API is like a classic car - You want to use it again and again.

A good API is like a classic car – You want to use it again and again.

Most of the interactions with Artifactory will be from your CI/CD tools. It might be your build engine or from your log aggregator. This powerful API can be invoked in any of the standard ways you like to work with any other RESTful APIs (e.g. curl, CLI, your source code, etc’).

In many cases, it’s the preferred ‘glue’ for developers when it comes to automation. The options are extensive and you can do many useful things with this API. However, in this short post, we will cover the most popular actions you ‘must have’.

Let’s start with the most common action: “ upload/download binaries “. This action could run automatically from the build machine to Artifactory using:

curl -u myUser:myP455w0rd! -X PUT "http://localhost:8081/artifactory/my-repository/my/new/artifact/directory/myAppBuild.exe" -T ./myAppBuild.exe And with the JFrog CLI: jfrog rt u ./myAppBuild.exe my-repository/my/new/artifact/directory/

As for can see, you can work with the REST API from any technology you like. One good option is to use the JFrog CLI.

It is a compact client (written in Go) that provides a simple interface to automate access to Artifactory. As a wrapper to the API, it offers a way to simplify automation scripts making them more readable and easier to maintain, features such as parallel uploads and downloads, checksum optimization and wildcards/regular expressions make your scripts more efficient and reliable.

Downloading artifacts is one line of GET command:

curl -u myUser:myP455w0rd! -X GET http://localhost:8081/artifactory/libs-release-local/ch/qos/logback/logback-classic/0.9.9/logback-classic-0.9.9.jar And with JFrog CLI: jfrog rt dl libs-release-local/ch/qos/logback/logback-classic/0.9.9/logback-classic-0.9.9.jar

Search

You will be astonished how quickly Artifactory will be the one source of true to all your binaries and how many of them you have. If you wish to find a certain package/library or build file – There is a powerful search API.

You can do some quick searches like this one:

curl -u myUser:myP455w0rd! http://localhost:8081/artifactory/api/search/artifact?name=lib&repos=libs-release-local OR with JFrog CLI: jfrog rt s libs-release-local/lib { "results": [{ "uri": "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/lib/ver/lib-ver.pom" },{ "uri": "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/lib/ver2/lib-ver2.pom" }] }

You can also search and find interesting data points like in the example below, we wish to query all the files that came out of the build (in this case, Jenkins) and that are bigger than 100Mb.

// Put this: items.find( { "type":"file", "created\_by":"jenkins", "size":{"$gt":"100000"} }) .sort({"$desc":["size","name"]}) .limit(50) // Add the query to file: aql-query-1.txt so in the next command, You can execute it. // // Search for the biggest files that our Jenkins built. // You can change it and tune the size/type of the files. // Let's use curl to get the results curl -uYourUserName:YourPassword -X POST http://ArtifactoryUrl/artifactory/api/search/aql -d @aql-query-1.txt -H \"content-type: text/plain\"";

You will get a JSON response that will look like:

{ "results" : [{ "repo" : "docker-prod-local", "path" : "docker-app/65", "name" : "sha256\_\_a56a07bf8a7e40e8580e26b5c7ad51e88e130cce5537bd53aec960de3e9f4e3f", "type" : "file", "size" : 182286302, "created" : "2019-06-10T22:59:11.432Z", "created\_by" : "jenkins", "modified" : "2019-06-10T22:59:08.982Z", "modified\_by" : "jenkins", "updated" : "2019-06-10T22:59:11.433Z" },{ "repo" : "docker-prod-local", "path" : "docker-framework/69", "name" : "sha256\_\_a56a07bf8a7e40e8580e26b5c7ad51e88e130cce5537bd53aec960de3e9f4e3f", "type" : "file", "size" : 182286302, "created" : "2019-06-10T22:57:22.855Z", "created\_by" : "jenkins", "modified" : "2019-06-10T22:57:19.454Z", "modified\_by" : "jenkins", "updated" : "2019-06-10T22:57:22.856Z" },{ "repo" : "docker-stage-local", "path" : "docker-app/65", "name" : "sha256\_\_a56a07bf8a7e40e8580e26b5c7ad51e88e130cce5537bd53aec960de3e9f4e3f", "type" : "file", "size" : 182286302, "created" : "2019-06-10T22:59:11.432Z", "created\_by" : "jenkins", "modified" : "2019-06-10T22:59:08.982Z", "modified\_by" : "jenkins", "updated" : "2019-06-10T22:59:11.433Z" }], "range" : { "start\_pos" : 0, "end\_pos" : 3, "total" : 3, "limit" : 3 } }

Now, you can take this information and get even more data on each docker image or any other package, library, artifact you are using.

Docker

Docker is quite popular these days, so let’s get a list of all our Docker repositories:

curl -u youUserName:youPassWord http://YouArtifactoryServer.com/artifactory/api/docker/docker-stage-local/v2/\_catalog // The response: { "repositories" : ["docker-local-apps", "docker-prod-frameworks"] }

Next, we can fetch the list of all the tags of specified Artifactory Docker repository:

// GET /api/docker/{repo-key}/v2/{image name}/tags/list?n=\<n from the request&gt;&amp;last=\<last tag value from previous response&gt; curl -u youUserName:youPassWord http://YouArtifactoryServer.com/artifactory/api/docker/docker-prod-local/v2/docker-app/tags/list // The response: { "name" : "docker-app", "tags" : ["59", "60", "61", "62", "63", "64", "65", "66", "67", "68"] }

The full example

Check out this:

There are many more options, but it will take (a bit) more than 5 minutes. Check it out at: Artifactory REST API doc.

Be strong!

Top comments (0)