Skip to content

JavaScript SDK for interacting with the put.io API.

License

Notifications You must be signed in to change notification settings

putdotio/putio-js

Repository files navigation

putio-js

JavaScript SDK for interacting with the put.io API.

Build Status Coverage Status npm (scoped) npm bundle size (scoped) GitHub

Installation

yarn add @putdotio/api-client

npm install @putdotio/api-client

ES Modules / TypeScript

import PutioAPI from '@putdotio/api-client'

CommonJS

const PutioAPI = require('@putdotio/api-client').default

Usage

// you can pass the options in constructor
const putioAPI = new PutioAPI({ clientID: 'OAUTH_CLIENT_ID' })

// or use `configure` method
MyApp.bootstrap(config => {
  putioAPI.configure({ clientID: config.OAUTH_CLIENT_ID })
})

// setToken will send the given auth token with every request, in Authorization header
MyApp.onLogin(token => {
  putioAPI.setToken(token)

  putioAPI.Account.Info()
    .then(r => console.log('Fetched user info: ', r))
    .catch(e => console.log('An error occurred while fetching user info: ', e))
})

// clearToken will perform a clean-up and stop sending the token in Authorization header
MyApp.onLogout(() => {
  putioAPI.clearToken()
})

API

Options

Prop Type Default Value Description
clientID number 1 OAuth app client ID, defaults to put.io web app
baseURL string api.put.io/v2 Base URL of the API
webAppURL string app.put.io Base URL of the Put.io web app, used in the authentication flow

Methods

Name Parameters Return Value
configure (options: IPutioAPIClientOptions) PutioAPIClient Instance
setToken (token: string) PutioAPIClient Instance
clearToken PutioAPIClient Instance

Events

Value Payload Description
ERROR IPutioAPIClientError Fired when an HTTP request fails
CLIENT_IP_CHANGED { IP: string, newIP: string } Fired when the IP of the client changes