# API

**How to setup**

**What are the api services**

**How to add a service**

**What are needed before running**

1. Creating a Virtual Environment

   Copy

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

Copy

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

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

Ask the administrator for permission

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

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

Copy

Copy

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

1. 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)

Copy

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

1. you can find all services

Copy

```
**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 
```

1. There are 22 services, for example

Copy

```
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>

1. 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.

Copy

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

Copy

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

Copy

```
# /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**

Copy

<figure><img src="https://spherex-4.gitbook.io/~gitbook/image?url=https%3A%2F%2F4083394398-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F8BW04YjhIRbpRCXtor7k%252Fuploads%252FFfyW53QQ6qlmtXgIj6NL%252F20240923213817.png%3Falt%3Dmedia%26token%3D39a47bb5-5d52-40cc-8b51-a49ca17bf85e&#x26;width=768&#x26;dpr=4&#x26;quality=100&#x26;sign=b4ddd581&#x26;sv=2" alt=""><figcaption></figcaption></figure>

<figure><img src="https://spherex-4.gitbook.io/~gitbook/image?url=https%3A%2F%2F4083394398-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F8BW04YjhIRbpRCXtor7k%252Fuploads%252FXgTs8Wk5nHy6KDVd0MZE%252F20240923213828.png%3Falt%3Dmedia%26token%3D96cd8ab5-1f17-41c0-920e-909f37e46d08&#x26;width=768&#x26;dpr=4&#x26;quality=100&#x26;sign=1292a831&#x26;sv=2" 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)
```

[<br>](https://spherex-4.gitbook.io/spherex/mechanism/deflation)
