A nodejs library for calling algorithms on Algorithmia.com with partial support for the DataAPI
The official Algorithmia nodejs client is available on NPM.
Install it for your project by adding
algorithmia to your package.json:
npm install --save algorithmia
Then instantiate an Algorithmia client using your API key:
var algorithmia = ;var client = ;
Now you are ready to call algorithms.
The following examples of calling algorithms are organized by type of input/output which vary between algorithms.
Note: a single algorithm may have different input and output types, or accept multiple types of input, so consult the algorithm's description for usage examples specific to that algorithm.
Call an algorithm with text input by passing a string into the
The returned promise will be called with the response with the Algorithm completes (or when an error occurs).
If the algorithm output is text, then the
get() method on the response will return a string.
client;// -> Hello HAL 9000
most of the time this will be an
Object or an
Null are possible).
Similarly, if the algorithm response is JSON, the
Alternatively, if you already have serialized JSON, you can call
pipeJson with the raw JSON string.
The following example makes the same API call as the previous example:
Call an algorithm with binary input by passing a
Buffer into the pipe method.
Similarly, if the algorithm response is binary data, then the
get method on the response will be a byte array.
var buffer = fs;client;
Note: while it is possible to use
response.result for text or JSON responses, in the case of a binary resonse,
result field will be base64-encoded. The
get() method is recommended
because it will return the correct type in all cases.
If an error occurs when calling an algorithm, the response will contain an error field that you can check:
The Algorithmia API exposes parameters to configure algorithm requests including support for changing the timeout of indicating that the API should include stdout in the response. Currently, the node.js client exposes these as query paremeters to the algorithm URI:
stdout=true is only supported if you have access to the algorithm source.
Working with data
The Algorithmia client also provides a way to manage both Algorithmia hosted data and data from Dropbox or S3 accounts that you've connected to you Algorithmia account.
Create directories by instantiating a
Dir object and calling
var robots = clientdir"data://.my/robots";robots;
Upload files to a directory
Upload files by calling the
putFile method a
Dir object or
put on a
var robots = clientdir"data://.my/robots";robots
You can also write to a
File using the
put method with either a
Buffer as input:
var prime = client;prime
Download content from files
Download files by calling
get on a
var robots = clientdir"data://.my/robots";// Get a text file's contents as a stringrobots;/// Get a binary file's contents as a Bufferrobots;
Delete files and directories
Delete files by calling
delete on their respective
When deleting directories, you may optionally specify a
that indicates whether or not a directory should be deleted if it contains files or other directories (default =
var c3po = client;c3po;// Force delete a directoryclientdir"data://.my/robots";
List directory contents
Iterate over the contents of a directory using the iterated returned by calling
forEachFile on a
// List top level directoriesclientdir"data://.my";// List files in the Public folder of your connected Dropbox accountclientdir"dropbox://Public";
Building the client
This project uses gulp to compile coffeescript.
npm installnpm install -g gulp-cligulp
Note: Don't edit the .js in the
lib directory; they will get overwritten on subsequent compiles.
.coffee files in the
src dir, and run