Authen
Set PG_API_ORIGIN and PG_API_KEY on your server, then call:
import { clientFromEnv } from './client.mjs';
const client = clientFromEnv();
const result = await client.authenticate(); // GET /v1/pay/balance
if (!result.ok) throw new Error(result.data?.error?.code ?? 'api_error');Download the helper from Get started. The complete API key is both the Bearer credential and the HMAC key. The helper supplies PG-Timestamp, PG-Nonce, PG-Version, and PG-Signature. No login endpoint is needed.
Your key needs wallet:read for this balance test, deposits:write plus deposits:read for Payin and its status reads, and withdrawals:write plus withdrawals:read for Payout and its status reads. Match the key to the issued Sandbox or LIVE hostname. Sandbox simulation also needs sandbox:write.
PHP and curl
Use 64-bit PHP 8.1+ with ext-curl, and ext-fileinfo for slip uploads:
<?php
require __DIR__ . '/PgMerchantClient.php';
$client = new PgMerchantClient(getenv('PG_API_ORIGIN'), getenv('PG_API_KEY'));
$result = $client->authenticate();
if (!$result['ok']) throw new RuntimeException('API authentication failed');This example executes curl, using Node to sign without printing credentials or headers:
node curl.mjs GET /v1/pay/balance
node curl.mjs POST /v1/pay/deposits payin.json order-1001-payinStore the Payin wire JSON text in payin.json, with a whole-baht JSON integer amount_minor, before the first send. Retain the exact file bytes and key for retries, including after restarting: do not reorder keys or reformat JSON. The curl example prints only HTTP status, id, status, and balance fields. Use the Node/PHP client to consume the full response inside your server session.
Troubleshooting
| Result | Check |
|---|---|
401 | API key, server clock, mode, and issued hostname |
403 scope_denied | Key scopes for this operation |
403 source_ip_not_allowed | Server outbound IP in the Merchant back office |
503 | Honor Retry-After; retain the original POST body and key |
Never ship the key or this helper to a customer browser/app. Do not log request headers or signatures.
Writing your own signer
Use the existing canonical string and test vectors. Sign the exact raw body you transmit. Persist the Idempotency-Key for every POST, and use a fresh nonce for each GET or POST attempt.