Envoyez ces quatre en-têtes avec chaque requête. L’horodatage doit être à moins de 5 minutes de notre horloge et chaque nonce ne peut servir qu’une fois.
Reliez ces sept parties par un saut de ligne : le préfixe fixe SGSUPPLY1, la méthode HTTP, le chemin tel qu’envoyé, la chaîne de requête brute (ou rien), l’horodatage, le nonce et le SHA-256 hexadécimal en minuscules du corps (d’une chaîne vide s’il n’y en a pas). Signez-la avec votre secret.
Python
import hashlib, hmac, json, os, secrets, time, urllib.parse
import requests
KEY_ID = os.environ["SMSGRAB_SUPPLY_KEY"]
SECRET = os.environ["SMSGRAB_SUPPLY_SECRET"].encode()
def signed(method, url, payload=None):
body = b"" if payload is None else json.dumps(payload, separators=(",", ":")).encode()
parts = urllib.parse.urlsplit(url)
timestamp = str(int(time.time()))
nonce = secrets.token_urlsafe(24)
canonical = "\n".join(["SGSUPPLY1", method, parts.path, parts.query,
timestamp, nonce, hashlib.sha256(body).hexdigest()])
headers = {
"SmsGrab-Key": KEY_ID,
"SmsGrab-Timestamp": timestamp,
"SmsGrab-Nonce": nonce,
"SmsGrab-Signature": "v1=" + hmac.new(SECRET, canonical.encode(), hashlib.sha256).hexdigest(),
}
if body:
headers["Content-Type"] = "application/json"
return requests.request(method, url, data=body or None, headers=headers, timeout=30)
signed("POST", "https://smsgrab.com/api/supply/v1/messages", {"messages": [{
"id": "gw-7781", "port": "1", "from": "WhatsApp",
"text": "Your WhatsApp code: 482-913", "received_at": "2026-11-28T10:00:00Z",
}]})
Node.js
import { createHash, createHmac, randomBytes } from 'node:crypto';
const keyId = process.env.SMSGRAB_SUPPLY_KEY;
const secret = process.env.SMSGRAB_SUPPLY_SECRET;
export async function signed(method, url, payload) {
const body = payload === undefined ? '' : JSON.stringify(payload);
const { pathname, search } = new URL(url);
const timestamp = String(Math.floor(Date.now() / 1000));
const nonce = randomBytes(18).toString('base64url');
const canonical = ['SGSUPPLY1', method, pathname, search.replace(/^\?/, ''), timestamp, nonce,
createHash('sha256').update(body).digest('hex')].join('\n');
return fetch(url, {
method,
body: body === '' ? undefined : body,
headers: {
'SmsGrab-Key': keyId,
'SmsGrab-Timestamp': timestamp,
'SmsGrab-Nonce': nonce,
'SmsGrab-Signature': `v1=${createHmac('sha256', secret).update(canonical).digest('hex')}`,
...(body === '' ? {} : { 'Content-Type': 'application/json' }),
},
});
}
await signed('POST', 'https://smsgrab.com/api/supply/v1/heartbeat', {
agent: 'my-gateway/1.0',
interval_seconds: 30,
ports: [{ port: '1', state: 'ready', signal_dbm: -71, operator: 'Turkcell' }],
});
PHP
function smsgrab_signed(string $method, string $url, ?array $payload = null): array
{
$body = $payload === null ? '' : json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
$parts = parse_url($url);
$timestamp = (string) time();
$nonce = rtrim(strtr(base64_encode(random_bytes(18)), '+/', '-_'), '=');
$canonical = implode("\n", ['SGSUPPLY1', $method, $parts['path'], $parts['query'] ?? '',
$timestamp, $nonce, hash('sha256', $body)]);
$headers = [
'SmsGrab-Key: '.getenv('SMSGRAB_SUPPLY_KEY'),
'SmsGrab-Timestamp: '.$timestamp,
'SmsGrab-Nonce: '.$nonce,
'SmsGrab-Signature: v1='.hash_hmac('sha256', $canonical, getenv('SMSGRAB_SUPPLY_SECRET')),
];
$curl = curl_init($url);
curl_setopt_array($curl, [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30]);
if ($body !== '') {
$headers[] = 'Content-Type: application/json';
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = (string) curl_exec($curl);
return ['status' => curl_getinfo($curl, CURLINFO_RESPONSE_CODE), 'body' => json_decode($response, true)];
}
smsgrab_signed('PUT', 'https://smsgrab.com/api/supply/v1/lines', ['lines' => [
['port' => '1', 'phone_e164' => '+905321112233', 'country_iso' => 'TR', 'operator' => 'Turkcell'],
]]);
curl
URL="https://smsgrab.com/api/supply/v1/messages"
BODY='{"messages":[{"id":"gw-7781","port":"1","from":"WhatsApp","text":"Your WhatsApp code: 123-456","received_at":"2026-11-28T10:00:00Z"}]}'
TS=$(date +%s)
NONCE=$(openssl rand -base64 18 | tr '+/' '-_' | tr -d '=')
REQUEST_PATH=$(printf '%s' "$URL" | sed -E 's#^https?://[^/]+##; s#\?.*$##')
BODY_HASH=$(printf '%s' "$BODY" | openssl dgst -sha256 -hex | sed 's/^.* //')
SIGNATURE=$(printf 'SGSUPPLY1\nPOST\n%s\n\n%s\n%s\n%s' "$REQUEST_PATH" "$TS" "$NONCE" "$BODY_HASH" \
| openssl dgst -sha256 -hmac "$SMSGRAB_SUPPLY_SECRET" -hex | sed 's/^.* //')
curl -s -X POST "$URL" \
-H "SmsGrab-Key: $SMSGRAB_SUPPLY_KEY" \
-H "SmsGrab-Timestamp: $TS" \
-H "SmsGrab-Nonce: $NONCE" \
-H "SmsGrab-Signature: v1=$SIGNATURE" \
-H "Content-Type: application/json" \
-d "$BODY"