API Integration: C Sharp (C#) Examples (Developer)

Volusion API Reader

Volusion API Reader is an example program created in C# in order to show how to utilize the import / export capabilities of the API.

This is a complete visual studio solution and includes complete source code and binaries. The program consists of two parts:

Export

Read from the Volusion API and display the data or continue to export it into another format or custom application.

Import

Import data from an external program into your Volusion store.

Data Format

Volusion API uses XML based format to import and export data. The XSD (XML Schema Definition) files can be found on this WIKI. In order to quickly load data from and into the Volusion server, we have converted the XSD file into a CS file using xsd.exe. More information on this tool can be found here: xsd.exe on MSDN

Reading Data

Reading data from The API can be done by simply loading the XML into a document:

XmlDocument APIDoc = null;
XmlTextReader reader = new XmlTextReader(APIURL);
APIDoc = new XmlDocument();
APIDoc.Load(reader);
reader.Close();

Writing Data

In order to write data into the API, we are utilizing the objects we built using the provided XSD files:

xmldata data = new xmldata();
data.Items = new xmldataProducts[1];
data.Items[0] = new xmldataProducts();
data.Items[0].ProductName = productName;
data.Items[0].ProductCode = productCode;
data.Items[0].ProductPrice = productPrice;

Since the Volusion API expects an XML-formatted message, we need to turn the object into XML:

StringBuilder productSb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(productSb);
XmlSerializer serializer = new XmlSerializer(typeof(DataObjects.xmldata));
serializer.Serialize(writer, product);

Then post the data into our server:

HttpWebRequest APIRequest = (HttpWebRequest)WebRequest.Create(APIURL);
APIRequest.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
APIRequest.ContentType = "application/x-www-form-urlencoded";
APIRequest.ContentLength = data.Length;

using (Stream APIStream = APIRequest.GetRequestStream())
{
    APIStream.Write(data, 0, data.Length);
}

How to Run the Program

  1. Open VAPIReader.sln using Visual Studio 2005.
  2. Before running the solution, you'll need to configure your store's URL, user name and encrypted password. In Visual Studio, use the Solution Explorer to navigate to VAPIReader/app.config and modify the following values to match the values in your account:
    <add key="VOLUSION_BASE_API" value="http://www.yourdomain.com/net/WebService.aspx"/>
    <add key="VOLUSION_USER" value="YourLogin"/>
    <add key="VOLUSION_PASSWORD" value="YourEncryptedPassword"/>
    
  3. Press F5 in order to run the program.

Download Solution file here: VAPIReader.zip

Volusion API Format Flex

Volusion API Format Flex is an example program created in C# that shows how to utilize the export capabilities of the API to different XML structure. This is a complete Visual Studio solution, and includes complete source code and binaries.

The concept behind this program is to simplify the modification of exported files. It includes a working example that converts Volusion orders data into UPS data, to be used for shipping orders.

It also includes a blank UPS XML file and a map of replacement values. The map indicates which values in the Volusion order will be stored in a UPS shipping order.

Exporting Data from the API

This is done by reading the Orders API and storing the data as in memory XML:

XmlTextReader reader = new XmlTextReader(APIURL);
APIDoc = new XmlDocument();
APIDoc.Load(reader);
reader.Close();

Then, load the UPS map or any custom map you've added to Mapper.xml:

Mapper mapper = new Mapper(mapName);

Once you have your order, load a blank UPS order and simply fill in the values from the Volusion order.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(destinationDoc.NameTable);
sourceNode.SelectSingleNode(mapper.TagDictionary[key], nsmgr).InnerText = destinationNode.SelectSingleNode(key).InnerText;

This is a very generic replace method, so any special replacement values will need to be added for each order.

loadTo.SelectSingleNode("//ups:Attention", nsmgr).InnerText = loadFrom.SelectSingleNode("ShipFirstName").InnerText +
" " + loadFrom.SelectSingleNode("ShipLastName").InnerText;

Finally, we can output the result into a file in order to upload it to UPS.

fileStream = mainSaveFileDialog.OpenFile();
ASCIIEncoding encoder = new ASCIIEncoding();
xmlData = GetFormatedXML(stateInfo.ToString());
byte[] data = encoder.GetBytes(xmlData);
fileStream.Write(data, 0, data.Length);
fileStream.Close();

Running the Program

Open the file VAPIShape.sln using Visual Studio 2005.

  • Press F5 to sun the solution. A settings screen will appear, prompting you to enter your store’s settings.
  • Enter your API URL, user name, and encrypted password, then press Save.
  • In the main window, press the Export button in order to select a file to save the data to.
  • Press Save on the dialog in order to start the exporting process.

The program will connect to the Volusion API server and retrieve all of your new orders. The data will be saved to the selected file, in the format required by UPS.

Download solution file here: VAPIShape.zip