Skip to content

Repository files navigation

Onspring API Javascript SDK

lint_format_testcodecovbuild_publishsemantic-release: angularNPMnpmnpm

The Javascript SDK for the Onspring API is meant to simplify development in Javascript for Onspring customers who want to build integrations with their Onspring instance.

Note: This is an unofficial SDK for the Onspring API. It was not built in consultation with Onspring Technologies LLC or a member of their development team.

This SDK was developed independently using Onspring's existing C# SDK, the Onspring API's swagger page, and api documentation as the starting point with the intention of making development of integrations done in Javascript with an Onspring instance quicker and more convenient.

🛠️ Dependencies

Node.js

node-current

Requires use of Node.js 14.x or later.

Axios

npm (prod) dependency version

All methods for the OnspringClient make use of the Axios http client to interact with the Onspring API.

Form-Data

npm (prod) dependency version

When it is necessary to send requests to the Onspring API using multi-part/form-data the Form-Data package is used.

💾 Installation

Install the SDK using npm:

npm install onspring-api-sdk

🔑 API Key

In order to successfully interact with the Onspring Api you will need an API key. API keys are obtained by an Onspring user with permissions to at least Read API Keys for your instance via the following steps:

  1. Login to the Onspring instance.
  2. Navigate to Administration > Security > API Keys
  3. On the list page, add a new API Key - this will require Create permissions - or click an existing API key to view its details.
  4. Click on the Developer Information tab.
  5. Copy the X-ApiKey Header value from this tab.

Important:

  • An API Key must have a status of Enabled in order to make authorized requests.
  • Each API Key must have an assigned Role. This role controls the permissions for requests made. If the API Key used does not have sufficient permissions the requests made won't be successful.

🔒 Permission Considerations

You can think of any API Key as another user in your Onspring instance and therefore it is subject to all the same permission considerations as any other user when it comes to its ability to access data in your instance. The API Key you use needs to have all the correct permissions within your instance to access the data requested. Things to think about in this context are role security, content security, and field security.

🧑🏻‍💻 Start Coding

OnspringClient

The most common way to use the SDK is to create an OnspringClient instance and call its methods. Its constructor requires two parameters:

  • baseUrl - currently this should always be: https://api.onspring.com
  • apiKey - the value obtained by following the steps in the API Key section

It is best practice to read these values in from a configuration file for both flexibility and security purposes.

Example .env file:

API_KEY=000000ffffff000000ffffff/00000000-ffff-0000-ffff-000000000000BASE_URL=https://api.onspring.com

Example constructing OnspringClient:

CommonJS

const{ OnspringClient }=require('onspring-api-sdk');constdotenv=require('dotenv');dotenv.config();constclient=newOnspringClient(process.env.BASE_URL,process.env.API_KEY);

ES Module

importdotenvfrom'dotenv';import{OnspringClient}from'onspring-api-sdk';dotenv.config();constclient=newOnspringClient(process.env.BASE_URL,process.env.API_KEY);

Axios Instance Configuration

By default when you construct an instance of the OnspringClient a new Axios instance will also be created. Its baseURL property will always be set to the baseUrl parameter based to the OnspringClient's constructor and its headers will always contain the proper x-api-key header.

You can though pass a third optional argument to the OnspringClient constructor that specifies additional configuration options for the Axios instance used to make requests.

importdotenvfrom'dotenv';import{OnspringClient}from'onspring-api-sdk';dotenv.config();constconfigs={timeout: 5000,};constclient=newOnspringClient(process.env.BASE_URL,process.env.API_KEY,configs);

ApiResponse

Each OnspringClient method - aside from canConnect - returns an ApiResponse object which will have the following properties:

  • statusCode - The http status code of the response.
  • isSuccessful - Indicates whether the request succeeded.
  • data - If the request was successful will contain the response data deserialized to custom classes.
  • message - A message that may provide more detail about the request when no successful

CommonJS or ES Modules

There is support for using either CommonJS or ES Modules depending upon your preference. This documentation will use the later in the usage examples.

Types

The package is written in typescript and all types are exported and available for you to use.

Full API Documentation

You may wish to refer to the full Onspring API documentation when determining which values to pass as parameters to some of the OnspringClient methods. There is also a swagger page that you can use for making exploratory requests.

📋 Examples

Note the following code snippets assume you've already instantiated an OnspringClient as shown in the OnspringClient section.

Connectivity

Verify connectivity

constres=awaitclient.canConnect();console.log(res);// true or false

Apps

Get Apps

Returns a paged collection of apps and/or surveys that can be paged through. By default the page size is 50 and page number is 1.

constres=awaitclient.getApps();constapps=res.data.items;for(constappofapps){console.log(app);}

You can set your own page size and page number (max is 1,000) as well.

import{PagingRequest}from'onspring-api-sdk';constres=awaitclient.getApps(newPagingRequest(1,1));constapps=res.data.items;for(constappofapps){console.log(app);}

Get App By Id

Returns an Onspring app or survey according to provided id.

constres=awaitclient.getAppById(130);constapp=res.data;console.log(app);

Get Apps By Ids

Returns a collection of Onspring apps and/or surveys according to provided ids.

constres=awaitclient.getAppsByIds([130,131]);constapps=res.data.items;for(constappofapps){console.log(app);}

Fields

Get Field By Id

Returns an Onspring field according to provided id.

constres=awaitclient.getFieldById(4793);constfield=res.data;console.log(field);

Get Fields By Ids

Returns a collection of Onspring fields according to provided ids.

constres=awaitclient.getFieldsByIds([4793,4801]);constfields=res.data.items;for(constfieldoffields){console.log(field);}

Get Fields By App Id

Returns a paged collection of fields that can be paged through. By default the page size is 50 and page number is 1.

constres=awaitclient.getFieldsByAppId(132);constfields=res.data.items;for(constfieldoffields){console.log(field);}

You can set your own page size and page number (max is 1,000) as well.

import{PagingRequest}from'onspring-api-sdk';constres=awaitclient.getFieldsByAppId(132,newPagingRequest(1,1));constfields=res.data.items;for(constfieldoffields){console.log(field);}

Files

Get File Info By Id

Returns the Onspring file's metadata.

constres=awaitclient.getFileInfoById(1,4806,909);constfileInfo=res.data;console.log(fileInfo);

Get File By Id

Returns the file itself.

importfsfrom'fs';constres=awaitclient.getFileById(1,4806,909);constfile=res.data;console.log(file.contentLength);console.log(file.contentType);console.log(file.fileName);file.stream.pipe(fs.createWriteStream(file.fileName));

Save File

importfsfrom'fs';import{SaveFileRequest}from'onspring-api-sdk';constrequest=newSaveFileRequest(1,4806,'notes',newDate(),'test-attachment.txt','text/plain',fs.createReadStream('test-attachment.txt'));constres=awaitclient.saveFile(request);constfileId=res.data.id;console.log(fileId);

Delete File By Id

constres=awaitclient.deleteFileById(1,4806,1505);res.statusCode===204
? console.log('File deleted')
: console.log('Error deleting file');

Lists

Add Or Update List Value

To add a list value don't provide an id value.

import{ListItemRequest}from'onspring-api-sdk';constrequest=newListItemRequest(638,null,'New Value',1,'#000000');constres=awaitclient.addOrUpdateListItem(request);constitemId=res.data.id;console.log(itemId);

To update a list value provide an id value.

import{ListItemRequest}from'onspring-api-sdk';constrequest=newListItemRequest(638,'35c79a46-04b8-4069-bbc1-161a175f962c','Updated Value',1,'#000000');constres=awaitclient.addOrUpdateListItem(request);constitemId=res.data.id;console.log(itemId);

Delete List Value

constres=awaitclient.deleteListItemById(638,'35c79a46-04b8-4069-bbc1-161a175f962c');res.statusCode===204
? console.log('List item deleted')
: console.log('Error deleting list item');

Records

Get Records By App Id

Returns a paged collection of records that can be paged through. By default the page size is 50 and page number is 1.

import{GetRecordsByAppIdRequest}from'onspring-api-sdk';constrequest=newGetRecordsByAppIdRequest(130);constres=awaitclient.getRecordsByAppId(request);constrecords=res.data.items;for(constrecordofrecords){console.log(record);}

You can set your own page size and page number (max is 1,000) as well. In addition to specifying what field values to return and in what format (Raw vs. Formatted) to return them.

import{DataFormat,GetRecordsByAppIdRequest,PagingRequest,}from'onspring-api-sdk';constrequest=newGetRecordsByAppIdRequest(130,[4804],DataFormat.Raw,newPagingRequest(1,1));constres=awaitclient.getRecordsByAppId(request);constrecords=res.data.items;for(constrecordofrecords){console.log(record);}

Get Record By Id

Returns an onspring record based on the provided app and record ids.

import{GetRecordRequest}from'onspring-api-sdk';constrequest=newGetRecordRequest(130,1);constres=awaitclient.getRecordById(request);constrecord=res.data;console.log(record);

You can also specify what field values to return and in what format (Raw vs. Formatted) to return them.

import{DataFormat,GetRecordRequest}from'onspring-api-sdk';constrequest=newGetRecordRequest(130,1,[4804],DataFormat.Raw);constres=awaitclient.getRecordById(request);constrecord=res.data;console.log(record);

Get Records By Ids

Returns a collection of Onspring records based on the provided appId and recordIds.

import{GetRecordsRequest}from'onspring-api-sdk';constrequest=newGetRecordsRequest(130,[1]);constres=awaitclient.getRecordsByIds(request);constrecords=res.data.items;for(constrecordofrecords){console.log(record);}

You can also specify what field values to return and in what format (Raw vs. Formatted) to return them.

import{DataFormat,GetRecordsRequest}from'onspring-api-sdk';constrequest=newGetRecordsRequest(130,[1],[4804],DataFormat.Formatted);constres=awaitclient.getRecordsByIds(request);constrecords=res.data.items;for(constrecordofrecords){console.log(record);}

Query Records

Returns a paged collection of records based on a criteria that can be paged through. By default the page size is 50 and page number is 1.

import{FilterOperators,QueryFilter,QueryRecordsRequest,}from'onspring-api-sdk';constfilter=newQueryFilter(4745,FilterOperators.GreaterThan,0);constrequest=newQueryRecordsRequest(130,filter);constres=awaitclient.queryRecords(request);constrecords=res.data.items;for(constrecordofrecords){console.log(record);}

You can set your own page size and page number (max is 1,000) as well. In addition to specifying what field values to return and in what format (Raw vs. Formatted) to return them.

import{DataFormat,FilterOperators,PagingRequest,QueryFilter,QueryRecordsRequest,}from'onspring-api-sdk';constfilter=newQueryFilter(4745,FilterOperators.GreaterThan,0);constrequest=newQueryRecordsRequest(130,filter,[4804],DataFormat.Formatted,newPagingRequest(1,1));constres=awaitclient.queryRecords(request);constrecords=res.data.items;for(constrecordofrecords){console.log(record);}

For further details on constructing the filter parameter please refer to the documentation for the Onspring API.

Add or Update A Record

You can add a record by not providing a record id value. If successful will return the id of the added record.

import{Record,StringRecordValue}from'onspring-api-sdk';constrecord=newRecord(130,null);constfieldValue=newStringRecordValue(4804,'Test');record.addValue(fieldValue);constres=awaitclient.saveRecord(record);constnewRecordId=res.data.id;console.log(newRecordId);

You can update a record by providing its id. If successful will return the id of record updated.

import{Record,StringRecordValue}from'onspring-api-sdk';constrecord=newRecord(130,607);constfieldValue=newStringRecordValue(4804,'Updated');record.addValue(fieldValue);constres=awaitclient.saveRecord(record);constupdatedRecordId=res.data.id;console.log(updatedRecordId);

Delete Record By Id

Delete an individual record based upon its id.

constres=awaitclient.deleteRecordById(130,607);res.statusCode===204
? console.log('Record deleted')
: console.log('Error deleting record');

Delete Records By Ids

Delete a batch of records based upon their ids.

constres=awaitclient.deleteRecordsByIds(130,[608,609]);res.statusCode===204
? console.log('Records deleted')
: console.log('Error deleting records');

Reports

Get Report By Id

Returns the report for the provided id.

constres=awaitclient.getReportById(408);constreport=res.data;console.log(report);

You can also specify the format of the data in the report as well as whether you are requesting the report's data or its chart data.

import{DataFormat,ReportDataType}from'onspring-api-sdk';constres=awaitclient.getReportById(409,DataFormat.Formatted,ReportDataType.ChartData);constreport=res.data;console.log(report);

Get Reports By App Id

Returns a paged collection of reports that can be paged through. By default the page size is 50 and page number is 1.

constres=awaitclient.getReportsByAppId(130);constreports=res.data.items;for(constreportofreports){console.log(report);}

You can set your own page size and page number (max is 1,000) as well.

import{PagingRequest}from'onspring-api-sdk';constres=awaitclient.getReportsByAppId(130,newPagingRequest(1,1));constreports=res.data.items;for(constreportofreports){console.log(report);}

About

A JavaScript SDK for interacting with the Onspring API.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages