web
analytics

Harnessing the Power of the Classy API to drive online donations

By Eric Stout on December 16th, 2016

We humans love to see the success stories. It’s why we watch movies, award shows, and get sucked into a good book. We like to see if the hero gets the job done and wins the heart of that special someone at the same time. Seeing the win is just something that draws us in. Let’s get technical today and share how we can create a powerful call to action around your story, and drive up donations with donation management tools from Classy.org, and some custom code.

If you’re a non-profit that uses Classy.org to accept and track donations, you know there is some deep data there. Using the metrics from your campaigns and fundraisers we can show some powerful information and create clear call to action elements to increase your donations. We accomplished this beautifully for Lifewater International on their Blessed campaign by adding amounts raised and “thermometer” charts to show their donors just where they were with donations.

Let’s dive in and find out how we can access the API. You’ll need to be familiar with JavaScript and some PHP/Composer knowledge to get up and running. We are also going to assume you are working on Mac OSX/macOS for this tutorial.

Setting Up Classy Authentication

With the newest iteration of the Classy API we must now authenticate our calls with OAuth2.0. If you are familiar with OAuth2.0 you can use your preferred tool to authenticate, but for this example we will use Composer / Guzzle to get started.

1 – Install Composer and Guzzle

First of all, what is Composer? From getcomposer.org:

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Navigate to your project directory and install Composer locally using:

After composer installed, we will need to navigate to our project folder and setup our Composer dependencies – which in this case – is Guzzle.

Create a file called composer.json and add Guzzle. Your file should look like this:

Then execute the install process via the command line:

Now we have Composer and Guzzle all setup and ready to rock. Let’s connect it to Classy and start getting some data!

2 – Setting Up the Classy API Call, Generating a Token, and Getting Campaign Data

Now, in our project directory, lets create a file called classy.php.

The first thing we will need to do is autoload Guzzle.

 

This will load all the functions we need to create our response. Next, we will setup our client ID and secret key (you can obtain these from classy.org) as well as the client.


(Note: Be sure to protect your Secret Key. It’s a good idea to exclude it from public repositories and from public view)


Adding to our classy.php file:

Now we will setup the request, handle errors, and generate a token to get values with. Continue adding to classy.php:

Now that we have a token, we can grab data from their API endpoints. You can find more documentation on the Classy API here: https://developers.classy.org/api-docs/v2/index.html. In this example we will be grabbing data sets for campaigns. Again, adding to classy.php:

Here, we’ve created a function called getCampaign and we will use this function to actually make the call. The function takes 3 parameters, $accessToken, $client, $id. The $id parameter is the ID of the campaign you are trying to call, which can be found in Classy. The last step for our classy.php file is to call the getCampaign function. For this example we are calling a Lifewater.org campaign.

So to recap, your classy.php file should look similar to this at this point:

Great! We did it! So what does this return? A nice JSON response with lots of data to access:

We have a few key values to take a look at here in this response. We get the name of the campaign as well as the goal of the campaign and this is very useful when trying to display how much has been raised as well as whats left to go. But now what? We need to do something with this response. So, lets save it to our server so that we can reference the classy data later.

Let’s modify our getCampaign function a bit to create a JSON file.

Great! Now we have a a response that will create a file with our data when it is called. There are a few ways you can handle executing this file to create updates to the data/generate a new response. For testing purposes you can just open your browser and navigate to classy.php and it will generate. You could also trigger this within your template, another php function, an ajax request, or even a cron job – whatever works best for your implementation.

You may have noticed that while we have the goal for the campaign we don’t have any other data such as how much money has been raised. Strangely, Classy stores this information within a different endpoint. So lets dive into getting more detailed information about our campaign.

Adding to our classy.php file, lets create a new function called getCampaignOverview():

This function is nearly identical to our first campaign function, with the exception of the endpoint url. It also generates a file for us (classy-{id)-overview.json) for us to grab data from. A typical response from this endpoint would look similar to this:

Now that we have this data, we can use it to calculate amount left, amount raised, or how many people have donated and display it on your site which can be great information to show to potential donors and create a sense of urgency.

Your finished classy.php file should look similar to this:

Overview

You did it! Hopefully you have a functioning API call to Classy to gather information about your campaign. Since you’ve made it this far, pat yourself on the back. If you have any questions or would like to implement this on your site please reach out to us at https://factor1studios.com/contact/.