Canada Post Shipping API Integration with ColdFusion

Canada Post Shipping API Integration using ColdFusion
Today, we will discuss about how to integrate Canada Post Shipping API using ColdFusion. If you have searched for Canada Post API code, you might have already noticed that there is no SDK, open source code or a tag readily available. So, we want to help you in doing it by yourself in simple steps. Here is the code sample we give here can be extended to call other Canada Post API methods.

<!--- Step1: Initialize all the variables (Canada Post account details, shipping location, parcel dimensions, etc) --->

apiKey = "xxxxxxxxxxxxxxxx : xxxxxxxxxxxxxxxxxxx";
username = ListFirst(apiKey,':');
password = ListLast(apiKey,':');
customerNumber = "xxxxxxx";
serviceURL = "";
originPostalCode = "xxxxxx";
postalCode = "xxxxxx";
countryCode = "CA";
weight = "1";
length = "1";
width = "1";
height = "1";

<!--- Step2: Construct xml to hit the api and get shipping options and prices --->

<cfsavecontent variable="xmlRequest">
<?xml version="1.0" encoding="utf-8"?>
<mailing-scenario xmlns="">
<length>#NumberFormat(length, ".9")#</length>
<width>#NumberFormat(width, ".9")#</width>
<height>#NumberFormat(height, ".9")#</height>
<weight>#NumberFormat(weight, ".9")#</weight>
<cfif countryCode EQ 'CA'>
<cfelseif countryCode EQ 'us'>

<!--- Step3: Post request to Canada Post API --->

username="#username#" password="#password#">
<cfhttpparam type="header" name="Content-type" value="application/vnd.cpc.ship.rate+xml">

<!--- Step4: Parse the XML response from the API and display the results --->

shipQuotes = xmlparse(httpResponse.Filecontent);
quoteDetail = shipQuotes["price-quotes"]["price-quote"];
quotesNum = ArrayLen(thisquote)

<table border="1">
<th>Service Code</th>
<th>Servie Name</th>
<th>Service Fee</th>
<cfloop from="1" to="#quotesNum#" index="i">

That’s it! You are all set to use CanadaPost API on your ColdFusion website / application.

If you have any questions or problems, please contact us.

References: Canada Post Getting Started Guide

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>