Skip to main content

$client->keyUsage()

Usage Stats

Reports the number of lookups a key consumed over a date range, as a total and a daily breakdown.

The range defaults to the last 21 days. start and end take UNIX timestamps in milliseconds, and end defaults to the current time. The maximum range is 90 days.

Query at most three tags at once.

Endpoint​

GET /keys/{key}/usage

See the API reference for this endpoint.

This operation needs a Management Key. Pass it as the second argument to IdealPostcodes::client(), which sends it in the Authorization header. See setup.

Signature​

public function keyUsage(string $key, array $params = []): ApiKeyUsageResponse

Arguments​

NameTypeRequiredDescription
$keystringyesAPI Key
$paramsarraynoQuery parameters. Keys under Query Parameters.

Query Parameters​

Pass these as keys of $params.

NameTypeRequiredDescription
startintnoStart Timestamp
endintnoEnd Timestamp
tagsstring|list<string>noTags
licenseestringnoLicensee Key

Response​

IdealPostcodes\Responses\ApiKeyUsageResponse. Read fields as properties, such as $response->result, or as an array with the API's field names. toArray() returns the decoded body.

Errors​

An HTTP error throws ApiException with the HTTP status, the API error code from getCode() and the decoded body. A transport failure throws TransporterException. See error handling.

Example​

key-usage.phpphp
<?php

declare(strict_types=1);

use IdealPostcodes\IdealPostcodes;

require 'vendor/autoload.php';

$client = IdealPostcodes::client('ak_test', 'uk_test');

$response = $client->keyUsage('ak_test', [
'start' => strtotime('-7 days') * 1000,
'end' => time() * 1000,
]);

foreach ($response->result->dailyCount as $day) {
echo $day->date, ': ', $day->count, PHP_EOL;
}

echo 'Total: ', $response->result->total, PHP_EOL;