Kloudless Blog

Tutorials, case studies and how-tos from our experts

Introducing the Kloudless JavaScript SDK

At Kloudless, we value the developer experience because we ourselves are developers. Starting today, we’re taking steps to provide the most up-to-date SDKs for popular programming languages, in order to keep them consistent with our latest API improvements. We want you to have the best tools available, no matter the language, to access our Unified APIs.

We’re happy to announce our brand new JavaScript SDK, which supports both the browser and Node.js environment.

The goal of an SDK is to provides classes and utility methods to facilitate using the Kloudless API. This SDK wraps the axios library package to make API requests, and utilizes the Promise API wherever possible. In this post, we’ll briefly cover the core concepts of the SDK and share a few code examples to demo some of the functionality.

Initializing Account objects

Let’s start by navigating to the API Explorer (signing up if you haven’t) and connecting a cloud storage account.

First, copy and paste the Bearer Token from the text input box after connecting an account to initialize an Account object:

Making API Requests

Once the Account object is created, it can be used to make requests with the following methods get()put()post()patch(), and delete(). This mirrors the HTTP methods available as well. The SDK assumes that the Base URL is the Kloudless API server, so please only specify the suffix of the API endpoints for the url:

Integrating OAuth Flow

In practice, your app will require users to connect their upstream service account in order to access upstream data and make API requests on their behalf.

For example, users will need to connect their Google Drive account, so that your app can make requests to Kloudless Storage API and retrieve the users’ file and folder data.

In a browser environment, the SDK provides the helper method connectAccount() to begin the OAuth flow. The SDK prompts the user to connect account their account and creates an Account object automatically. The method requires your App ID, which can be obtained via the API Explorer as well:

For the Node.js environment, the SDK provides the full 3-legged OAuth flow helper methods utilizing frameworks like Express.js. Please check out the SDK’s README for a detailed example.

Handle API Responses

The Kloudless API returns several different HTTP responses, and the SDK has defined different objects to parse the different responses. The most common ones are Resource and ResourceList.

A Resource object represents any Object with an identifier. For example: files, folders, calendars and calendar events.

A ResourceList object, on the other hand, represents a list of resources. For example: folder contents or a list of all of the user’s calendars.

The ResourceList class provides method getNextPage()to paginate without having to supply the page IDs yourself (We will use the Calendar API to showcase how to use the ResourceList object, as the storage services we support tend to have large default page sizes):

If you would like to iterate through all resources in all pages, you can use the iterate() method:

Similar to the Account object, these resource objects have the http methods to make API requests as well. Again, you only need to specify portions after the Resource or ResourceList’s URL:

And More

That’s it for now, but this is just a brief overview of what the Kloudless JavaScript SDK can do! Visit our GitHub repository for more detailed usage guide and a full SDK reference at https://github.com/Kloudless/kloudless-js.

Published By

Andy Lee