Crystal Javascript SDK
The Crystal SDK is a library that facilitates communication with the Crystal API. This allows for applications to tailor the experience to fit the personalities of individual users. It also allows for applications to create personality-aware features themselves specifically useful to their problem domain.
Enterprise Access
Contact us at hello@crystalknows.com to get an Organization Token to utilize this library in your web application.
Installation
Currently, we have two options for installation of the SDK. NPM can be used (preferred) or it can be embedded directly into the page through the Browserify CDN
npm install --save crystal-sdk
If downloaded through NPM, a handle to the SDK can be created:
// Javascriptvar CrystalSDK = require('crystal-sdk'); // ECMAScript 6import CrystalSDK from 'crystal-sdk'
Any of these methods for importing will add CrystalSDK as a variable you can reference.
Authentication
Call this on every page before you make use of the Crystal SDK. This establishes your credentials and enables the rest of the SDK functionality.
CrystalSDK.Authentication.asOrganization("YOUR_ORG_NAME", "YOUR_ORG_TOKEN");
Email Recommendations
The Crystal SDK can be used to make email recommendations to your users. This SDK sends the sender, recipient and the email body to our API for analysis. The sender and recipient are described through Person Search objects. These objects allow for your application to enter whatever it knows about the sender and recipient, giving our API a way to attach them to an identity.
var sender = new CrystalSDK.Person.Search();sender.first_name = "Cory";sender.last_name = "Finger";sender.email = "cory@crystalknows.com"; var recipient = new CrystalSDK.Person.Search();recipient.first_name = "Drew";recipient.last_name = "D'Agostino";recipient.email = "drew@crystalknows.com";
An Email Session represents a single email being sent by your user. Your application can make multiple getRecommendation calls from this Email Session without it counting as extra towards your overall limit.
var email_session = new CrystalSDK.Email.Session(sender, recipient);var email_body = "We'll try to get to that as soon as possible!"; email_session.getRecommendations(email_body).then(function(recommendations) { recommendations.forEach(function(rec) { console.log("Recommendation for email body starts at character", rec.start); console.log("Recommendation for email body ends at character", rec.end); console.log("Description of recommendation:", rec.description); }) }).catch(function() { console.log("Trouble communicating with the API");});
Let's see the output of that:
Recommendation for email body starts at character 0
Recommendation for email body ends at character 12
Description of recommendation: When you say <b class="red">We'll try to</b>, it can make Drew uncomfortable with ambiguity...
Putting it all together
Here's a fully-formed code sample for using the Crystal SDK on a webpage
Contact us if you have any SDK suggestions or need clarification on anything!