We recommend only using this approach when you encounter timestamp-related issues. For regular use, your local system's timestamp should suffice
Always make sure that your system time is accurate and try to keep it synchronised with a reliable source
If you're receiving a "Ahead of Time" 401 error, it likely indicates a mismatch between your local timestamp and the server timestamp. This discrepancy can lead to failed authentication, thus triggering a 401 error.
You can follow the steps below to resolve the issue:
Fetch Server Timestamp: Use the Server Timestamp endpoint to obtain the current timestamp from the server, using the link here.
Generate Signature with Server Timestamp: Use the fetched server timestamp to generate the signature for your API requests.
Re-attempt API Request: Send your API request again, this time using the signature generated with the server's timestamp.
Here is a reference code to utilise server timestamp:
var CryptoJS =require('crypto-js');constaxios=require('axios');constcurrentTimestamp=async () => {try {constresponse=awaitaxios.get(`https://api.onramp.money/onramp/api/v2/common/public/currentTimestamp`);// console.log("Timestamp API Response:", JSON.stringify(response.data.data, null, 2));returnresponse.data.data; } catch (error) {console.error("Timestamp API Error:", error);returnnull; }};asyncfunctionmain() {try {constAPI_KEY="API_KEY";constAPI_SECRET="API_SECRET";let body = { page:2, pageSize:50, order:"ASC" };let current_timestamp =awaitcurrentTimestamp();//let current_timestamp = new Date().getTime(); // local timestampif (!current_timestamp) { current_timestamp =newDate().getTime(); }console.log("Current Timestamp:", current_timestamp);let payload = { timestamp: current_timestamp, body: body }; payload =Buffer.from(JSON.stringify(payload)).toString('base64');let signature =CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA512(payload,API_SECRET));let options = { url:'https://api.onramp.money/onramp/api/v1/transaction/merchantHistory', method:'POST', headers: {'Accept':'application/json','Content-Type':'application/json;charset=UTF-8','X-RECON-SIGNATURE': signature,'X-RECON-APIKEY':API_KEY,'X-RECON-PAYLOAD': payload }, data: body };console.log("API Request Options:", options);let data =awaitaxios(options);console.log("API Response:",data?.data); } catch (error) {console.log("Error Details:", error); }}main();