> For the complete documentation index, see [llms.txt](https://spherex-4.gitbook.io/spherex/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://spherex-4.gitbook.io/spherex/for-devs/api.md).

# API

#### How to setup <a href="#how-to-setup" id="how-to-setup"></a>

#### What are the api services <a href="#what-are-the-api-services" id="what-are-the-api-services"></a>

#### How to add a service <a href="#how-to-add-a-service" id="how-to-add-a-service"></a>

#### What are needed before running <a href="#what-are-needed-before-running" id="what-are-needed-before-running"></a>

1. Creating a Virtual Environment

   ```
   sudo apt update
   sudo apt install python3.12-venv
   python3 -m venv /home/ubuntu/myenv
   ```
2. Automatically Activating the Virtual Environment

```
nano ~/.bashrc
source /home/ubuntu/myenv/bin/activate #add this code at the end of .bashrc file
source ~/.bashrc
```

3. Git Cloning the Repository under the Branch feat/standalone-signer

Ask the administrator for permission

<https://github.com/spherex-code/spherex-openapi-demo>

4. Initiating Setup Files in the Directory /spherex-openapi-demo

Copy

```
sudo -i 
pip install .
```

5. Running Demos in the Directory /spherex-openapi-demo/tests

Substituting Your Testnet or Mainnet gRPC Path in Demo Files(ask the administrator for grpc paths)

```
GrpcPrivate("spherex-testnet-grpc-gateway-ex-***************.elb.ap-southeast-1.amazonaws.com", 9001,) 
```

6. you can find all services

```
**grpcurl -plaintext spherex-testnet-grpc-gateway-ex-5802c5bf04e19798.elb.ap-southeast-1.amazonaws.com:9001 list**
grpcurl -plaintext [spherex-testnet-grpc-gateway-ex-5802c5bf04e19798.elb.ap-southeast-1.amazonaws.com:9001](<http://spherex-testnet-grpc-gateway-ex-5802c5bf04e19798.elb.ap-southeast-1.amazonaws.com:9001/>) describe spherex.trade.OrderServices 
```

7. There are 22 services, for example

```
spherex.quote.QuoteService
spherex.trade.OrderService
spherex.trade.TradeService
```

For example, OrderServices(you can create batch order and cancel orders using this service)

<https://github.com/spherex-code/spherex-protos/blob/main/protos/spherex/trade/order_rpc.proto>

Quote Services(you can find market data and account position using this service)

<https://github.com/spherex-code/spherex-protos/blob/main/protos/spherex/quote/quote_rpc.proto>

8. Adding a service

If you want to get open/active orders by account, you can first find the catagory(Account/Order/Quote/Trade etc.) and the service, then add the service.

```
# OrderService
rpc CreateOrderBatch ( .spherex.trade.CreateOrderBatchRequest ) returns ( .spherex.trade.CreateOrderBatchResponse ) {
    option (.spherex.common.rpc_method_ext_opt) = { is_write_operation:true };
  }
```

```
# /spherex-openapi-demo/spherex/grpc_private.py
def order_batch_create(self, **kwargs):
    resp = grpc_client.call_method("OrderService", "CreateOrderBatch", **kwargs)
    return resp
```

```
# /spherex-openapi-demo/spherex/order.py
class SpherexOrder:
    def __init__(self, grpc_client):
        self.grpc_client = grpc_client
		
    def create_batch_order(self, order_params_list):
		    ...
		    return create_order_batch_resp
```

**What needs to be prepared before running**

* VPN or A Server
* A permission for a github repo
* An valid grpc path with a whitelisted ip
* Account\_id/L2\_keys

**Batch order demo**

<figure><img src="/files/GG9JsKRPC4exktrBlHB8" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/9euhmjYJLCL9kDi9V1AO" alt=""><figcaption></figcaption></figure>

```
  order_params_list = []
  for order in orders:
      side = order['side']
      size = str(order['quantity'])
      price = str(order['price'])
      limit_fee = "0.0300000"
      type = "LIMIT"
      time_in_force = "GOOD_TIL_CANCEL"
      reduce_only = False
      is_position_tpsl = False
      is_set_open_tp = False
      is_set_open_sl = False
      contract_id = 10000001

      order_params = OrderParam(
          market,
          register_account_id,
          contract_id,
          side,
          size,
          price,
          limit_fee,
          position_id,
          synthetic_resolution,
          quote_resolution,
          synthetic_id,
          collateral_id,
          type,
          time_in_force,
          reduce_only,
          is_position_tpsl,
          is_set_open_tp,
          is_set_open_sl,
      )
      order_params_list.append(order_params)

  order_resp = self.spherexOrder.create_batch_order(order_params_list)
  print(order_resp)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://spherex-4.gitbook.io/spherex/for-devs/api.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
