JavaScript and NodeJS

A minimal client

To demonstrate the use of the API, we present a minimal client which will use the Deployments endpoint to get a listing of deployments available to an account's user. We use the node-rest-client package to simplify the process of calling the endpoint. This package also offers useful ways to wrap the API endpoints to simplify its use in larger applications.

const util = require('util');
var Client = require('node-rest-client').Client;
var client = new Client();

const apibase = "https://api.compose.io/2016-07/"

var args={
  headers: { "Authorization":"Bearer "+process.env.COMPOSEAPITOKEN}
};

client.get(apibase+"/deployments", args, function (data, response) {
    console.log(util.inspect(data, { depth:null}));
});

Step by step

  • The example code above starts by requiring the Node util library and the Client package of node-rest-client. It then creates a Client instance to service any calls we wish to make.
  • Next, we define an apibase constant which will precede all the REST URIs.
  • For most calls, we will need to present at least a header with the Authorization key and a value set to "Bearer " followed by the API token value. In this case, we take that from the environment variable COMPOSEAPITOKEN.
  • This is the get call. We pass the apibase with deployments appended to it as the URL to query. The next parameter passed are the arguments for the client package, specifically the authorization headers we wish to use. The last parameter is the definition of a callback function.
  • The callback function simply dumps the contents of the returned JSON structure to the console.