Signature Code Examples

HMAC-SHA256 Authentication

Code examples for generating signatures to use with Trustsig API. Supports multiple languages and can be adapted for your backend immediately

Select Programming Language

signature.example.selectLanguageDesc

⬢

Node.js Implementation

JavaScript Runtime Environment

v18+
generateSignature.js
Node.js
import crypto from 'crypto';

// Type definitions
interface RequestHeaders {
  'Content-Type': string;
  'x-api-key': string;
  'x-signature': string;
  'x-timestamp': string;
}

function generateSignature(
  secretKey: string,
  method: string,
  fullUrl: string,
  body: any | null,
  timestamp: number
): string {
  // Parse body - if empty/null, use empty string, then stringify it
  const parseBody = body ? (typeof body === 'object' ? body : JSON.parse(body)) : '';
  const bodyString: string = JSON.stringify(parseBody);
  const signatureContent: string = `${timestamp}|${method}|${fullUrl}|${bodyString}`;

  return crypto
    .createHmac('sha256', secretKey)
    .update(signatureContent)
    .digest('hex');
}

// Usage Example
const apiKey: string = 'YOUR_API_KEY';
const secretKey: string = 'YOUR_SECRET_KEY';
const timestamp: number = Date.now();
const signature: string = generateSignature(
  secretKey,
  'GET',
  'https://testnet.trustsig.xyz/v1/balance/query',
  null,
  timestamp
);

// Headers for API request
const headers: RequestHeaders = {
  'Content-Type': 'application/json',
  'x-api-key': apiKey,
  'x-signature': signature,
  'x-timestamp': timestamp.toString()
};
              

Installation Note

Install crypto with: npm install crypto

Usage Instructions

  1. 1Replace 'YOUR_SECRET_KEY' with your Secret Key
  2. 2Modify method, URL and body according to your API usage
  3. 3Use the generated signature in the 'X-Signature' header of HTTP request

signature.example.curlExample

api-request.sh
cURL
curl --location 'https://testnet.trustsig.xyz/v1/balance/query' \
--header 'Content-Type: application/json' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'x-signature: a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2' \
--header 'x-timestamp: 1729900000000'