REM is a minimal library to consume REST APIs, constructed around simple language idioms and manifests for popular libraries that alleviate differences between between configuration, authentication, and formats. Getting started with REM is quick and easy.
To use REM with Node.js, install using npm
:
npm install rem
Examples are provided for each predefined API. These examples can be run from the command line:
coffee examples/dropbox
For authentication, create a file called examples/keys.json
with your
API keys and secrets, of the following format:
{"tumblr": {"key": "KEY", "secret": "SECRET"}, "twitter": ...}
REM includes configurations for several popular APIs. Getting started with a particular API is as simple is specifying the name and API version:
var rem = require('rem')
var tw = rem.load('twitter', 1, {key: 'YOUR_API_KEY', secret: 'YOUR_INNERMOST_API_SECRET'})
// Get started with version 1 of the Twitter API
You can make API requests simply:
tw('search').get({q: 'fleetwood mac', rpp: 5}, function(err, json) {
console.log('There are', json.results.length, 'results for Fleetwood Mac. #awesome');
});
OAuth authentication parameters are already included. You can authenticate by using callbacks, connect middleware, or out-of-band modes when available:
var read = require('read');
var oauth = rem.oauth(tw);
tw.start(function (url, token, secret) {
console.log("Visit:", url);
read({prompt: "Verification code: "}, function (err, verifier) {
oauth.complete(verifier, token, secret, function (err, user) {
// Authenticated calls with the Twitter API can now be made:
user('statuses/update').post({status: message}, function (err, json) {
console.log('Posted a comment:', err, json);
})
})
})
})
You can also define and load your own manifests for your own APIs:
var api = rem.create({base: 'http://cozy.api/v1', ...}, {format: 'json'})
-
Load a predefined manifest. The available list exists in the
rem
repo. A version parameter is required for each API. Theoptions
object is a map of values that configure the API, most commonlykey
andsecret
. You can also specify aformat
parameter with the value"xml"
or"json"
, which determines what format to use for REST calls. When a format is not specified,"json"
is assumed. -
Create a REM
Api
object using the JSON manifest you supply. The format of manifests is defined in therem
repo.
The Api
object is callable:
-
Returns a route object for the given path, and if specified, query parameters. These parameters can be augmented by method calls, for instance
api('/some/path', {"key1": "A"}).get({"key2": "B"}, function () { ... })
uses bothkey1
andkey2
.
All route methods perform a REST call. Each takes a callback parameter (which can be omitted, for instance to just return the ClientRequest object). The callback receives an err
argument, a data
object (which will be a JSON object or a libxmljs
document), and a RESTCall
object with additional methods and properties.
-
Performs a GET request on the given route. Additional query parameters can be specified.
-
Performs a POST request on the given route with the
data
argument, which can be a string, buffer, or object. Themime
argument can be a MIME type or one ofform
orjson
. If themime
argument is omitted, the value is eitheruploadFormat
as defined in the manifest, orform
by default. If an object is passed as thedata
argument, it will be serialized as either form data or JSON, depending on the MIME type. -
Performs a PUT request on the given route. See
route.post()
-
Performs a HEAD request on the given route. The
data
argument in the callback will be empty. (TheClientResponse
is saved in thecall.res
parameter.) -
Performs a DELETE request on the given route.
TODO. See examples/server-oauth1.coffee
and examples/server-oauth2.coffee
.
MIT.