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.
Before and after
Section titled “Before and after”| 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 |
POST /GetQuote HTTP/1.1Host: api.parcelmonkey.co.ukContent-Type: application/jsonapiversion: 3.3userid: 12345token: YOUR_API_KEYPOST /api/quotes HTTP/1.1Host: sandbox.parcel2go.comContent-Type: application/jsonAccept: application/jsonAuthorization: Bearer YOUR_ACCESS_TOKENGet your credentials
Section titled “Get your credentials”You create API clients yourself. Each environment has its own, and credentials don’t work across environments.
- Sandbox: generate a Client ID and secret in the sandbox portal.
- Live: sign in to your Parcel2Go account and go to My Account > Account > API Clients. Choose Create API Client and pick Client Credentials.
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.
Request a token
Section titled “Request a token”Send your credentials to the token endpoint as a form-encoded POST. Ask for two scopes:
public-apiis needed for every call.paymentlets 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"<?php// Use the Parcel2Go class below. It requests and caches the token for you.$p2g = new Parcel2Go(getenv('P2G_CLIENT_ID'), getenv('P2G_CLIENT_SECRET'));{ "access_token": "435435435345SDFS...", "expires_in": 7200, "token_type": "Bearer"}Send the token
Section titled “Send the token”Add the token to every API call:
Authorization: Bearer YOUR_ACCESS_TOKENCache the token
Section titled “Cache the 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.
PHP client
Section titled “PHP client”The PHP samples in this guide use this small client. It needs PHP 8.1+ and the curl extension, and has no other dependencies.
<?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 and live
Section titled “Sandbox and live”| 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.