Code Samples
The 6 samples below are all tested and include:
- HMAC signature generation
- HTTP call wrappers with response checking (
code/error_key) - Webhook signature verification
Pick the right one
Dependencies
| Language | Dependencies | Notes |
|---|---|---|
| Python | requests (or httpx / urllib3) | hmac / hashlib / secrets are stdlib |
| Node.js | Node.js ≥ 18 (native fetch) | crypto is built-in |
| Go | stdlib crypto/hmac / crypto/sha256 / net/http | No third-party deps |
| Java | JDK 11+ stdlib (HttpClient / Mac / MessageDigest) | Signing needs no third-party deps; the usage sample parses responses with Jackson (any JSON library works) |
| PHP | PHP 7.4+ with curl extension | hash_hmac / random_bytes are built-in |
| cURL | openssl + awk (+ optional jq to check responses) | Generic shell tools |
Common Variables
APP_ID = "cp_a1b2c3d4..." # 31 chars, cp_ + 28 hex
SECRET = "f1e2d3c4..." # 64 hex chars
BASE = "https://api.coinepay.net"Don't hardcode
Samples have SECRET inline for clarity; production code should read from env vars / Vault / Secret Manager.
Handling Responses
Every endpoint returns the same envelope. All samples follow these rules:
- Success is
code == 0(never200). Readdataonly then. - Don't rely on the HTTP status alone: most business errors (
40000,40001,50000) come back with HTTP 200 and a non-zerocode. - Branch on
error_key— a stable machine key present on every error.messageis localized text for humans (English unless you sendAccept-Language); never match on it. 401/40100openapi_invalid_credentials: signature, clock or credential problem — fix it and re-sign; retrying unchanged will not help.429/42900too_many_requests: waitRetry-Afterseconds, then retry (sameIdempotency-Keyon write endpoints).
json
{ "code": 0, "message": "Success", "data": { "card_id": "card_12345", "status": 1, "status_desc": "pending" } }
{ "code": 40000, "message": "Insufficient balance", "error_key": "insufficient_balance" }See Error Codes for the full error_key dictionary.