Tronsave
🇬🇧 English
🇬🇧 English
  • 📗INTRODUCTION
    • What is Tronsave ?
    • Why Tronsave?
  • Buyer
    • How to buy Energy ?
      • Buy on the Website
      • Buy on Telegram
        • 1. Create a Tronsave telegram account
        • 2. How To Deposit TRX
        • 3. Get the Tronsave API Key
        • 4. How to buy on Telegram
    • Extend
      • Quick extend
      • Advance
  • 🏬Seller
    • Get Energy by Staking 2.0
    • Permission
    • Sell Energy
      • Manual Sell
      • Auto Sell
      • Sell Suggestion
  • DEVELOPER
    • Get API key
      • On the Website
      • On Telegram
    • Buy Resources (v2)
      • Use Signed Transaction
        • Estimate TRX
        • Get Signed Transaction
        • Create order
      • Use API Key
        • Get Internal Account Info
        • Get Order Book
        • Estimate TRX
        • Create order
        • Get one order details
        • Get Internal Account Order History
    • Extend Orders (v2)
      • Step 1: Get Extendable Delegates
      • Step 2: Extend Request
    • Rest API v0
      • Buy on Rest API
        • Use Signed Transaction
          • Estimate TRX
          • Get Signed Transaction
          • Create order
          • Demo
        • Use API Key
          • Get Internal Account Info
          • Get Order Book
          • Estimate of TRX
          • Create order
          • Get one order details
          • Get Internal Account Order History
      • Extend with Rest API
  • 🤝Referrer
    • Referrals
  • 💡FAQ
    • Questions for Energy market
    • Calculate APY in TronSave
    • How to connect wallet in Tronsave?
    • Team of Service
  • 👨‍💻Full Code Example
    • Code Example (v2)
      • Buy Resource by API using private key
      • Buy Resources by API using api key
      • Extend order by API using api key
      • Extend order by API using private key
    • Code Example (v0)
      • Buy energy by API using private key
      • Buy energy by API using api key
      • Extend order by API using api key
Powered by GitBook
On this page
  1. DEVELOPER
  2. Buy Resources (v2)
  3. Use API Key

Create order

PreviousEstimate TRXNextGet one order details

Last updated 1 day ago

To use this feature, you must have the API key. Read to view how to get our API key

Buy with API Key (Postman)

Create a new order by API key

POST https://api.tronsave.io/v2/buy-resource

Create a new buy energy order by

Rate limit: 15 requests per 1 second

Headers

Name
Type
Description

apikey*

String

Tronsave api key that present for internal account

Request Body

Field
Type
Description

resourceType

String

"ENERGY" or "BANDWIDTH", default: ENERGY

unitPrice

Number, String

"FAST", "MEDIUM", "SLOW", or a number. Default: "MEDIUM"

-"FAST": If the market is ready to fill = 100%, FAST = MEDIUM. If the market is ready to fill < 100%, FAST = MEDIUM + 10. If market ready to fill = 0%, FAST = SLOW + 20.

-"MEDIUM": The lowest price for the maximum market fill for this order. If market is ready to fill = 0%, MEDIUM = SLOW + 10.

-"SLOW": The lowest price that can be set for this order.

-If the price is a number, the price unit is equal to SUN

resourceAmount*

Number

The number of resources.

receiver*

String

Resource receiving address

durationSec

Number

The duration of the bought resource, time unit, is in seconds. Default: 259200 (3 days).

sponsor

String

sponsor code

options

Object

optional

options.allowPartialFill

Boolean

Allow the order to be filled partially or not

options.onlyCreateWhenFulfilled

Boolean

[true]=> order only creates when it can be fulfilled

[false]=> order will create even if it can not be fulfilled

Default value: false

options.maxPriceAccepted

Number

Only create order when the estimate price less than this value.

options.preventDuplicateIncompleteOrders

Boolean

[true]=> Only create if no uncompleted order with the same parameters exists.

[false]=> Always create a new order, regardless of existing unfinished ones.

Default value: false

options.minResourceDelegateRequiredAmount

Number

The minimum energy amount delegated by a single provider.

Return the order ID if successful

{
    "error": false,
    "message": "Success",
    "data": {
        "orderId": "6818426a65fa8ea36d119d2c"
    }
}
{
    "MISSING_PARAMS": "Missing some params in body",
    "INVALID_PARAMS": "Some params are invalid",
    "MIN_PRICE_INVALID": "minPrice is less than the system's minimum price or not in the correct format."
    "INTERNAL_ACCOUNT_NOT_FOUND": "internal account does not exist",
    "INTERNAL_BALANCE_ACCOUNT_TOO_LOW": "Balance is not enough",
    "CANNOT_FULFILLED": "The order requires an immediate full match, but the system cannot fulfill 100% of it.",
    "MUST_BE_WAIT_PREVIOUS_ORDER_FILLED": "A pending order with the same parameters already exists in the system.",
    "PRICE_EXCEED_MAX_PRICE_REQUIRED": "The price of the order exceeds the maximum price accepted."
}
{
    "API_KEY_REQUIRED": "Missing api key in headers",
    "INVALID_API_KEY": "api key not correct"
}
{
    "RATE_LIMIT": "Rate limit reached",
}

Example

{
    "resourceType": "ENERGY",
    "receiver": "TFFbwz3UpmgaPT4UudwsxbiJf63t777777",
    "durationSec": 3600,
    "resourceAmount": 32000,
    "unitPrice": "MEDIUM",
    "options": {
        "allowPartialFill": true,
        "onlyCreateWhenFulfilled": true,
        "preventDuplicateIncompleteOrders": false,
        "maxPriceAccepted": 100,
        "minResourceDelegateRequiredAmount": 100000
    }
}
{
  "apikey": <YOUR_API_KEY>
}
{
    "error": false,
    "message": "Success",
    "data": {
        "orderId": "6818426a65fa8ea36d119d2c"
    }
}

Example Code

const BuyResource = async (apiKey, receiverAddress, resourceAmount, durationSec, maxPriceAccepted) => {
    const url = `${TRONSAVE_API_URL}/v2/buy-resource`
    const body = {
        resourceType: RESOURCE_TYPE, //ENERGY or BANDWIDTH
        unitPrice: "MEDIUM", //price in SUN or "SLOW"|"MEDIUM"|"FAST"
        resourceAmount, //Amount of resource wanted to buy
        receiver: receiverAddress,
        durationSec, //order duration in sec. Default: 259200 (3 days)
        options: {
            allowPartialFill: true,
            onlyCreateWhenFulfilled: false,
            maxPriceAccepted
        }
    }
    const data = await fetch(url, {
        method: "POST",
        headers: {
            'apikey': apiKey,
            "content-type": "application/json",
        },
        body: JSON.stringify(body)
    })
    const response = await data.json()
    /**
     * Example response
     * {
     *     "error": false,
     *     "message": "Success",
     *     "data": {
     *         "orderId": "6809fdb7b9...a41d726fd"
     *     }
     * }
     */
    return response
}
function buyResource(int $resourceAmount, int $durationSec, int $maxPriceAccepted): array {
    $url = TRONSAVE_API_URL . "/v2/buy-resource";
    $body = [
        'resourceType' => RESOURCE_TYPE,
        'unitPrice' => "MEDIUM",
        'resourceAmount' => $resourceAmount,
        'receiver' => RECEIVER_ADDRESS,
        'durationSec' => $durationSec,
        'options' => [
            'allowPartialFill' => true,
            'onlyCreateWhenFulfilled' => false,
            'maxPriceAccepted' => $maxPriceAccepted
        ]
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'apikey: '. API_KEY,
        'Content-Type: application/json'
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}
def buy_resource(resourceAmount: int, duration_sec: int, max_price_accepted: int) -> Dict[str, Any]:
    """Buy resource"""
    url = f"{TRONSAVE_API_URL}/v2/buy-resource"
    headers = {
        'apikey': API_KEY,
        'Content-Type': 'application/json'
    }
    
    body = {
        'resourceType': RESOURCE_TYPE,
        'unitPrice': "MEDIUM",
        'resourceAmount': resourceAmount,
        'receiver': RECEIVER_ADDRESS,
        'durationSec': duration_sec,
        'options': {
            'allowPartialFill': True,
            'onlyCreateWhenFulfilled': False,
            'maxPriceAccepted': max_price_accepted
        }
    }
    
    response = requests.post(url, headers=headers, json=body)
    return response.json()

Get detailed orders

here
API key
here