Node.js Guide for Calling Mefree.NET API
Last updated
npm install axios cryptoconst crypto = require("crypto");
/**
* Generate HMAC-SHA256 signature.
* @param {string} timestamp - Current UTC timestamp in ISO 8601 format.
* @param {string} method - HTTP method (e.g., GET or POST).
* @param {string} requestPath - API endpoint path (including query parameters).
* @param {string} secretKey - Your API Secret Key.
* @returns {string} - Base64-encoded signature.
*/
function generateSignature(timestamp, method, requestPath, secretKey) {
const message = `${timestamp}${method}${requestPath}`;
const hmac = crypto.createHmac("sha256", secretKey);
hmac.update(message);
return hmac.digest("base64");
}
/**
* Get the current UTC timestamp in ISO 8601 format.
* @returns {string} - Timestamp in ISO 8601 format (e.g., 2024-11-26T12:34:56.789Z).
*/
function getUtcTimestamp() {
return new Date().toISOString();
}const axios = require("axios");
const BASE_URL = "https://api.mefree.net";
const API_KEY = "your_api_key"; // Replace with your API Key
const SECRET_KEY = "your_secret_key"; // Replace with your Secret Key
/**
* Send an HTTP request to the Mefree.NET API.
* @param {string} method - HTTP method (GET or POST).
* @param {string} requestPath - API endpoint path (including query parameters).
* @returns {Promise<object>} - The JSON response from the API.
*/
async function sendRequest(method, requestPath) {
const timestamp = getUtcTimestamp();
const signature = generateSignature(timestamp, method, requestPath, SECRET_KEY);
const headers = {
"Content-Type": "application/json",
"MF-ACCESS-KEY": API_KEY,
"MF-ACCESS-SIGN": signature,
"MF-ACCESS-TIMESTAMP": timestamp,
};
const url = `${BASE_URL}${requestPath}`;
try {
const response = await axios({
method,
url,
headers,
});
return response.data;
} catch (error) {
console.error("Request failed:", error.response?.data || error.message);
throw error;
}
}(async () => {
try {
const response = await sendRequest("GET", "/api/config");
console.log("Account Information:", response);
} catch (error) {
console.error("Failed to fetch account information.");
}
})();(async () => {
try {
const requestPath = "/api/order?quantity=65000&target_address=TRON_ADDRESS&period=1";
const response = await sendRequest("POST", requestPath);
console.log("Order Created:", response);
} catch (error) {
console.error("Failed to create order.");
}
})();(async () => {
try {
const payHash = "abcd1234"; // Replace with the actual pay_hash
const requestPath = `/api/order/${payHash}`;
const response = await sendRequest("GET", requestPath);
console.log("Order Status:", response);
} catch (error) {
console.error("Failed to fetch order status.");
}
})();