Javscript wrapper for the Infusionsoft XML-RPC API
npm install infusionsoft-javascript-apiA promise-driven, fluent-style Node.js wrapper for the XML-RPC Infusionsoft API.
Extended from Bvalosek's awesome but largely abandoned Infusionsoft API
Updated to work from the current Oauth token system, not the legacy App Id / Key pair.
Install via npm:
```
$ npm install infusionsoft-javascript-api
Set it up by providing a valid Oauth token to instantiate DataContext:
`javascript
var api = require('infusionsoft-api');
var infusionsoft = new api.DataContext('VALID_AUTH_TOKEN');
`
Then, work your Infusionsoft magic:
`javascript `
infusionsoft.Contacts
.where(Contact.FirstName, 'Brandon')
.like(Contact.LastName, 'V%')
.select(Contact.Id, Contact.Email)
.orderByDescending('LastName')
.take(100)
.toArray()
.done(function(result) {
console.log(result);
});
You can also use API Services directly:
`javascript`
infusionsoft.ContactService
.findByEmail('brandon@aol.com', ['Id', 'FirstName', 'LastName']);
Very cool.
All asynchronous methods return a Promise
that represents the eventual value that will be returned.
Promises are glorious and make writing heavily asynchronous code much less
awful than it would otherwise be.
See the More Examples section to see them in action.
All examples use infusionsoft as an instantiated DataContext with your Oauth Token. ie:
`javascript`
var infusionsoft = new api.DataContext('MY_OAUTH_TOKEN');
`javascript`
infusionsoft.Payments
.like(Payment.PayDate, '2013-06%')
.sum(function(x) { return x.PayAmt; })
.done(function(total) {
console.log('total revenue: ' + total);
});
And an example of using the fail method to catch any problems.
`javascript`
infusionsoft.DataService
.authenticateUser('user@email.com', 'md5-hash-of-password')
.then(function(userId) {
return infusionsoft.Users.where(User.Id, userId).first();
})
.then(function(user) {
console.log('Hello ' + user.FirstName + ' ' + user.LastName);
})
.fail(function(err) {
console.log('uh oh: ' + err);
});
Uses underscore.
`javascript`
infusionsoft.Invoices
.like(Invoice.DateCreated, '2013-08%')
.groupBy(function(x) { return x.ProductSold; })
.done(function(result) {
_(result).each(function(invoices, productId) {
console.log(productId, invoices.length);
});
});
Same as above, but use the spread function to wait on 2 promises to get theProduct
corresponding product names. The API hits for querying both the tableInvoice
and the table will actually fire off at the same time.
Hashtag asynchronous.
`javascript
var products = infusionsoft.Products.toArray();
var invoices = infusionsoft.Invoices
.like(Invoice.DateCreated, '2013-08%')
.groupBy(function(x) { return x.ProductSold; });
Q.spread([products, invoices], function(products, invoices) {
_(invoices).each(function(invoices, productId) {
var productName = _(products)
.find(function(x) { return x.Id == productId; })
.ProductName;
console.log(productName, invoices.length);
});
});
`
`javascript`
sdk.Contacts
.where(Contact.Email, 'some@email.com')
.first()
.then(function(contact) {
return sdk.ContactGroupAssigns
.where(ContactGroupAssign.ContactId, contact.Id)
.toArray();
})
.then(function(cgas) {
cgas.forEach(function(group) {
console.log(group.ContactGroup, group.DateCreated);
});
});
Okay, take a deep breath. We can do (inner) joins. We fake it though... the
inner part of the join has to be loaded entirely and then we do a O(n^2)inner
iteration to make it, but we can still do it. If the is cheap, this
isn't too bad. Especially when the SDK will handle loading, paging, waiting,
etc... all for you.
Syntax (stolen from C#'s LINQ):
Let's do this:
`javascript
var pc = infusionsoft.ProductCategories;
var pca = infusionsoft.ProductCategoryAssigns;
var plans = infusionsoft.SubscriptionPlans;
// Join the categories onto itself for creating the full category name
// (category parent name + category name)
var categories = pc
.join(pc, 'ParentId', 'Id', function(pc, parent) {
return {
Id: pc.Id,
Name: parent.CategoryDisplayName + ' ' + pc.CategoryDisplayName
}; });
var subPlans = plans
// Join the sub plan (which only has product Id) onto the PCA table to get
// the product category ID
.join(pca, 'ProductId', 'ProductId', function(plan, pca) {
plan.ProductCategoryId = pca.ProductCategoryId;
return plan;
})
// Join our categories object we made above onto the projection from the
// most recent join to get the full category name + subscription plan Id
.join(categories, 'ProductCategoryId', 'Id', function(plan, category) {
return { planId: plan.Id, category: category.Name }; });
subPlans.toArray().done(function(d) { console.log(d); });
`
What happens magically behind the scenes is pretty nice. When we call
toArray() at the end, we first query the SubscriptionPlan table (aliased asplans). It then knows we need to join the ProductCategoryAssign table onProductCategory
there, so it fetches that (which may be more than one page). It finally gets
the table (in its entirety), and joins them all up.
The syntax looks nasty, but that is somewhat unavoidable with a join`
function.
Infusionsoft Javascript API is released under the MIT license.