Signed Requests
Add a signature to each API request so that a stolen or replayed request is refused, even if someone sees your traffic.
Signed requests are an extra layer on top of your secret key. To turn them on for your business, contact Wire Gh support. Once they are on, every request made with your secret key must be signed. Requests with your public key are not affected.
How it works
For each request you build a short text from the request details, sign it with your secret key, and send the result in three headers. We rebuild the same text on our side and refuse the request if the signature does not match, if it is more than 5 minutes old, or if its nonce has been used before.
Headers
| Header | Value |
|---|---|
X-WireGh-Timestamp | The current time as a Unix timestamp in seconds. Must be within 5 minutes of our clock. |
X-WireGh-Nonce | 32 random lowercase hex characters, new for every request. |
X-WireGh-Signature | The signature, as 128 lowercase hex characters. |
Build the signature
- Hash the bodyTake the SHA256 of the exact request body as lowercase hex. For a request with no body, hash an empty string.
- Join the partsJoin these five values with a newline character between each: timestamp, nonce, HTTP method in capitals, path with query string (for example
/transaction?perPage=10), and the body hash. - SignCompute HMAC SHA512 of that text with your secret key, as lowercase hex.
1731112800
9f2c4e1a7b3d5f6091a2b3c4d5e6f708
POST
/transaction/initialize
3f1b0c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f9const crypto = require('crypto');
async function signedRequest(method, pathWithQuery, body) {
const secret = process.env.WIREGH_SECRET_KEY;
const raw = body ? JSON.stringify(body) : '';
const timestamp = Math.floor(Date.now() / 1000).toString();
const nonce = crypto.randomBytes(16).toString('hex'); // 32 hex characters
const bodyHash = crypto.createHash('sha256').update(raw).digest('hex');
const base = [timestamp, nonce, method.toUpperCase(), pathWithQuery, bodyHash].join('\n');
const signature = crypto.createHmac('sha512', secret).update(base).digest('hex');
const res = await fetch('https://api.wiregh.com' + pathWithQuery, {
method,
headers: {
Authorization: 'Bearer ' + secret,
'Content-Type': 'application/json',
'X-WireGh-Timestamp': timestamp,
'X-WireGh-Nonce': nonce,
'X-WireGh-Signature': signature
},
body: raw || undefined // send exactly the bytes you hashed
});
return res.json();
}
const result = await signedRequest('POST', '/transaction/initialize', {
email: 'customer@email.com',
amount: 20000
});<?php
function wiregh_signed(string $method, string $pathWithQuery, ?array $body = null): array
{
$secret = getenv('WIREGH_SECRET_KEY');
$raw = $body ? json_encode($body) : '';
$timestamp = (string) time();
$nonce = bin2hex(random_bytes(16)); // 32 hex characters
$base = implode("\n", [$timestamp, $nonce, strtoupper($method), $pathWithQuery, hash('sha256', $raw)]);
$signature = hash_hmac('sha512', $base, $secret);
$ch = curl_init('https://api.wiregh.com' . $pathWithQuery);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_POSTFIELDS => $raw !== '' ? $raw : null, // send exactly the bytes you hashed
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $secret,
'Content-Type: application/json',
'X-WireGh-Timestamp: ' . $timestamp,
'X-WireGh-Nonce: ' . $nonce,
'X-WireGh-Signature: ' . $signature,
],
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
return $result;
}
$result = wiregh_signed('GET', '/transaction?perPage=10');Errors
A request that fails the check gets a 401 response with code bad_signature and one of these messages:
- Signed request headers missing or malformed. A header is missing or has the wrong format.
- Request timestamp outside the allowed window. Check that your server clock is correct.
- Invalid request signature. The text you signed differs from the request, usually because the body was changed after hashing.
- Nonce already used. Create a new nonce for every request, including retries.