Java Guide for Calling Mefree.NET API
Last updated
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class MefreeApiHelper {
// Generate HMAC-SHA256 signature
public static String generateSignature(String timestamp, String method, String requestPath, String secretKey) {
try {
String message = timestamp + method + requestPath;
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
mac.init(secretKeySpec);
byte[] hash = mac.doFinal(message.getBytes());
return Base64.getEncoder().encodeToString(hash);
} catch (Exception e) {
throw new RuntimeException("Error while generating signature: " + e.getMessage());
}
}
// Get current UTC timestamp in ISO 8601 format
public static String getUtcTimestamp() {
return java.time.ZonedDateTime.now(java.time.ZoneOffset.UTC)
.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
}
}import org.apache.http.client.methods.*;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class MefreeApiClient {
private static final String BASE_URL = "https://api.mefree.net";
private static final String API_KEY = "your_api_key"; // Replace with your API Key
private static final String SECRET_KEY = "your_secret_key"; // Replace with your Secret Key
public static String sendRequest(String method, String requestPath) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String timestamp = MefreeApiHelper.getUtcTimestamp();
String signature = MefreeApiHelper.generateSignature(timestamp, method, requestPath, SECRET_KEY);
// Create HTTP request
HttpRequestBase request;
if (method.equalsIgnoreCase("GET")) {
request = new HttpGet(BASE_URL + requestPath);
} else if (method.equalsIgnoreCase("POST")) {
request = new HttpPost(BASE_URL + requestPath);
} else {
throw new IllegalArgumentException("Unsupported HTTP method: " + method);
}
// Add headers
request.setHeader("Content-Type", "application/json");
request.setHeader("MF-ACCESS-KEY", API_KEY);
request.setHeader("MF-ACCESS-SIGN", signature);
request.setHeader("MF-ACCESS-TIMESTAMP", timestamp);
// Send the request
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
if (statusCode != 200) {
throw new RuntimeException("HTTP " + statusCode + ": " + responseBody);
}
return responseBody;
}
} catch (Exception e) {
throw new RuntimeException("Error while sending request: " + e.getMessage(), e);
}
}
}public class Main {
public static void main(String[] args) {
try {
String response = MefreeApiClient.sendRequest("GET", "/api/config");
System.out.println("Account Information: " + response);
} catch (Exception e) {
System.err.println("Failed to fetch account information: " + e.getMessage());
}
}
}public class Main {
public static void main(String[] args) {
try {
String requestPath = "/api/order?quantity=65000&target_address=TRON_ADDRESS&period=1";
String response = MefreeApiClient.sendRequest("POST", requestPath);
System.out.println("Order Created: " + response);
} catch (Exception e) {
System.err.println("Failed to create order: " + e.getMessage());
}
}
}public class Main {
public static void main(String[] args) {
try {
String payHash = "abcd1234"; // Replace with actual pay_hash
String requestPath = "/api/order/" + payHash;
String response = MefreeApiClient.sendRequest("GET", requestPath);
System.out.println("Order Status: " + response);
} catch (Exception e) {
System.err.println("Failed to fetch order status: " + e.getMessage());
}
}
}