Summer 2021 Sale

Cool off with 25% off all pricing plans!


Using the Give API to Create Charts with ChartJS

Abstract image with the words "Chart.js + Give Rest API by Roy Sivan"

What if I told you that the Give API + JavaScript = Awesome Reporting Tools? Mind-blown, right?

We already know how awesome Give is, but what if you need more power or visual aid from the data? Charts are an easy way to evaluate how a form is performing, how much donors are donating, or even which gateway people use the most.

For the purpose of this article, I am going to work on an example pie chart that will compare all my forms and, more importantly, demonstrate which one performs best.


D3 and highcharts are powerful tools for JS data, but for the sake of simplicity, we are going to use Chart.js. It’s powerful without being overly complex.

Chart.js has a few awesome charts built into its library with a large contributor community of people who extend and add features. Out of the box you get line charts, bar graphs, radar charts, polar area charts, and pie / doughnut charts. As it is, it’s powerful. A closer look tells us it really just uses the HTML5 canvas element to draw so it is easy to tap into and extend if you want further control.

Give API

Give comes with an amazing developer’s dream feature: an API. While the WordPress REST API is coming into core, it isn’t there yet. As of now the Give API gives you the same power, but built into a production-ready API. There is some great documentation about the API, and the available endpoints which include:

  • /donations
  • /stats
  • /forms
  • /donors

If you are not yet familiar with the Give API, I suggest you read that documentation first since I’ll reference it often throughout this article.

For this project, we are going to focus on the /donations endpoint, which gets the latest donations made across all the forms on your website.

bell icon

Like What You're Reading? Subscribe Here!

Newsletter Opt-in

Getting the Data you Need via AJAX

To start, we need to get the data, for that we are going to use jQuery Ajax. Here is some sample code from the current plugin I am working on:

Get the data

$.get( give_local.give_api_url + '/donations?key=' + give_local.key + '&token=' + give_local.token + '&number=100', function( res ) {
 dfd.resolve( res.donations );

The call is a pretty basic jQuery Ajax call. I use $.get, the give_local is a localized object I have created with a property of api_url. This just stores the Give API url or for me so I can re-use it anywhere in my JS. Since I want to be able to use it with multiple endpoints, I need to fill in the rest of the url. In this example, I want to get the latest donations so I add on /donations.

API Keys

I also need to pass through with the url my API key and API token. If you are not sure what your key and token are, follow the easy instructions under “Generating API Keys.” Don’t ever store your secret key in a local object since it is visible to anyone who knows how to find a localized object.

Ten Latest Donations

As a default for all of the Give API endpoints, only the ten most recent entries will show. For my example this is enough, but if you want to show more than ten, just add number= to your query string (i.e – don’t forget your API data). This will allow you to show more than the latest ten donations.

Creating the Pie Chart

As mentioned earlier, the goal is to create a pie chart so I can see which form actually performs the best. If you have a page that loads both forms, or maybe swaps them out, consider this an easy way to even do some A/B variant testing between forms.

Step 1: Saving the form names

Saving Form Names

$.each(donations, function (key, value) {
	var date = new Date( );
	if( !month[date.getMonth()] ) {
		month[date.getMonth()] =;
	} else {
		month[date.getMonth()] = month[date.getMonth()] +;

We aren’t really concerned with the donation amounts here, just the name of the forms, so first let’s loop through our donation data and create an array. The forms array will be an array of all the forms the donations have come through.

In my example I also push the form name to another array called labels this is a duplicate of forms, till I use $.unique which removes all duplicates leaving me with just an array with unique form names I have used.

Step 2: Loop through unique labels

Loop through the Labels

$.each(labels, function (key, value) {
	data.push(count(forms, value));
		value: count(forms, value),
		color: pieColors[key],
		highlight: '#C5E0DC',
		label: value

The Chart.js pie chart needs an array of objects. Each object needs the following properties:

  • value – the number amount
  • color – the default color
  • highlight – the secondary color for hover
  • label – the name of that object (in this case the form name)

As I loop through each unique form name, I store the value as how many times that form name came up in my donations which I can do by counting how many times it is in the forms array I created earlier. For example, if I have 10 donations and 8 were from “form-homepage” the forms array would have “form-homepage” duplicated eight times.

The color and highlight are either string colors like ‘red’ or can be hex/rgb like ‘#FF0000.’

Lastly the label, which is just the form name. In this case the form name is the value for each iteration of our loop.

Implementing Canvas and Chart.js

A pie chart of Give Donations using the Chart.js library

Now that we have the data, we need to display that data as a pie chart using the HTML5 canvas element and Chart.js.

In my HTML I created a <canvas> element with ID of “chart.” I know, very creative, right?

<canvas id=”chart”></canvas>

The code for Chart.js is pretty simple. It’s just some basic vanilla javascript to get the context of the canvas element. Then — using the Chart.js functions — we pass in the data.

Pass the data to the piechart

var month_key = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$.each( month, function( key, value ) {
	barChartData.labels.push( month_key[key] );
	barChartData.datasets[0].data.push( value );

With 4 lines of code (minus getting and preparing the data), you should have a functional pie chart that will show you how many of the last donations came from which form.

As you can see, I hovered over the pie chart and saw that my “Test Form” brought in three donations, while my “Another form” got four donations.

What Will You do with the Give API?

The data that you get back from the Give API is extensive. There are some awesome things you can probably do, especially when it comes to date-specific charts (donations between two days), as well as comparing donations month-per-month.

I am working on a plugin to showcase more charts I created. You can track my progress on the Github repo here. Included will be a shortcode integration so you can easily display the charts within your pages or posts for your visitors. Another great use case is to put them on a password-protected page, so only the right eyes can see your new chart — perhaps your board of trustees.

We would love to hear your ideas and thoughts on how you can marry Chart.js with the Give API and what uses and applications you’d like to do. Comment below with your ideas.

Amplify your fundraising with a GiveWP Plan

About the Author

Liked what you read?

We have a lot more where that came from! Join 15,197 subscribers who stay ahead of the pack.

Share this post

Share on facebook
Share on twitter
Share on linkedin

You might also like

One Response

  1. Wow this is cool, nice tutorial ;) I have just recently started diving into Give WP (so I can hopefully make some themes with it in mind) and it’s sooo cool and well coded. These guides are a nice added bonus as well.

Leave a Reply

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

Like what you're reading?

Subscribe and stay ahead of the curve.

Share on facebook
Share on twitter
Share on linkedin

Choose Your Plan

Get the “” add-on with Recurring Donations, Fee Recovery, Annual Receipts and more for one low price.

Pro Plan

normally $499



Most Popular

Plus Plan

normally $360




Basic Plan

normally $249