Introducing the Official Zencoder Module for Node.js

We're proud to announce the release of an officially supported Zencoder integration library for Node.js. If you have used Zencoder with Node.js before, there's a good chance you've used Ryan Faerman's integration library, which was listed on our website as our recommended third-party Node package. Not only did he make and support the original Node library for a year, he was kind enough to transfer ownership of the NPM module to us when we decided to release our own.

Getting Started

First you'll need to install the NPM module.
npm install zencoder
This will install the package in the directory node_modules. You can then require the package in your application.
var Zencoder = require('zencoder');
Now instantiate a new client.
var client = new Zencoder('abcdefg1234qwert9302sdfjki30kfi9');
This will create a new client using the API key abcdefg1234qwert9302sdfjki30kfi9. If no API key is set, then the library will check for a ZENCODER_API_KEY environment variable. By default, the library uses v2 of the Zencoder API, but you can pass in a different base url when creating a client if need be.
var client = new Zencoder('abcdefg1234qwert9302sdfjki30kfi9', 'https://app.zencoder.com/api/v1')
Now you're ready to start interacting with the API! The library follows RESTful conventions as closely as possible, so if you're familiar with our API already you can hit the ground running. Let's create a basic application that will submit a job for us, then return the job ID if successful. Start by creating a new app called zentest.js and set up a new client as we detailed above, then use that client to submit a job.

var Zencoder = require('zencoder');

var client = new Zencoder('abcdefg1234qwert9302sdfjki30kfi9');

client.Job.create({
  input: 'http://s3.amazonaws.com/zencodertesting/test.mov',
  outputs: [{
    url: 's3://zen-tests/awesome-movie.mp4'
  }]
}, function(err, data){
  if (err) { console.log("OH NO! There was an error"); return err; }
  console.log('Job created!\nJob ID: ' + data.id);
});
Save the file and run it using $ node zentest.js. You should see output like this:
$ node zentest.js
Job created!
Job ID: 1234567
Now instead of just logging the creation, let's change our app to poll for progress after a successful request so we can get progress updates. We'll use a poll function and timeouts to query the API for progress every 5 seconds until the job is finished.

var Zencoder = require('./')
  , readline = require('readline');

var client = new Zencoder('abcdefg1234qwert9302sdfjki30kfi9');

// We'll use readline to update progress so we don't get have to see a bajillion console.logs
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

client.Job.create({
  input: 'http://s3.amazonaws.com/zencodertesting/test.mov',
  outputs: [{
    url: 's3://zen-tests/awesome-movie.mp4'
  }]
}, function(err, data){
  if (err) { console.log("OH NO! There was an error"); return err; }
  console.log('Job created!\nJob ID: ' + data.id);
  poll(data.id); // start polling... polling will continue to call itself until finished.
});

function poll(id) {
  setTimeout(function(){
    client.Job.progress(id, function(err, data) {
      if (err) { console.log("OH NO! There was an error"); return err; } // blargh!
      if (data.state == 'waiting') {
        if (!this.status || this.status != 'waiting') {
          rl.write('Waiting'); // display waiting
          this.status = 'waiting'; // set status to waiting so we can start adding dots.
        } else {
          rl.write('.'); // keep adding '.' until we start processing
        }
        poll(id);
      } else if (data.state == 'processing') {
        var progress = Math.round(data.progress * 100) / 100; // round to nearest decimal places.
        rl.write(null, {ctrl: true, name: 'u'}); // clear the current status so we can update progress
        rl.write('Processing: ' + progress + '%');
        this.status = 'processing'; // not important, but makes sure we don't display waiting again
        poll(id);
      } else if (data.state == 'finished') {
        rl.write(null, {ctrl: true, name: 'u'}); // clear the current status
        console.log('Job finished!'); // finished!
        process.exit(0); // exit
      }
    }, 5000);
  })
}
Now run $ node zentest.js again. You should see a notice that the job was created, followed by an updated percentage until the job is complete. In a web application, you would most likely query for progress in the client, and handle job completion with a notification on the server. We'll go into more detail on using notifications in a series on building applications around Zencoder with Node. The GitHub repository includes a comprehensive test suite, so if you'd like to see more usage examples that's a great place to start. If you have any other questions or comments, feel free to reach out! @matt_mcclure