Python Guide for Calling Mefree.NET API
Last updated
pip install requestsimport hmac
import base64
import hashlib
from datetime import datetime
def generate_signature(timestamp, method, request_path, secret_key):
"""
Generate HMAC-SHA256 signature.
Args:
timestamp (str): Current UTC timestamp in ISO 8601 format.
method (str): HTTP method (e.g., GET or POST).
request_path (str): API request path (with query parameters, if any).
secret_key (str): Your API Secret Key.
Returns:
str: Base64-encoded signature.
"""
message = f"{timestamp}{method}{request_path}"
signature = hmac.new(
secret_key.encode('utf-8'),
message.encode('utf-8'),
hashlib.sha256
).digest()
return base64.b64encode(signature).decode()
def get_utc_timestamp():
"""
Get the current UTC timestamp in ISO 8601 format.
Returns:
str: Timestamp in ISO 8601 format (e.g., 2024-11-26T12:34:56.789Z).
"""
return datetime.utcnow().isoformat(timespec='milliseconds') + 'Z'import requests
BASE_URL = "https://api.mefree.net"
API_KEY = "your_api_key" # Replace with your API Key
SECRET_KEY = "your_secret_key" # Replace with your Secret Key
def send_request(method, request_path):
"""
Send an HTTP request to the Mefree.NET API.
Args:
method (str): HTTP method (GET or POST).
request_path (str): API request path (with query parameters).
Returns:
dict: Parsed JSON response from the API.
"""
timestamp = get_utc_timestamp()
signature = generate_signature(timestamp, method, request_path, SECRET_KEY)
# Construct headers
headers = {
"Content-Type": "application/json",
"MF-ACCESS-KEY": API_KEY,
"MF-ACCESS-SIGN": signature,
"MF-ACCESS-TIMESTAMP": timestamp
}
# Send the request
url = BASE_URL + request_path
try:
if method == "GET":
response = requests.get(url, headers=headers)
elif method == "POST":
response = requests.post(url, headers=headers)
else:
raise ValueError("Unsupported HTTP method")
response.raise_for_status()
return response.json()
except requests.RequestException as e:
print(f"Request failed: {e}")
return Noneif __name__ == "__main__":
response = send_request("GET", "/api/config")
if response:
print("Account Information:", response)if __name__ == "__main__":
request_path = "/api/order?quantity=65000&target_address=TRON_ADDRESS&period=1"
response = send_request("POST", request_path)
if response:
print("Order Created:", response)if __name__ == "__main__":
pay_hash = "abcd1234" # Replace with actual pay_hash
request_path = f"/api/order/{pay_hash}"
response = send_request("GET", request_path)
if response:
print("Order Status:", response)