Below is one method showing usage of our API with NODE.JS separated by different functions.

  1. Before you start using it, make sure that you have maxcdn library installed:

          $ npm install maxcdn
          npm WARN package.json vows@0.7.0 No repository field.
          npm http GET https://registry.npmjs.org/maxcdn
          npm http 200 https://registry.npmjs.org/maxcdn
          npm http GET https://registry.npmjs.org/maxcdn/-/maxcdn-0.1.3.tgz
          npm http 200 https://registry.npmjs.org/maxcdn/-/maxcdn-0.1.3.tgz
          npm http GET https://registry.npmjs.org/oauth
          npm http 200 https://registry.npmjs.org/oauth
          maxcdn@0.1.3 node_modules/maxcdn
          └── oauth@0.9.11
          
  2. When library is installed, you can proceed with coding. First create a file where your functions will be stored in by running:

          $ sudo nano main.js
          
  3. And by referencing to below sections you can compose one JS file to execute all functions from a same prompt. So, let’s include our library first and set some options we can use to trigger different sections.

          var MaxCDN = require('maxcdn');
          var api = new MaxCDN(ALIAS, KEY, SECRET);
          console.log("1. Get account info\n2. Get list of zones\n3. Generate reports\n4. Purge\n5. Create zone\n6. Manage custom domains\n7. Manage zone\nChoose option: \n");
          var prompt = require('prompt');
            prompt.start();
            prompt.get(['insert'], function (err, result) {
    
          var s = result.insert;
    
          switch(s)
          {
          case '1':
            accountinfo();
            break;
          case '2':
              listzones();
            break;
          case '3':
              reports();
          break;
          case '4':
              purge();
          break;
          case '5':
              create_zone();
          break;
          case '6':
              customdom();
          break;
          case '7':
              zonedetails();
          break;
          default:
            console.log("code to be executed if n is different from case 1 and 2");
          }
          });
          
  4. Get account info
    Using GET method we are sending a request to API and parsing response based on personal use case:

          //****************** account info *********************//
          function accountinfo(){
          api.get('account.json', function(err, results) {
              if (err) {
                  console.trace(err);
              } else {
                  var res = results.data;
                  for(var data in res){
                  console.log("Created: " + res[data]['date_created']);
                  console.log("Account ID: " + res[data]['id']);
                  console.log("Company Name: " + res[data]['name']);
                  console.log("SSL Credits: " + res[data]['ssl_credits']);
                  console.log("Flex Credits: " + res[data]['flex_credits']);
                  console.log("Secure Tokens Credits: " + res[data]['secure_token_pull_credits']);
                  }
              }
          });
          }
          
  5. List zones
    Methods used: GET.

          //*************** list zones ******************//
    
          function listzones(){
          var prompt = require('prompt');
          prompt.start();
          var type = "";
          console.log("Zone type: ");
          prompt.get(['zonetype'], function (err, result) {
          var type = "zones/" + result.zonetype + ".json";
          console.log(type);
          var input = result.zonetype;
          console.log(input);
          get(type, input);
          });
          }
          function get(type, ztype){
              api.get(type, function(err, results) {
                  if (err) {
                      console.trace(err);
                  } else {
                      var rr = "";
                                  switch(ztype){
                                          case 'pull':
                                          rr = results.data.pullzones;
                                          break;
                                          case 'push':
                                          rr = results.data.pushzones;
                                          break;
                                          case 'vod':
                                          rr = results.data.vodzones;
                                          break;
                                          case 'live':
                                          rr = results.data.livezones;
                                          break;
                                          default: console.log("Oops! Looks like made a typo...");
                                  }
                      console.log(rr);
                      for(var data in rr){
                          console.log(rr[data]['id'] + "\n-----------\n");
                          console.log(rr[data]['name']);
                          console.log(rr[data]['url']);
                          console.log(rr[data]['label']);
                          console.log(rr[data]['cdn_url']);
                      }
                  }
              });
          }
          
  6. Reporting
    Methods: GET.

          //*************** reports ******************//
          function reports(){
              var prompt = require('prompt');
              prompt.start();
              console.log("Report type (hourly, daily, monthly): ");
              prompt.get(['insert'], function (err, result) {
                  console.log('Input: ' + result.insert);
                  type = "reports/stats.json/" + result.insert;
                  var input = result.insert;
                  getrep(type, result.insert);
              });
              function getrep(type, input){
                  console.log(type);
                  api.get(type, function(err, results) {
                      if (err) {
                          console.trace(err);
                          } else {
                              console.log("Summary");
                              console.log("Hits: " + results.data.summary['hit'] + "\nMisses: " + results.data.summary['noncache_hit'] + "\nNumber of requests: " + results.data.summary['hit'] + "\nTotal traffic (bytes): " + results.data.summary['size']);
                              console.log("All");
                              var tr = results.data.stats;
                              for(var data in tr){
                                  console.log("Time: " + results.data.stats[data]['timestamp']);
                                  console.log("Cache Hits: " + results.data.stats[data]['cache_hit']);
                                  console.log("Cache Misses: " + results.data.stats[data]['noncache_hit']);
                                  console.log("Number of requests: " + results.data.stats[data]['hit']);
                                  console.log("Total Traffic (bytes): " + results.data.stats[data]['size']);
                              }
                          }
                      });
                  }
              }
          
  7. Purging
    Methods used: DELETE.

          //**************** purge ******************//
          function purge(){
          var prompt = require('prompt');
            prompt.start();
            prompt.get(['zoneid', 'file'], function (err, result) {
              console.log('Zone ID: ' + result.zoneid);
              console.log('File (blank for all): ' + result.file);
          var zoneid = result.zoneid;
          var file = { files: [ '' + result.file + '' ] };
          purgeme(zoneid,file);
            });
          function purgeme(zoneid,file){
          var zoneId = zoneid;
          console.log(file);
          api.delete('zones/pull.json/'+zoneId+'/cache', file, function(err, results) {
              if (err) {
                  console.trace(err);
                  return;
              }
              if (results.code === 200) {
                  console.log('SUCCESS!');
              }
          });
          }
          }
          
  8. Create Zone
    Methods used: POST.

          //**************** create zone *****************//
          function create_zone(){
          var prompt = require('prompt');
          prompt.start();
          prompt.get(['zonename', 'originurl', 'label'], function (err, result) {
                  var zname = result.zonename;
                  var ourl = result.originurl;
                  var zlabel = result.label;
          api.post('zones/pull.json', { name: '' + zname + '', url: '' + ourl + '', label: '' + zlabel + '' }, function(err, results) {
                  if (err) {
                  console.trace(err);
                  return;
                  }
                  console.dir(results);
          });
          });
          }
          
  9. Manage Custom Domains
    Methods used: GET, POST.

          //***************** custom domains *********************//
          function customdom(){
          console.log("Chose action (view, add remove): ");
          var prompt = require('prompt');
          prompt.start();
          prompt.get(['action'], function (err, result) {
                  var action = result.action;
              switch(action){
                  case 'view':
                  viewdoms();
                  break;
                  case 'add':
                  customadd();
                  break;
                  case 'remove':
                  remove();
                  break;
              }
          });
          function remove(){
          var prompt = require('prompt');
          prompt.start();
          prompt.get(['zone_type', 'zone_id', 'custom_domain_id'], function (err, result) {
                  var ztype = result.zone_type;
                  var zoneid = result.zone_id;
                  var cdomid = result.custom_domain_id;
          api.delete('zones/'+ ztype +'/'+ zoneid +'/customdomains.json/' + cdomid, function(err, results) {
              if (err) {
                  console.trace(err);
                  return;
              }
              else{
                  console.log("CUSTOM DOMAIN REMOVED!");
                  }
          });
          });
          }
          function customadd(){
          var prompt = require('prompt');
          prompt.start();
          prompt.get(['zone_type', 'zone_id', 'custom_domain'], function (err, result) {
                  var ztype = result.zone_type;
                  var zoneid = result.zone_id;
                  var cdom = result.custom_domain;
    
          api.post('zones/'+ ztype +'/'+ zoneid +'/customdomains.json', { custom_domain: '' + cdom + '' }, function(err, results) {
              if (err) {
                  console.trace(err);
                  return;
              }
              else{
              console.log("CUSTOM DOMAIN ADDED!");
              }
          });
          });
          }
          function viewdoms(ztype, zoneid){
              var prompt = require('prompt');
              prompt.start();
              prompt.get(['zone_type', 'zone_id'], function (err, result) {
                         var ztype = result.zone_type;
                         var zoneid = result.zone_id;
    
                  api.get('zones/'+ ztype +'/'+ zoneid +'/customdomains.json', function(err, results) {
                  if (err) {
                             console.trace(err);
                             return;
                  }
                  else{
                      var res = results.data.customdomains;
                      for(var data in res){
                      console.log("Custom Domain ID: " + res[data]['id'] + "\n");
                      console.log("Custom Domain: " + res[data]['custom_domain'] + "\n-------------------\n");
                  }
              }
          });
          });
          }
          }
          
  10. Preview/Manage zone details
    Methods used: GET, POST.

          //***************** zone details ***********************//
          function zonedetails(){
          var prompt = require('prompt');
          prompt.start();
          prompt.get(['zoneid'], function (err, result) {
              var zoneid = result.zoneid;
              view(zoneid);
          });
          function view(zoneid){
          var prompt = require('prompt');
          prompt.start();
          console.log("Zone Details: ");
          prompt.get(['zonetype'], function (err, result){
              var zone_type = result.zonetype;
          api.get('zones/'+ zone_type +'.json/'+ zoneid, function(err, results) {
                  if (err) {
                  console.trace(err);
                  return;
                  }
              var lbl = results.data.pullzone.label;
              var gzip= results.data.pullzone.compression;
              var cookie = results.data.pullzone.ignore_setcookie_header;
              var download = results.data.pullzone.content_disposition;
              var pseudo = results.data.pullzone.pseudo_streaming;
              console.log("Creation Date: " + results.data.pullzone.creation_date);
              console.log("Zone Name: " + results.data.pullzone.name);
              console.log("Alias: " + results.data.pullzone.username);
              console.log("Label: " + results.data.pullzone.label);
              console.log("Gzip status: " + results.data.pullzone.compress);
              console.log("Ignore cookies: " + results.data.pullzone.ignore_setcookie_header);
              console.log("Force files download: " + results.data.pullzone.content_disposition);
              console.log("Pseudo streaming: " + results.data.pullzone.pseudo_streaming);
              edit(zoneid, lbl, gzip, cookie, download, pseudo);
          });
          });
          }
          function edit(zoneid, lbl, gzip, cookie, download, pseudo){
              var prompt = require('prompt');
              prompt.start();
              console.log("Setting to edit (choose number next to it): 1. Label, 2. GZIP, 3. Ignore Cookies, 4. Force download, 5. Pseudo streaming");
              prompt.get(['option'], function (err, result) {
                  var option = result.option;
                  switch(option){
                      case '1':
                      setedit("1", zoneid, lbl, gzip, cookie, download, pseudo);
                      break;
                      case '2':
                      setedit("2", zoneid, lbl, gzip, cookie, download, pseudo);
                      break;
                      case '3':
                      setedit("3", zoneid, lbl, gzip, cookie, download, pseudo);
                      break;
                      case '4':
                                  setedit("4", zoneid, lbl, gzip, cookie, download, pseudo);
                                  break;
                      case '5':
                                  setedit("5", zoneid, lbl, gzip, cookie, download, pseudo);
                                  break;
                  }
              });
          }
          function setedit(option, zoneid, lbl, gzip, cookie, download, pseudo){
          console.log("New Value: ");
                                  prompt.start();
                                  prompt.get(['value'], function (err, result) {
                          switch(option){
                              case '1':
                              lbl = result.value;
                              break;
                              case '2':
                              gzip = result.value;
                              break;
                              case '3':
                                                  cookie = result.value;
                                                  break;
                              case '4':
                                                  download = result.value;
                                                  break;
                              case '5':
                                                  pseudo = result.value;
                                                  break;
                          }
                                          var value = result.value;
                                          api.put('/zones/pull.json/' + zoneid, {label: lbl, compress: gzip, ignore_setcookie_header: cookie, content_disposition: download, pseudo_streaming: pseudo}, function(err, response) {
                                                  if (err) {
                                      console.trace(err);
                                      return;
                                  }
                              else{
                              console.log("ALL DONE!");
                              }
                                          });
    
                                  });
          }
          }