Skip to content

Authentication

The Parcel Monkey API used static headers on every call. Parcel2Go uses OAuth2 client credentials: you swap a Client ID and secret for a short-lived access token, then send that token on each call.

This guide covers client credentials only, which is what you need when you call the API for your own account. For other flows, see Parcel2Go’s authentication guide.

Parcel Monkey Parcel2Go
Credentials User ID + API key from your account settings Client ID + client secret from My Account > Account > API Clients
Sent on each call apiversion, userid, token headers Authorization: Bearer <access_token>
Lifetime Until you regenerate the key Token lasts expires_in seconds, then request a new one
Version apiversion header (3.0, 3.1, 3.3) No version header
Base URL https://api.parcelmonkey.co.uk API under /api, token endpoint under /auth (see environments)
Method Every call is POST REST: GET for reads, POST for actions
Before: Parcel Monkey
POST /GetQuote HTTP/1.1
Host: api.parcelmonkey.co.uk
Content-Type: application/json
apiversion: 3.3
userid: 12345
token: YOUR_API_KEY
After: Parcel2Go
POST /api/quotes HTTP/1.1
Host: sandbox.parcel2go.com
Content-Type: application/json
Accept: application/json
Authorization: Bearer YOUR_ACCESS_TOKEN

You create API clients yourself. Each environment has its own, and credentials don’t work across environments.

You choose the client secret when you create a live client. Parcel2Go only keeps a hash, so it can’t be shown again. Keep it on your server only, never in browser or mobile app code.

Send your credentials to the token endpoint as a form-encoded POST. Ask for two scopes:

  • public-api is needed for every call.
  • payment lets you pay for orders from your PrePay balance.
curl -X POST "https://sandbox.parcel2go.com/auth/connect/token" \
-H "Accept: application/json" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "scope=public-api payment" \
--data-urlencode "client_id=$P2G_CLIENT_ID" \
--data-urlencode "client_secret=$P2G_CLIENT_SECRET"
Response
{
"access_token": "435435435345SDFS...",
"expires_in": 7200,
"token_type": "Bearer"
}

Add the token to every API call:

Authorization: Bearer YOUR_ACCESS_TOKEN

Tokens are short-lived. Don’t request a new token for every call.

  • Read the lifetime from expires_in, in seconds. Don’t hard-code it.
  • Reuse the token until shortly before it expires, then request a new one with the same credentials.
  • If a call returns 401, request a new token and retry once.
  • If you run several workers or servers, keep the token in a shared cache (for example Redis, APCu or your database), not in each process.

The PHP samples in this guide use this small client. It needs PHP 8.1+ and the curl extension, and has no other dependencies.

Parcel2Go.php
<?php
final class Parcel2Go
{
private ?string $token = null;
private int $expiresAt = 0;
public function __construct(
private string $clientId,
private string $clientSecret,
private string $baseUrl = 'https://sandbox.parcel2go.com', // live: https://www.parcel2go.com
private string $scope = 'public-api payment',
) {}
/** Call the API. $path starts with /api, e.g. /api/quotes. */
public function request(string $method, string $path, ?array $body = null, array $query = []): array
{
$url = $this->baseUrl . $path . ($query ? '?' . http_build_query($query) : '');
$payload = $body === null ? null : json_encode($body, JSON_THROW_ON_ERROR);
[$status, $response] = $this->call($method, $url, $payload);
if ($status === 401) {
$this->token = null; // expired or revoked: get a new token and retry once
[$status, $response] = $this->call($method, $url, $payload);
}
if ($status >= 400) {
throw new RuntimeException("Parcel2Go {$method} {$path} returned {$status}: {$response}");
}
return $response === '' ? [] : json_decode($response, true, flags: JSON_THROW_ON_ERROR);
}
private function call(string $method, string $url, ?string $payload): array
{
$headers = ['Accept: application/json', 'Authorization: Bearer ' . $this->token()];
if ($payload !== null) {
$headers[] = 'Content-Type: application/json';
}
return $this->send($method, $url, $headers, $payload ?? ($method === 'POST' ? '' : null));
}
private function token(): string
{
if ($this->token !== null && time() < $this->expiresAt - 60) {
return $this->token;
}
[$status, $response] = $this->send('POST', $this->baseUrl . '/auth/connect/token', [
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
], http_build_query([
'grant_type' => 'client_credentials',
'scope' => $this->scope,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
]));
if ($status !== 200) {
throw new RuntimeException("Parcel2Go token request returned {$status}: {$response}");
}
$data = json_decode($response, true, flags: JSON_THROW_ON_ERROR);
$this->token = $data['access_token'];
$this->expiresAt = time() + (int) $data['expires_in'];
return $this->token;
}
private function send(string $method, string $url, array $headers, ?string $payload): array
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
]);
if ($payload !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException('Parcel2Go request failed: ' . curl_error($ch));
}
return [curl_getinfo($ch, CURLINFO_RESPONSE_CODE), $response];
}
}

The token is cached in the object. Swap $token and $expiresAt for your shared cache if you have more than one process.

Sandbox Live
Get credentials Sandbox portal My Account > API Clients
Token endpoint https://sandbox.parcel2go.com/auth/connect/token https://www.parcel2go.com/auth/connect/token
API base https://sandbox.parcel2go.com/api https://www.parcel2go.com/api

Keep the base URL and credentials in config so you can switch environments without code changes. All the URLs are on Parcel2Go’s environments page.