Skip to main content

Authentication

Authorization

To authorize a request, you must use the API key obtained earlier from your merchant panel. This key ensures secure communication between your server and our platform.

When making API requests, include the API key in the request header using the x-api-key field.

Example Header:

"x-api-key": "your-api-key-value"

Example Request:

POST /example/endpoint HTTP/1.1  
Host: api.bitzone.space
Content-Type: application/json
x-api-key: your-api-key-value

{
"field1": "value1",
"field2": "value2"
}
note

Ensure your API key is kept secure and never exposed publicly. If you believe your key has been compromised, generate a new one through the merchant panel immediately.

Signature Verification

To ensure security and confirm that webhook requests are genuinely sent by our platform, you must verify the request signature. The signature is provided in the x-signature header

Overview

  1. Obtain the request body (as a raw JSON string).
  2. Extract the signature from the x-signature header.
  3. Generate the signature using your API key.
  4. Compare the signatures using a secure method.

Signature Generation Algorithm

The signature is generated using the HMAC-SHA256 algorithm. It is calculated based on:

  • The request body (unaltered), and
  • Your API key.

Examples

Signature Verification Function

const crypto = require('crypto');

/**
* Verifies the webhook signature.
* @param {string} apiKey - Your API key.
* @param {string} requestBody - The raw request body (unaltered).
* @param {string} receivedSignature - The signature from the `x-signature` header.
* @returns {boolean} true if the signatures match; otherwise, false.
*/
function verifyWebhookSignature(apiKey, requestBody, receivedSignature) {
// Generate the computed signature
const computedSignature = crypto
.createHmac('sha256', apiKey) // Create an HMAC using SHA256
.update(requestBody) // Update it with the request body
.digest('hex'); // Convert the result to a hex string

// Compare the signatures securely
return crypto.timingSafeEqual(
Buffer.from(receivedSignature, 'hex'),
Buffer.from(computedSignature, 'hex')
);
}

module.exports = { verifyWebhookSignature };

Usage Example
const apiKey = 'your_api_key'; // Your API key
const requestBody = '{"event":"payment","data":{"amount":100,"currency":"USD"}}'; // Example request body
const receivedSignature = 'd3b07384d113edec49eaa6238ad5ff00'; // Signature from the x-signature header

if (verifyWebhookSignature(apiKey, requestBody, receivedSignature)) {
console.log('Signature is valid. The request is authenticated!');
} else {
console.log('Invalid signature. The request may be forged.');
}

FAQs

Q: What algorithm is used for signature generation?
A: HMAC-SHA256 is used.

Q: What should I do if the signature doesn't match?
A: Reject the request and respond with an HTTP status code 401 Unauthorized.


By following the above steps, you can securely validate the authenticity of webhook requests.