Voxel51 Platform API JavaScript Client Library

A JavaScript client library built on Node.js for interacting with the Voxel51 Platform.

Available at https://github.com/voxel51/api-js.

The library is implemented with ES6-style classes and uses async/await to deliver Promised-based asynchronous execution.

voxel51-logo.png

Installation

To install the library, first clone it:

git clone https://github.com/voxel51/api-js
cd api-js

and then install the package:

npm ci

Documentation

For full documentation of the Voxel51 Platform API, including usage of this client library, see the API Documentation.

To learn how to use this client library to create and run jobs that execute each of the analytics exposed on the Voxel51 Platform, see the Analytics Documentation.

User Quickstart

This section provides a brief guide to using the Platform API with your user account.

Sign-up and Authentication

To use the API, you must first create an account at https://console.voxel51.com and download an API token.

Each API request you make must be authenticated by your token. To activate your token, set the VOXEL51_API_TOKEN environment variable in your shell to point to your API token file:

export VOXEL51_API_TOKEN="/path/to/your/api-token.json"

Alternatively, you can permanently activate a token by executing the following commands:

let voxel51 = require('.');

voxel51.users.auth.activateToken('/path/to/your/api-token.json');

In the latter case, your token is copied to ~/.voxel51/ and will be automatically used in all future sessions. A token can be deactivated via the voxel51.users.auth.deactivateToken() method.

After you have activated an API token, you have full access to the API.

Creating an API Session

To initialize an API session, issue the following commands:

let voxel51 = require('.');

let api = new voxel51.users.api.API();

Analytics

List available analytics:

api.listAnalytics().then(function(analytics) {
  // Use analytics
});

Get documentation for the analytic with the given ID:

// ID of the analytic
let analyticId = 'XXXXXXXX';

api.getAnalyticDoc(analyticId).then(function(doc) {
  // Use doc
});

Data

Upload data to the cloud storage:

// Local path to the data
let dataPath = '/path/to/video.mp4';

api.uploadData(dataPath);

List uploaded data:

api.listData().then(function(data) {
  // Use data
});

Jobs

List jobs you have created:

api.listJobs().then(function(jobs) {
  // Use jobs
});

Create a job request to perform an analytic on a data, where <analytic> is the name of the analytic to run, <data-id> is the ID of the data to process, and any <param> values are set as necessary to configre the analytic:

let jobRequest = new voxel51.users.jobs.JobRequest('<analytic>');
let inputPath = voxel51.users.jobs.RemoteDataPath.fromDataId('<data-id>');
jobRequest.setInput('<input>', inputPath);
jobRequest.setParameter('<param>', val);

console.log(jobRequest.toString());

Upload a job request:

api.uploadJobRequest(jobRequest, '<job-name>');

Start a job:

// ID of the job
let jobId = 'XXXXXXXX';

api.startJob(jobId);

Wait until a job is complete and then download its output:

// Local path to which to download the output
let outputPath = '/path/to/output.zip';

api.waitUntilJobCompletes(jobId).then(function() {
  api.downloadJobOutput(jobId, outputPath).then(function() {
    console.log('Download complete!');
  });
});

Get the status of a job:

api.getJobStatus(jobId).then(function(status) {
  // Use status
});

Application Quickstart

This section provides a brief guide to using the Platform API with your application.

Sign-up and Authentication

To use the API with your application, you must first login to your application admin account at https://console.voxel51.com/admin and create an API token for your application.

Keep your application API token private; it is your access key to the API.

Each API request you make must be authenticated by your application token. To activate your application token, set the VOXEL51_APP_TOKEN environment variable in your shell to point to your API token file:

export VOXEL51_APP_TOKEN="/path/to/your/app-token.json"

Alternatively, you can permanently activate an application token by executing the following commands:

let voxel51 = require('.');

voxel51.apps.auth.activateApplicationToken('/path/to/your/app-token.json');

In the latter case, your token is copied to ~/.voxel51/ and will be automatically used in all future sessions. An application token can be deactivated via the voxel51.apps.auth.deactivateApplicationToken() method.

After you have activated an application API token, you have full access to the API.

Creating an Application API Session

To initialize an API session for your application, issue the following commands:

let voxel51 = require('.');

let api = new voxel51.apps.api.ApplicationAPI();

User Management

The application API provides methods to manage the users of your application.

For example, you can list the current users of your application:

api.listUsers().then(function(users) {
  // Use users
});

and create a new user:

api.createUser('<username>');

Performing User Actions

To perform actions for a user of your application, you must first activate the user:

// Activate an application user
api.withUser('<username>');

With a user activated, all subsequent API requests will be applied to that user. To deactivate the user, use the api.exitUser() method.

For example, you can upload data for the user:

// Local path to the data
let dataPath = '/path/to/video.mp4';

api.uploadData(dataPath);

And run a job on the user's data:

let jobRequest = new voxel51.users.jobs.JobRequest('<analytic>');
let inputPath = voxel51.users.jobs.RemoteDataPath.fromDataId('<data-id>');
jobRequest.setInput('<input>', inputPath);
jobRequest.setParameter('<param>', val);
api.uploadJobRequest(jobRequest, '<job-name>', true);

Improving Request Efficiency

A common pattern when interacting with the platform is to perform an operation to a list of data or jobs. In such cases, you can dramatically increase the efficiency of your code by taking advantage of the Promise-based nature of this library.

For example, the following code will start all unstarted jobs on the platform, using Promise.all to wait for the asynchronous requests to complete:

let voxel51 = require('.');

async function startAllJobs(api) {
  // Get jobs
  let jobsQuery = new voxel51.users.query.JobsQuery().addFields(
    ['id', 'state']);
  let result = await api.queryJobs(jobsQuery);
  let jobs = result.jobs;

  // Start all unstarted jobs
  let promises = [];
  jobs.forEach(function(job) {
    if (job.state === voxel51.users.jobs.JobState.READY) {
      promises.push(api.startJob(jod.id));
    }
  });

  return Promise.all(promises);
}

let api = new voxel51.users.api.API();

startAllJobs(api);

Generating Documentation

This project uses JSDoc to generate its documentation from source. To generate the documentation, run:

bash docs/generate_docs.bash

To view the documentation, open the docs/build/index.html file in your browser.

Copyright

Copyright 2017-2019, Voxel51, Inc.
voxel51.com