The Moat Analytics API is a simple way to programmatically retrieve your data organized by whatever metrics you want.
The API uses token-based authentication which is passed in a header to make an authenticated request.
Building an HTTP request with a token is simple. Assuming your token is abc123, you would create an Authorization header with the value Bearer abc123 like the example below to https://api.moat.com
HTTP/1.1 GET /1/stats.json
Authorization: Bearer abc123
Here are a few examples of sending an authentication token using some common libraries and tools.
When using cURL, we are piping the output to a tool called jq which pretty prints JSON on the command line:
$ curl -s -H 'Authorization: Bearer abc123'\
'https://api.moat.com/1/stats.json?metrics=level1,loads&start=2018-10-08&end=2018-10-08' | jq .
{
"data_available": true,
"results": {
"details": [
{
"level1_label": "ABC",
"level1_id": "123",
"loads": 1000
},
{
"level1_label": "XYZ",
"level1_id": "456",
"loads": 100
}
],
"summary": {
"loads": 1100
}
},
"query": {
"end": "2018-10-08",
"start": "2018-10-08",
"metrics": "level1,loads"
}
}
Using GuzzleHttp library:
<?php
// Using Composer, you would include GuzzleHttp library like so:
require_once 'vendor/autoload.php';
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://api.moat.com/1'
]);
$query = [
'start' => '2018-10-01',
'end' => '2018-10-01',
'metrics' => 'level1,loads'
];
$headers = [
'Authorization' => 'Bearer abc123'
];
try {
$resp = $client->get('/stats.json', ['query' => $query, 'headers' => $headers]);
print_r($resp->getBody()->getContents());
} catch (\GuzzleHttp\Exception\ClientException $exception) {
echo "Error from API: " . $exception->getMessage() . "\n";
}
Using urllib3 module:
import urllib3
import datetime
token = 'abc123'
auth_header = 'Bearer {}'.format(token)
yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
metrics = ('level1', 'loads')
query = {
'metrics': ','.join(metrics),
'start': yesterday,
'end': yesterday
}
http = urllib3.PoolManager()
resp = http.request('GET', 'https://api.moat.com/1/stats.json',
fields=query,
headers={'Authorization': auth_header})
print(resp.status)
print(resp.data)
If you are unable to log in and you are receiving an HTTP status code of 401, that means you have provided an invalid token. Make sure that you are using the token exactly as it was provided without any additional characters except for a single space to separate the word Bearer and the actual token. Example: Authorization: Bearer abc123