# The cURL flow covers the two TronSave API calls (estimate and create order).
# Signing the TRX payment transaction must be done with a TRON wallet/SDK and
# the resulting signed transaction passed into the second request as "signedTx".
# Step 1: Estimate the cost
curl -X POST https://api.tronsave.io/v2/estimate-buy-resource \
-H "content-type: application/json" \
-d '{
"resourceAmount": 32000,
"unitPrice": "MEDIUM",
"resourceType": "ENERGY",
"durationSec": 3600
}'
# Example response:
# {
# "error": false,
# "message": "Success",
# "data": {
# "unitPrice": 50,
# "durationSec": 259200,
# "estimateTrx": 7680000,
# "availableResource": 32000
# }
# }
# Step 2: Build and sign a sendTrx transaction to TRONSAVE_RECEIVER_ADDRESS
# (TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S on Mainnet) for "estimateTrx" SUN, using
# your wallet's private key. This produces the signedTx object.
# Step 3: Create the order with the signed transaction
curl -X POST https://api.tronsave.io/v2/buy-resource \
-H "content-type: application/json" \
-d '{
"resourceType": "ENERGY",
"resourceAmount": 32000,
"unitPrice": 50,
"allowPartialFill": true,
"receiver": "your_receiver_address",
"durationSec": 3600,
"signedTx": { "...": "the signed transaction object from step 2" }
}'
# Example response:
# {
# "error": false,
# "message": "Success",
# "data": {
# "orderId": "6809fdb7b9ba217a41d726fd"
# }
# }
// The Go example covers the two TronSave API calls. Signing the TRX payment
// transaction requires a TRON Go SDK (e.g. github.com/fbsobreira/gotron-sdk);
// pass the resulting signed transaction as the "signedTx" field below.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
TronsaveReceiverAddress = "TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S"
TronsaveAPIURL = "https://api.tronsave.io"
ResourceType = "ENERGY"
ReceiverAddress = "your_receiver_address"
BuyAmount = 32000
DurationSec = 3600
)
func postJSON(url string, body map[string]interface{}) (map[string]interface{}, error) {
payload, _ := json.Marshal(body)
resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
var out map[string]interface{}
if err := json.Unmarshal(data, &out); err != nil {
return nil, err
}
return out, nil
}
func getEstimate(resourceAmount, durationSec int) (map[string]interface{}, error) {
return postJSON(TronsaveAPIURL+"/v2/estimate-buy-resource", map[string]interface{}{
"resourceAmount": resourceAmount,
"unitPrice": "MEDIUM",
"resourceType": ResourceType,
"durationSec": durationSec,
})
}
func createOrder(resourceAmount int, signedTx interface{}, receiver string, unitPrice, durationSec float64) (map[string]interface{}, error) {
return postJSON(TronsaveAPIURL+"/v2/buy-resource", map[string]interface{}{
"resourceType": ResourceType,
"resourceAmount": resourceAmount,
"unitPrice": unitPrice,
"allowPartialFill": true,
"receiver": receiver,
"durationSec": durationSec,
"signedTx": signedTx,
})
}
func main() {
// Step 1: Estimate the cost
estimate, err := getEstimate(BuyAmount, DurationSec)
if err != nil {
panic(err)
}
if estimate["error"] == true {
panic(estimate["message"])
}
data := estimate["data"].(map[string]interface{})
unitPrice := data["unitPrice"].(float64)
durationSec := data["durationSec"].(float64)
availableResource := data["availableResource"].(float64)
// estimateTrx := data["estimateTrx"].(float64) // amount of SUN to sign in step 2
if availableResource >= BuyAmount {
// Step 2: Build and sign a sendTrx transaction with your private key,
// producing signedTx. (Use a TRON Go SDK.)
var signedTx interface{} // = your signed transaction object
// Step 3: Create the order
order, err := createOrder(BuyAmount, signedTx, ReceiverAddress, unitPrice, durationSec)
if err != nil {
panic(err)
}
fmt.Println(order)
}
}
// The Java example covers the two TronSave API calls. Signing the TRX payment
// transaction requires a TRON Java SDK (e.g. Trident); pass the resulting
// signed transaction as the "signedTx" field below.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class BuyResourceUsingPrivateKey {
static final String TRONSAVE_RECEIVER_ADDRESS = "TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S";
static final String TRONSAVE_API_URL = "https://api.tronsave.io";
static final String RESOURCE_TYPE = "ENERGY";
static final String RECEIVER_ADDRESS = "your_receiver_address";
static final int BUY_AMOUNT = 32000;
static final int DURATION_SEC = 3600;
static final HttpClient client = HttpClient.newHttpClient();
static String postJson(String url, String body) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("content-type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
static String getEstimate() throws Exception {
String body = String.format(
"{\"resourceAmount\":%d,\"unitPrice\":\"MEDIUM\",\"resourceType\":\"%s\",\"durationSec\":%d}",
BUY_AMOUNT, RESOURCE_TYPE, DURATION_SEC);
return postJson(TRONSAVE_API_URL + "/v2/estimate-buy-resource", body);
}
static String createOrder(String signedTxJson, long unitPrice, long durationSec) throws Exception {
String body = String.format(
"{\"resourceType\":\"%s\",\"resourceAmount\":%d,\"unitPrice\":%d,"
+ "\"allowPartialFill\":true,\"receiver\":\"%s\",\"durationSec\":%d,\"signedTx\":%s}",
RESOURCE_TYPE, BUY_AMOUNT, unitPrice, RECEIVER_ADDRESS, durationSec, signedTxJson);
return postJson(TRONSAVE_API_URL + "/v2/buy-resource", body);
}
public static void main(String[] args) throws Exception {
// Step 1: Estimate the cost
String estimate = getEstimate();
System.out.println(estimate);
// Parse estimate to read unitPrice, estimateTrx, durationSec, availableResource.
// Step 2: Build and sign a sendTrx transaction for estimateTrx SUN to
// TRONSAVE_RECEIVER_ADDRESS using your private key (use a TRON Java SDK),
// producing signedTxJson.
String signedTxJson = "{}"; // = your signed transaction object
// Step 3: Create the order (substitute parsed unitPrice and durationSec)
String order = createOrder(signedTxJson, 50L, 259200L);
System.out.println(order);
}
}
// The Rust example covers the two TronSave API calls. Signing the TRX payment
// transaction requires a TRON Rust library; pass the resulting signed
// transaction as the "signedTx" field below.
use serde_json::{json, Value};
const TRONSAVE_RECEIVER_ADDRESS: &str = "TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S";
const TRONSAVE_API_URL: &str = "https://api.tronsave.io";
const RESOURCE_TYPE: &str = "ENERGY";
const RECEIVER_ADDRESS: &str = "your_receiver_address";
const BUY_AMOUNT: i64 = 32000;
const DURATION_SEC: i64 = 3600;
async fn post_json(url: &str, body: Value) -> Result<Value, reqwest::Error> {
let client = reqwest::Client::new();
client.post(url).json(&body).send().await?.json::<Value>().await
}
async fn get_estimate() -> Result<Value, reqwest::Error> {
post_json(
&format!("{}/v2/estimate-buy-resource", TRONSAVE_API_URL),
json!({
"resourceAmount": BUY_AMOUNT,
"unitPrice": "MEDIUM",
"resourceType": RESOURCE_TYPE,
"durationSec": DURATION_SEC,
}),
)
.await
}
async fn create_order(signed_tx: Value, unit_price: i64, duration_sec: i64) -> Result<Value, reqwest::Error> {
post_json(
&format!("{}/v2/buy-resource", TRONSAVE_API_URL),
json!({
"resourceType": RESOURCE_TYPE,
"resourceAmount": BUY_AMOUNT,
"unitPrice": unit_price,
"allowPartialFill": true,
"receiver": RECEIVER_ADDRESS,
"durationSec": duration_sec,
"signedTx": signed_tx,
}),
)
.await
}
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
// Step 1: Estimate the cost
let estimate = get_estimate().await?;
println!("{estimate}");
let data = &estimate["data"];
let unit_price = data["unitPrice"].as_i64().unwrap();
let duration_sec = data["durationSec"].as_i64().unwrap();
let available_resource = data["availableResource"].as_i64().unwrap();
// let estimate_trx = data["estimateTrx"].as_i64().unwrap(); // SUN to sign in step 2
if available_resource >= BUY_AMOUNT {
// Step 2: Build and sign a sendTrx transaction to TRONSAVE_RECEIVER_ADDRESS
// for estimate_trx SUN using your private key, producing signed_tx.
let signed_tx = json!({}); // = your signed transaction object
// Step 3: Create the order
let order = create_order(signed_tx, unit_price, duration_sec).await?;
println!("{order}");
}
Ok(())
}