Posts Tagged ‘API’

Connecting to the MailChimp API with Coldfusion

Sunday, April 25th, 2010

I have just started playing with the MailChimp API with Coldfusion. As usual, there’s not a lot of Coldfusion documentation or examples. So I thought I’d start posting some of the how-to’s as I myself figure them out. Here will demonstrate how to make a simple connection with the API so you can start doing more productive things like adding emails to lists and getting stats.

Let’s begin at the beginning of using any API. We need an account and an API key. Go ahead and create an account as if you’re a client, or if you are doing this for an actual client go ahead and create an account for them. MailChimp makes this super simple. Once you have an account click on the ‘account’ button in the top left corner of the dashboard. This will take you to a set of links. Click the ‘API Keys & Info’ link. This will take you to a page where you can manage your API keys. Clicking ‘Add A Key’ will generate a new key for you to use.

Now that we have our key and account we’re ready to make our connection via Coldfusion. Before looking at any code, let’s make sure we know where our documentation is at. You can find the MailChimp docs here. Requesting data from MailChimp requires you to pass the method and parameters via the url: http://us1.api.mailchimp.com/1.2/. As parameters you need to indicate the type of output MailChimp should return data as. In our case we’ll use JSON. So we’ll attach ‘?output=json’. Next we need to specify the method. MailChimp has a ping() method meant for no other reason than to make sure the connection is working. So this is what we’ll use here. To do this we attach the method parameter ‘&method=ping’. If you look at the docs you’ll see that this requires only one parameter to work, which is the API key (this is required for all methods). So we’ll attach another parameter ‘&apikey=[yourapikey]‘.

So let’s look at how to write this using Coldfusion.

<!--- SET URL --->
<cfset VARIABLES.api_url = "http://us1.api.mailchimp.com/1.2/?output=json&method=ping&apikey=[your key]-us1">

<!--- SEND DATA VIA CFHTTP TAG --->
<cfhttp name="call" url="#VARIABLES.api_url#" result="VARIABLES.mycall">

<!--- BECAUSE RETURNED AS JSON WE NEED TO DESERIALIZE THE RESPONSE --->
<cfset VARIABLES.deserialized = deserializeJSON(VARIABLES.mycall.fileContent)>

<!--- DUMP RESPONSE --->
<cfdump var="#VARIABLES.deserialized#">

First we set our URL. In this case you’ll, of course, change the ‘[your key]‘ to whatever your actual key is that MailChimp generated for you in your account. Next we use the Coldfusion tag to send the url to MailChimp. MailChimp then processes it and sends back the result which we store as ‘VARIABLES.mycall’. Because we specified to MailChimp to return data back to us as JSON we need to make sure we deserialize it before displaying it. We do this using the ‘deserializeJSON()’ method. Also the response is passed back as a struct with the actual data from MailChimp stored in the ‘fileContent’ key. Lastly we just dump the data so we can see that ‘Everything’s Chimpy!’.

At this point you should be able to start playing more with the API to do more complex and practical things.

Using the UPS Shipping & Rating API with Coldfusion

Friday, December 11th, 2009

Just a heads up that this is a fairly long article and makes several assumptions about your application. Using the UPS API is fairly easy, but there are several variables that are required for the api to work successfully.

I’ll be making several assumptions here.  First and foremost that you are looking to implement the UPS shipping and rating API for ecommerce.  Second that you have a database set up with a table named ‘shipping_tbl’ that includes column names ‘package_weight’, ‘package_width’, ‘package_length’, and ‘package_height’.  And that these values are in some way related to the products whose dimensions and weight they represent.  As well as other assumptions like how we store our cart data that will be further explained later.

Let’s start out with our HTML.  This is the HTML that represents your cart, which is where I assume you are showing the shipping rate for whatever packages are being sold.  We’ll pretend there’s a cart here.  I’m, however, only going to show the code for the output of the shipping rate.  So it is as follows.

The HTML (Output of shipping data)

<!---set our shipping rate--->
<cfset VARIABLES.shippingrate = "">

<span class=”shippingrate”>
	<!---output our shipping rate to the page--->
	<cfoutput>#dollarformat(VARIABLES.shippingrate)#</cfoutput>
</span>

(more…)

Paypal Express example with Coldfusion 8

Tuesday, November 3rd, 2009

If you are building an ecommerce site it’s likely that you’re using paypal as your payment processing method.  Paypal’s documentation is not bad, but for someone creating a paypal checkout with paypal’s Website Payments Pro package, the documentation can be somewhat overwhelming.  Once you get the hang of it, this documentation will be indispensable.

So what I’m going to explain is paypal’s express checkout feature in the Website Payments Pro package offered.  This is the paypal button that is on the cart of the website.  When clicked the user is redirected to paypal where they log into their account, choose their payment method, are then returned to the cart where they proceed to checkout. My quick disclaimer is that this is the quick and easy way of doing this.  This is just to simplify the process, not how it should actually be implemented on a production site.

(more…)