What is the bcapi.js SDK?

This SDK provides a collection of methods you can use to easily interact with the API system. It offers the same functionality as the API endpoints but it is easier to work with.

Using the bcapi.js SDK

In order to call the bcapi.js methods you need to make sure the bcapi.js and its dependencies are all linked from your pages:

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<script type="text/javascript" src="//cdn.worldsecuresystems.com/bcapi/bcapi-0.1.0.min.js"></script>

See the "Hello World" app landing page for a complete example.

Example API requests using the SDK

Before using the SDK, you will need to install an app (any app) on your site. Just as in the case of regular API calls you will need the App Loader to generate the access token.

Install an app

To quickly get you started you can install the "Hello World" app.

Once the app is installed click its menu entry in the Admin console to open up the app and then hit F12 to open up Chrome Developer Tools.

Switch across to the "Sources" tab, open up the index.html landing page and add a break like so:

This break will pause the script and will allow us to run the test API calls while in context of the app.

After adding the break click the app's Open Admin menu item again. This should fire it again and this time it will stop at the break we added.

At this point you can test any of the SDK classes available as well as  regular API calls available, let's take a look at a few of them.

1. Get a list of Web Apps that exist on the current site

var apps = new BCAPI.Models.WebApp.AppCollection();
var response = apps.fetch();
response.done(function (webApps) { 
//Do something in case of success
response.fail(function () { 
//Do something in case of error

2. Create a file on the site, then delete it

Creating the file

var f = BCAPI.Models.FileSystem.Root.file('hello_world.txt');
var data = 'Hello World !';
f.upload(data).done(function () {
    console.log('File uploaded succesfully');

Deleting the file

var f = BCAPI.Models.FileSystem.Root.file('hello_world.txt');
f.destroy().done(function() {
    console.log('File was destroyed');

Here is a list of all the classes you can access via the bcapi.js SDK