# Create Order

{% openapi src="/files/VOZ0twanUuUSjlUllBM0" path="/api/order" method="post" %}
[api.json](https://3248898104-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLIQdCqMpkUtQ2eL2t8un%2Fuploads%2Fgit-blob-3ff62faf23c66ad0a12c79271d9e9b9610805c52%2Fapi.json?alt=media)
{% endopenapi %}

#### **Core API: Create Order (POST /order)**

When creating an order through Mefree.net's API, it is crucial to track the following three return values:

1. **`pay_hash` (Payment Hash / Order ID):**
   * This is a unique identifier for the order.
   * Use it to query the order details via the **GET `/order/{pay_hash}`** endpoint.
2. **`status` (Order Status):**
   * Indicates the progress of the order.
   * When the `status` is `DELEGATE_SUCCESS`, it means the energy transaction has been successfully submitted to the TRON blockchain.
   * Note: A very small chance (\~0.1%) exists where the transaction may not be added to the chain.
3. **`confirm_status` (On-chain Confirmation Status):**
   * Confirms whether the energy has been successfully transferred on-chain.
   * When `confirm_status` is `DELEGATION_CONFIRMED`, it guarantees that the energy has been delivered to the target address.

***

#### **Q: How to ensure that energy is sent to the address?**

There are **two ways** to verify successful energy delivery:

1. **Query the energy balance of the address via the TRON API:**
   * Use TRON's official API or a TRON wallet (e.g., TronLink) to check the target address's current energy balance.
   * Endpoint Example (TRON Grid API):

     ```bash
     curl --request POST \
          --url https://api.trongrid.io/wallet/getaccountresource \
          --header 'accept: application/json' \
          --header 'content-type: application/json' \
          --data '
     {
       "address": "TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g",
       "visible": true
     }
     '
     ```

     Parse the response to check the `energy_limit` or `energy_used` field.
2. **Check the `confirm_status` of the order:**
   * Use the **GET `/order/{pay_hash}`** endpoint to retrieve the order details.
   * If `confirm_status` is `DELEGATION_CONFIRMED`, the energy transaction has been confirmed on the blockchain, and the target address has received the energy.

***

#### **Example Workflow**

**1. Create an Order**

Request:

```http
POST /order?quantity=65000&target_address=TRON_ADDRESS&period=1
Host: https://api.mefree.net
Headers: 
  Content-Type: application/json
  MF-ACCESS-KEY: {api_key}
  MF-ACCESS-SIGN: {signature}
  MF-ACCESS-TIMESTAMP: {timestamp}


```

Response:

```json
{
  "pay_hash": "abcd1234",
  "status": "DELEGATE_SUCCESS",
  "confirm_status": "UNCONFIRMED"
}
```

**2. Query Order Details**

Request:

```http
GET /order/abcd1234
Host: https://api.mefree.net
Headers: 
  Content-Type: application/json
  MF-ACCESS-KEY: {api_key}
  MF-ACCESS-SIGN: {signature}
  MF-ACCESS-TIMESTAMP: {timestamp}
```

Response:

```json
{
  "pay_hash": "abcd1234",
  "status": "DELEGATE_SUCCESS",
  "confirm_status": "DELEGATION_CONFIRMED"
}
```

**3. Verify Energy Delivery**

* If `confirm_status` = `DELEGATION_CONFIRMED`, the energy has been delivered.
* Alternatively, check the energy balance of the receiver address via TRON’s blockchain API.

***

By combining order tracking (`pay_hash` and `confirm_status`) and on-chain verification, you can ensure that energy transactions are reliable and properly executed. If any issues arise, contact Mefree support([@mefreenet](https://t.me/mefreenet)) for assistance.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mefree.net/api/create-order.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
