Eric Nagel

Using the ShareASale API

I have been, or can be if you click on a link and make a purchase, compensated via a cash payment, gift, or something else of value for writing this post. Regardless, I only recommend products or services I use personally and believe will be good for my readers.

There are plenty of things you can do via the ShareASale website, including running reports, getting links, and keeping updated on current offers. But when you’re dealing with dozens, or hundreds, of merchants, you can really increase your productivity by harnessing the power of the ShareASale API.

To get started, login to ShareASale and open the API Reporting page (found under Tools). In the section labeled IP Addresses and Token Key, you’ll need to enter the IP of your server. The easiest way to do this is to ping your domain name from the windows command line (or, ask your hosting provider). The token is your password to access the API.

Authentication to the ShareASale API is done via a combination of userID, IP address, and Token. Finally, select which actions you want your account activated for (transaction reports, creative reports, and / or merchant reports).

The section on the right, labeled Report Parameters, gives an idea of what can be done with the ShareASale API. In this example, I’ll be working with the merchantStatus action, and using PHP to gather the information & work further with it.

First, we need to build the URL. All URLs start with https://shareasale.com/x.cfm, followed by the parameter list.

$cURL = 'https://shareasale.com/x.cfm?version=1.1 &action=merchantStatus &affiliateId=yourID &token=yourToken &programStatus=online &XMLFormat=0';

This will return the following columns:
Merchant Id, Merchant, WWW, Program Status, Program Category, Sale Comm, Lead Comm, Hit Comm, Approved, Link Url

The first line returned contain the column headers, and the rest of the file contains the data. As usual with ShareASale, fields are pipe (|) delimited.

First, the variable $rsMap is created, to hold the column headings and their index (place in line). Then, the file is read and once data is found, $rsMap is populated. In this example, $rsMap looks like:

Array
(
	[Merchant Id] => 0
	[Merchant] => 1
	[WWW] => 2
	[Program Status] => 3
	[Program Category] => 4
	[Sale Comm] => 5
	[Lead Comm] => 6
	[Hit Comm] => 7
	[Approved] => 8
	[Link Url] => 9
)

After $rsMap is filled up, the script continues to read lines, saving them in $rsMerchant. If the line isn’t empty (which the last line is always empty, so we have this check put in place) and the Merchant name isn’t empty (I found 1 Merchant who doesn’t have a name – not sure if this has been fixed yet), you can work with this data. For example, if you want to use the Merchant name, it’s saved in $rsMerchant[$rsMap[‘Merchant’]]. The Merchant’s URL is saved in $rsMerchant[$rsMap[‘WWW’]] and your affiliate link is $rsMerchant[$rsMap[‘Link Url’]].

Geek stuff

I use $rsMerchant[$rsMap[‘Link Url’]] to make the code easier to use, but the nested associative arrays may confuse some people, so let’s break it down.
$rsMap[‘Link Url’] is 9 (see above), so $rsMerchant[$rsMap[‘Link Url’]] translates to $rsMerchant[9], which is the 10th column of data (start counting at 0).

Now you could use this to pull all merchants from a particular category (category=fam, or category=fud (see the ShareASale site for list of categories & their values)) and create a page with just merchants from this category. You’d want to save this data, and recreate it daily, or weekly, as you are limited to 200 requests per month (I run 3 queries, every day, using up about 100 queries / month).

Tip

While you’re developing your script, save the entire result from ShareASale and save it as a .csv file. Then, make your script read the .csv file, not the ShareASale URL. This way, you only use 1 API request as you develop your script. You can see where I use this in the script below.

Here’s a script which pulls the merchant list, and outputs the data you can work with:

<?php
	$cURL = 'https://shareasale.com/x.cfm?version=1.1 &action=merchantStatus &affiliateId=yourID &token=yourToken &programStatus=online &XMLFormat=0';
	// $cURL = 'temp/sasmerchants.csv';


	$fp = fopen($cURL, "r");
	if ($fp) {
		$rsMap = array();
		while (empty($rsMap) && (($rsHeadings = fgetcsv($fp, 1000, "|")) !== FALSE)) {
			if (md5(serialize($rsHeadings)) != '5b448a7bdbeea0be7d7f758f5f8ee90b') {
				while (list($nIndex, $cColumn) = each($rsHeadings)) {
					// echo("$cColumn => $nIndex<br />\n");

					$cColumn = ereg_replace("\(.+\)", "", $cColumn);

					$rsMap[$cColumn] = $nIndex;
				} // ends while (list($nIndex, $cColumn) = each($rsHeadings))
				// print_r($rsMap);
			} // ends
		}
		// print_r($rsMap);
		/*
		Array
		(
			[Merchant Id] => 0
			[Merchant] => 1
			[WWW] => 2
			[Program Status] => 3
			[Program Category] => 4
			[Sale Comm] => 5
			[Lead Comm] => 6
			[Hit Comm] => 7
			[Approved] => 8
			[Link Url] => 9
		)
		*/

		while (($rsMerchant = fgetcsv($fp, 1000, "|")) !== FALSE) {
			if (
				(md5(serialize($rsMerchant)) != '5b448a7bdbeea0be7d7f758f5f8ee90b') &&
				!empty($rsMerchant[$rsMap['Merchant']])
				) {
				print_r($rsMerchant);
			} // ends <not empty>
		} // ends while (($data = fgetcsv($fp, 1000, "|")) !== FALSE)
		fclose($fp);
	} // ends if ($fp)
?>

I use the ShareASale API to get yesterday’s stats and load it into my custom tracking script, so I know ROI per merchant. My script runs daily (sometime before I wake up in the morning) and request yesterday’s statistics. Here’s the URL:

$dYesterday = date("m/d/Y", time()-86400);
$cURL = 'https://shareasale.com/x.cfm?action=activity&affiliateId=yourID&token=yourToken&dateStart=' . $dYesterday . '&dateEnd=' . $dYesterday . '&XMLFormat=0';

As I work with more & more merchants in the ShareASale network, automating these tasks has saved me countless hours of manual labor.

If you have questions, feel free to leave a comment or email me!