Generate API keys for programmatic energy rentals
| Project Name | API Key | Secret | Created | Actions |
|---|
Select an API key and date to view orders placed that day.
Base URL: https://energyrental.io
X-Api-Key and X-Api-Secret
curl -X POST https://energyrental.io/energy/get-quote \
-H "Content-Type: application/json" \
-H "X-Api-Key: YOUR_KEY" \
-H "X-Api-Secret: YOUR_SECRET" \
-d '{"receiver":"TYourReceiverAddress","amount":65000,"duration":5}'
curl -X POST https://energyrental.io/energy/create-order \
-H "Content-Type: application/json" \
-H "X-Api-Key: YOUR_KEY" \
-H "X-Api-Secret: YOUR_SECRET" \
-d '{"receiver":"TYourReceiverAddress","amount":65000,"duration":5,"payment_txid":"your_txid_here"}'
curl -H "X-Api-Key: YOUR_KEY" -H "X-Api-Secret: YOUR_SECRET" \
"https://energyrental.io/energy/delegation-status?order_id=YOUR_ORDER_ID"
// energy-rental-order.js
// Complete working example for EnergyRental.io API
const TronWeb = require('tronweb').TronWeb;
// ==================== CONFIGURATION ====================
const TRONGRID_API_KEY = "YOUR_TRONGRID_API_KEY"; // Get free at https://www.trongrid.io/
const PRIVATE_KEY = "YOUR_WALLET_PRIVATE_KEY"; // Wallet that will pay for the energy
const API_KEY = "YOUR_API_KEY";
const API_SECRET = "YOUR_API_SECRET";
// Order settings
const ORDER = {
receiver: "Wallet-address", // ← Change to your receiving wallet
amount: 65000, // Minimum 65000 energy
duration: 5 // Fixed at 5 minutes
};
const BASE_URL = "https://energyrental.io";
// ======================================================
const tronWeb = new TronWeb({
fullHost: "https://api.trongrid.io",
headers: { "TRON-PRO-API-KEY": TRONGRID_API_KEY }
});
const headers = {
"Content-Type": "application/json",
"X-Api-Key": API_KEY,
"X-Api-Secret": API_SECRET
};
(async () => {
console.log("🚀 EnergyRental.io API Order Script");
console.log(`🎯 Receiver: ${ORDER.receiver}`);
console.log(`⚡ Energy: ${ORDER.amount} | Duration: ${ORDER.duration} min\n`);
try {
// Step 1: Get Quote
console.log("📡 Fetching quote...");
const quoteRes = await fetch(`${BASE_URL}/energy/get-quote`, {
method: "POST",
headers,
body: JSON.stringify(ORDER)
});
const quoteData = await quoteRes.json();
if (!quoteData.success) throw new Error(quoteData.error || "Quote failed");
const { price_trx, sun_required, payment_address } = quoteData.quote;
console.log(`💰 Price: ${price_trx} TRX (${sun_required} SUN)`);
console.log(`📍 Pay to: ${payment_address}\n`);
// Step 2: Build & sign transaction
console.log("🔑 Setting private key...");
tronWeb.setPrivateKey(PRIVATE_KEY);
const senderAddress = tronWeb.defaultAddress.base58;
if (!senderAddress) throw new Error("Failed to derive sender address from private key");
console.log(`🔑 Sender address: ${senderAddress}`);
const amountSun = Math.floor(price_trx * 1_000_000);
const unsignedTx = await tronWeb.transactionBuilder.sendTrx(payment_address, amountSun, senderAddress);
const signedTx = await tronWeb.trx.sign(unsignedTx);
const txid = signedTx.txID;
console.log(`✅ Transaction signed: ${txid}`);
// Step 3: Broadcast
console.log("📤 Broadcasting transaction...");
const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
if (!broadcast.result) throw new Error("Broadcast failed");
console.log(`📤 TX broadcasted: ${txid}\n`);
// Wait for confirmation
console.log("⏳ Waiting 8 seconds for confirmation...");
await new Promise(r => setTimeout(r, 8000));
// Step 4: Create Order
console.log("📡 Creating order...");
const orderRes = await fetch(`${BASE_URL}/energy/create-order`, {
method: "POST",
headers,
body: JSON.stringify({
receiver: ORDER.receiver,
amount: ORDER.amount,
duration: ORDER.duration,
payment_txid: txid
})
});
const orderData = await orderRes.json();
if (!orderData.success) throw new Error(orderData.error || "Order creation failed");
const orderId = orderData.order_id;
console.log(`✅ Order created! Order ID: ${orderId}\n`);
// Step 5: Poll status
console.log("⏳ Waiting for delegation...");
let attempts = 0;
const maxAttempts = 60;
const interval = setInterval(async () => {
attempts++;
try {
const statusRes = await fetch(`${BASE_URL}/energy/delegation-status?order_id=${orderId}`, {
headers
});
const status = await statusRes.json();
console.log(`[${attempts}] Status: ${status.status}`);
if (status.status === "delegated") {
console.log(`🎉 SUCCESS! Energy delegated → TxID: ${status.delegation_txid}`);
clearInterval(interval);
} else if (status.status === "failed" || status.status === "expired" || attempts >= maxAttempts) {
console.log(`❌ Failed or timed out`);
clearInterval(interval);
}
} catch (e) {
console.error("Status check error:", e.message);
}
}, 3000);
} catch (error) {
console.error("❌ Error:", error.message);
}
})();