Consume Volusion eStore API Using Meteor JS
Tags: code, fyi, howto
Published: 2018-04-08 18:23:44

---
title: Consume Volusion eStore API Using Meteor JS
published: true
tags: Code,FYI,How To
canonical_url: 
---

The \server\main.js in this example performs an API call to your Volusion eStore, converts the response to JSON and inserts to MongoDB.

The \client\main.js makes the call and displays the data from MongoDB.

> meteor create volusionapi
> 
> cd volusionapi
> 
> meteor add http peerlibrary:xml2js

Copy these files [https://github.com/cjfleischhacker/MeteorVolusionAPI.git](https://github.com/cjfleischhacker/MeteorVolusionAPI.git) or create these files:

**\client\main.html**  
`
<head>
<title>volusion</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> vapi}}
</body>
<template name="vapi">
<h3>List Courses</h3>
<p style="color: #ff0000; margin: 10px 0 10px 0;">
{{errMessage}}
</p>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Code</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{{#each courses}}
<tr>
<td>{{ProductID}}</td>
<td>{{ProductCode}}</td>
<td>{{ProductName}}</td>
</tr>
{{/each}}
</tbody>
</table>
</template>
`

**\client\main.js**  
`
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
Courses = new Mongo.Collection('courses');`

Meteor.call(‘getJSONFromAPI’, function (err, res) {  
if (err) {  
console.log(err);  
} else {  
console.log(res.xmldata.Products);  
}  
});

Template.vapi.onCreated(function helloOnCreated() {  
console.log(‘oncreate…’);  
});

Template.vapi.helpers({  
courses: function () {  
return Courses.find();  
}  
});

**\server\main.js**  
`
import { Meteor } from 'meteor/meteor';
import { xml2js } from 'meteor/peerlibrary:xml2js';
Courses = new Mongo.Collection('courses');`

Meteor.methods({  
‘getJSONFromAPI’: function () {  
this.unblock();  
var apiUrl = ‘[http://store.volusion.com/net/WebService.aspx?Login=email@mystore.com&EncryptedPassword=YourVolusionAPIEncryptedPassword&EDI\_Name=Generic](http://store.volusion.com/net/WebService.aspx?Login=email@mystore.com&EncryptedPassword=YourVolusionAPIEncryptedPassword&EDI_Name=Generic)\\Products&SELECT\_Columns=p.ProductCode,p.ProductName,pd.ProductDescription’

;  
var response = Meteor.wrapAsync(apiCall)(apiUrl);  
const result = xml2js.parseStringSync(response, { explicitArray: false, emptyTag: undefined });  
for (var i = 0; i \< result.xmldata.Products.length; i++) {  
Courses.insert(result.xmldata.Products[i]);  
}  
return result;  
}  
});

var apiCall = function (apiUrl, callback) {  
// try…catch allows you to handle errors  
try {  
var response = HTTP.get(apiUrl).content;  
// A successful API call returns no error  
callback(null, response);  
} catch (error) {  
// If the API responded with an error message and a payload  
if (error.response) {  
var errorCode = error.response.data.code;  
var errorMessage = error.response.data.message;  
// Otherwise use a generic error message  
} else {  
var errorCode = 500;  
var errorMessage = ‘Cannot access the API’;  
}  
// Create an Error object and return it via callback  
var myError = new Meteor.Error(errorCode, errorMessage);  
callback(myError, null);  
}  
}

> meteor