Anyone who knows me is fully aware that deep down I’m a complete geek. Further to that, I’m also inherently lazy, so – as I talked about in my talk at BrightonSEO last year – APIs are something that I’m very passionate about making the most of. Now, having a ‘passion’ for something like that isn’t really a talking point at the pub, but it is something that can save each and every person who works in SEO a load of time.
So, let’s get started.
Unless you’ve been living under the proverbial rock since 2004 you’ll know that Moz provide a suite of SEO tools and have arguably become the defacto standard when it comes to metrics. ‘Page Authority’ and ‘Domain Authority’ are getting to the point where they’re as well known as PageRank and, although the likes of MajesticSEO and Ahrefs would argue that their indexes are of a higher quality and quantity, the ease of use that Moz provides keeps OpenSiteExplorer in my list of most used tools.
But what if you want to do things en-masse? Grabbing the DomainAuthority of 500 sites manually would take forever and, although there are a few different options that integrate with Excel and Google Docs there comes a time when being able to create your own custom-made tools is almost a must have. This is where an API comes in.
Essentially – and this is way oversimplified – an API is a way of accessing a data point to get the information that you need. In this example I’m going to take you through getting set up with the Moz API, explain a few bits of code and give you a downloadable set of files that could be installed on any server that runs PHP. It probably won’t be life changing but will give you a great place to start.
Stage 1: Generate your API keys.
By accessing the Moz API you’re using resources on their server, which is limited by the number of calls you can make each second. Because of this the first thing you’ll need to do is generate your Access ID and Secret Key which is really just a username and password. Log in to your Moz account, go to https://moz.com/products/api/keys, generate your keys, and store them somewhere safe. Once you’re done you can come back to this page at any time to check or regenerate your keys.
Stage 2: A Simple API call.
Stay on that Moz page and scroll down a little. You’ll see a box that looks something like this:
Notice the ‘Sample’ URL? Copy and paste that into your browser. You should see something like this:
Go you. You just made your first API call. Okay, so it might not mean anything but in the URL you’ll see ‘Cols=2048’. Change that to ‘Cols=103616137252’ and refresh the page so that you now see this:
Not the easiest of things to read, but let’s translate things using this reference:
- Subdomain MozRank (normalised): 7.6202542681508305
- Subdomain MozRank (raw): 2.9744205357664444e-06
- Domain Authority: 92.30687761491824
- Number of External Links to the URL: 67236
- Total number of Links to the URL: 82035
- MozRank (Normalised): 6.882065684679716
- MozRank (Raw): 1.126529888185239e-07
- PageAuthority: 90.23459668002063
- HTTP Status: 200
- Canonical URL: moz.com/blog
Now we’re getting somewhere. In an instant we access the Moz API, pull back a load of data, and show it on the screen. Now all we need to do is throw a bit of code together that does all of this for multiple URLs… simple!
Get Your Code On!
For this example we’re going to use PHP but Moz have some Ruby and Python examples here too. The aim of this simple script will be:
- Show a multi-line text box on the screen that lets us input a load of URLs.
- Batch those URLs (so that we’re limiting the amount of calls we’re making) and send them to Moz.
- Get a load of data back.
- Show it on screen.
I’ll miss out an explanation of the text box code as this is just HTML, but part 2 is quite important. Most people start off by just sending one URL at a time to Moz, processing that data, and then moving onto the next one. This takes far longer than it should because each time you make a call to Moz there’s a period of time where you’re waiting for them to find the data and send it back.
Think of it like going to the bar, ordering a pint, waiting for that to come, and then doing the same thing for all 80 of your mates instead of just asking for all those drinks in one go.
Instead, we batch up the URLs using a built in PHP function:
$chunked_verified_urls = array_chunk($urls,80);
This then allows us to send 80 URLs at a time over to Moz meaning that instead of asking them for information 80 times we’re just making one big request.
Moving on from there we send the batched list of URLs to Moz using the ‘getSeomozMetrics()’ function that’s included in the attached file. The data is returned, we switch it around to grab the bits that we want, and then output it to the screen. I’ve added plenty of comments to explain what’s going on so it should (hopefully) make sense. Here’s what we end up with…
Get the code!
Finally, the most important bit – a free download. There’s a single PHP script in this ZIP file which you’ll just need to unzip, add your Moz credentials to the top, and upload to any web server that supports PHP. Alternatively I’ve included an example on my site that you can use to your heart’s delight (until someone decides to abuse it ;))
I hope this served as a decent introduction but there’s far more that can be done. I’ve already shared some other bits and pieces in my APISet Download, which includes a number of APIs and also some automated link profiling.
Questions? Comments about my horrendous code? Would love to hear your thoughts…