Using WebFaction’s API with Node.js

WebFaction has an API you can use to script GUI commands for things like creating application instances, configuring domains, and setting up mailboxes. To interact with the API using Node.js you just need an XML-RPC library. This one by Brandon Alexander is great.

To make working with the API a little easier I started working on a wrapper, which you can install as an npm module:

npm install webfaction-api

Note: you don’t have to install the XML-RPC library; it’s included within the webfaction-api module.

Here’s a basic usage example:

var Webfaction = require('webfaction-api');

var webfaction = new Webfaction('username', 'password');

webfaction.login(function(result) {

  webfaction.listApps(function(result) {
    console.log(result);
  });

});

Here’s a more practical example:

var Webfaction = require('webfaction-api');

var webfaction = new Webfaction('username', 'password');

webfaction.login(function(result) {

  webfaction.createWebsite({
    website_name: 'example',
    ip: '123.45.67.89',
    https: false,
    subdomains: ['www.example.com'],
    site_apps: [ ['node', '/'] ]
    },
    function(result) {
      console.log(result);
    }
  );

});

The wrapper is still a work in progress. You can find the list of currently supported API calls on the github page. If you find this useful, discover bugs, or would like to contribute to the project, please get in touch on Twitter or Github.