> For the complete documentation index, see [llms.txt](https://docs.expand.network/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.expand.network/integrations/derivatives/dydx-v4.md).

# dYdX V4

Additional functionalities available in expand.network SDK: onboarding user, place order, transfer, deposit, and cancel order.

### /getorders

Users can fetch details for all orders corresponding to a specified address and a specified sub-account number.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/getorders`

#### Query Parameters

| Name                                               | Type   | Description                                                             |
| -------------------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark>          | String | User's dydx address.                                                    |
| derivativeId                                       | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |
| subAccountNumber<mark style="color:red;">\*</mark> | String | Number of the sub account.                                              |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/getorders?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d&subAccountNumber=0
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
    "status": 200,
    "msg": "success",
    "data": {
      "orders": [
        {
            "id": "5724e441-654f-5dc6-ac02-e8e4a7ecda2e",
            "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
            "clientId": "3711471933",
            "clobPairId": "1",
            "side": "SELL",
            "size": "0.01",
            "totalFilled": "0",
            "price": "2600",
            "type": "LIMIT",
            "status": "CANCELED",
            "timeInForce": "GTT",
            "reduceOnly": false,
            "orderFlags": "64",
            "goodTilBlockTime": "2024-01-10T10:42:45.000Z",
            "createdAtHeight": "4993716",
            "clientMetadata": "0",
            "updatedAt": "2024-01-10T10:42:45.385Z",
            "updatedAtHeight": "4993921",
            "postOnly": true,
            "ticker": "ETH-USD"
          },
          {
            "id": "ff494eb3-d74e-5852-8aea-1ddabc4d9a37",
            "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
            "clientId": "1594632561",
            "clobPairId": "1",
            "side": "BUY",
            "size": "0.01",
            "totalFilled": "0.01",
            "price": "2600",
            "type": "LIMIT",
            "status": "FILLED",
            "timeInForce": "GTT",
            "reduceOnly": false,
            "orderFlags": "64",
            "goodTilBlockTime": "2024-01-10T10:31:42.000Z",
            "createdAtHeight": "4993244",
            "clientMetadata": "0",
            "updatedAt": "2024-01-10T10:26:43.386Z",
            "updatedAtHeight": "4993245",
            "postOnly": false,
            "ticker": "ETH-USD"
          }
      ]
    }
  }
```

{% endtab %}
{% endtabs %}

### /getorder

Users can fetch details for a specific order using the specified order id.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/getorder`

#### Query Parameters

| Name                                      | Type   | Description                                                             |
| ----------------------------------------- | ------ | ----------------------------------------------------------------------- |
| orderId<mark style="color:red;">\*</mark> | String | Order Id to fetch details of.                                           |
| derivativeId                              | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/getorder?derivativeId=1000&orderId=5724e441-654f-5dc6-ac02-e8e4a7ecda2e
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
  "status": 200,
  "msg": "success",
  "data": {
    "id": "5724e441-654f-5dc6-ac02-e8e4a7ecda2e",
    "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
    "clientId": "3711471933",
    "clobPairId": "1",
    "side": "SELL",
    "size": "0.01",
    "totalFilled": "0",
    "price": "2600",
    "type": "LIMIT",
    "status": "CANCELED",
    "timeInForce": "GTT",
    "reduceOnly": false,
    "orderFlags": "64",
    "goodTilBlockTime": "2024-01-10T10:42:45.000Z",
    "createdAtHeight": "4993716",
    "clientMetadata": "0",
    "updatedAt": "2024-01-10T10:42:45.385Z",
    "updatedAtHeight": "4993921",
    "postOnly": true,
    "ticker": "ETH-USD"
  }
}
```

{% endtab %}
{% endtabs %}

### /getfills

Users can fetch the fill details of the order corresponding to the specified address and the specified sub-account number.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/getfills`

#### Query Parameters

| Name                                               | Type   | Description                                                             |
| -------------------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark>          | String | User's dydx address.                                                    |
| derivativeId                                       | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |
| subAccountNumber<mark style="color:red;">\*</mark> | String | Number of the sub account.                                              |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/getfills?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d&subAccountNumber=0
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
    "status": 200,
        "msg": "success",
            "data": {
        "fills": [
            {
                "id": "025d1daa-3b12-5a65-a59a-fb57fefb6233",
                "side": "BUY",
                "liquidity": "TAKER",
                "type": "LIMIT",
                "market": "ETH-USD",
                "marketType": "PERPETUAL",
                "price": "2413.9",
                "size": "0.01",
                "fee": "0.01207",
                "createdAt": "2024-01-10T10:41:20.668Z",
                "createdAtHeight": "4993859",
                "orderId": "25432d45-2ac7-5476-991e-5c37a5d53cf7",
                "clientMetadata": "0"
            },
            {
                "id": "1017f858-a126-520e-bf3f-a579f5cc6c40",
                "side": "BUY",
                "liquidity": "TAKER",
                "type": "LIMIT",
                "market": "ETH-USD",
                "marketType": "PERPETUAL",
                "price": "2413.9",
                "size": "0.01",
                "fee": "0.01207",
                "createdAt": "2024-01-10T10:41:10.957Z",
                "createdAtHeight": "4993853",
                "orderId": "64900bdb-a4af-597f-897e-7d1ee7b3f9c5",
                "clientMetadata": "0"
            },
            {
                "id": "daae86a4-e125-5231-97e6-b1ea46174e3b",
                "side": "BUY",
                "liquidity": "TAKER",
                "type": "LIMIT",
                "market": "ETH-USD",
                "marketType": "PERPETUAL",
                "price": "2500",
                "size": "0.01",
                "fee": "0.0125",
                "createdAt": "2024-01-10T10:40:55.682Z",
                "createdAtHeight": "4993842",
                "orderId": "e2d931c4-7236-50dd-853e-72db64c7c67a",
                "clientMetadata": "0"
            }
        ]
    }
}
```

{% endtab %}
{% endtabs %}

### /getassets

Users can fetch a list of their assets deposited in dYdX corresponding to the specified address and the specified sub-account number.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/getassets`

#### Query Parameters

| Name                                               | Type   | Description                                                             |
| -------------------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark>          | String | User's dydx address.                                                    |
| derivativeId                                       | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |
| subAccountNumber<mark style="color:red;">\*</mark> | String | Number of the sub account.                                              |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/getassets?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d&subAccountNumber=0
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
  "status": 200,
  "msg": "success",
  "data": {
    "assets": [
      {
        "size": "189.229062",
        "symbol": "USDC",
        "side": "SHORT",
        "assetId": "0"
      }
    ]
  }
}
```

{% endtab %}
{% endtabs %}

### /gethistoricalpnl

Users can fetch the historical profit and loss corresponding to the specified address and the specified sub-account number.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/gethistoricalpnl`

#### Query Parameters

| Name                                               | Type   | Description                                                             |
| -------------------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark>          | String | User's dydx address.                                                    |
| derivativeId                                       | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |
| subAccountNumber<mark style="color:red;">\*</mark> | String | Number of the sub account.                                              |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/gethistoricalpnl?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d&subAccountNumber=0
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
    "status": 200,
        "msg": "success",
        "data": {
        "historicalPnL": [
            {
                "id": "4c3e3f94-ea28-5d34-8c60-95acc0e975fc",
                "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
                "equity": "509.314640",
                "totalPnl": "19.086934",
                "netTransfers": "0.000000",
                "createdAt": "2024-01-16T09:03:28.508Z",
                "blockHeight": "5355500",
                "blockTime": "2024-01-16T09:03:21.614Z"
            },
            {
                "id": "ee9d8bc8-af28-5f91-8edd-2982c6ff42ee",
                "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
                "equity": "509.314640",
                "totalPnl": "19.086934",
                "netTransfers": "0.000000",
                "createdAt": "2024-01-16T09:02:58.369Z",
                "blockHeight": "5355479",
                "blockTime": "2024-01-16T09:02:52.074Z"
            },
            {
                "id": "35420152-944c-5898-9465-42f4ec55bed4",
                "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
                "equity": "509.314640",
                "totalPnl": "19.086934",
                "netTransfers": "0.000000",
                "createdAt": "2024-01-16T09:02:28.204Z",
                "blockHeight": "5355457",
                "blockTime": "2024-01-16T09:02:21.962Z"
            },
            {
                "id": "b7b5f39a-47d4-55f2-b46f-48795dd83be9",
                "subaccountId": "8845f855-4943-5f72-9ad2-d0a2a416c5bd",
                "equity": "509.314640",
                "totalPnl": "19.086934",
                "netTransfers": "0.000000",
                "createdAt": "2024-01-16T09:01:58.165Z",
                "blockHeight": "5355434",
                "blockTime": "2024-01-16T09:01:51.006Z"
            }
        ]
    }
}
```

{% endtab %}
{% endtabs %}

### /getperpetualpositions

Users can fetch the position corresponding to the specified address and the specified sub-account number.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/getperpetualpositions`

#### Query Parameters

| Name                                               | Type   | Description                                                             |
| -------------------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark>          | String | User's dydx address.                                                    |
| derivativeId                                       | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |
| subAccountNumber<mark style="color:red;">\*</mark> | String | Number of the sub account.                                              |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/getperpetualpositions?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d&subAccountNumber=0
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
    "status": 200,
        "msg": "success",
        "data": {
        "positions": [
            {
                "market": "ETH-USD",
                "status": "OPEN",
                "side": "LONG",
                "size": "0.275",
                "maxSize": "0.275",
                "entryPrice": "2446.92727272727272727273",
                "exitPrice": null,
                "realizedPnl": "-0.30197",
                "unrealizedPnl": "24.92914112499999999999925",
                "createdAt": "2023-11-01T09:40:35.893Z",
                "createdAtHeight": "882343",
                "closedAt": null,
                "sumOpen": "0.275",
                "sumClose": "0",
                "netFunding": "-0.30197"
            },
            {
                "market": "ETH-USD",
                "status": "CLOSED",
                "side": "LONG",
                "size": "0",
                "maxSize": "0.01",
                "entryPrice": "1851.3",
                "exitPrice": "1849.8",
                "realizedPnl": "-0.015",
                "unrealizedPnl": "0",
                "createdAt": "2023-10-26T08:22:15.515Z",
                "createdAtHeight": "532122",
                "closedAt": "2023-10-26T08:22:23.632Z",
                "sumOpen": "0.01",
                "sumClose": "0.01",
                "netFunding": "0"
            },
            {
                "market": "ETH-USD",
                "status": "CLOSED",
                "side": "SHORT",
                "size": "0",
                "maxSize": "-0.005",
                "entryPrice": "1847.1",
                "exitPrice": "1851.2",
                "realizedPnl": "-0.0205",
                "unrealizedPnl": "0",
                "createdAt": "2023-10-26T07:52:16.125Z",
                "createdAtHeight": "531124",
                "closedAt": "2023-10-26T07:59:24.633Z",
                "sumOpen": "0.005",
                "sumClose": "0.005",
                "netFunding": "0"
            }
        ]
    }
}
```

{% endtab %}
{% endtabs %}

### /getsubaccounts

Users can fetch the details of a sub-account corresponding to the specified address.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/getsubaccounts`

#### Query Parameters

| Name                                      | Type   | Description                                                             |
| ----------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark> | String | User's dydx address.                                                    |
| derivativeId                              | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/getsubaccounts?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
  "status": 200,
  "msg": "success",
  "data": {
    "subaccounts": [
      {
        "address": "dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d",
        "subaccountNumber": 0,
        "equity": "507.190302025",
        "freeCollateral": "472.36933382375",
        "openPerpetualPositions": {
          "ETH-USD": {
            "market": "ETH-USD",
            "status": "OPEN",
            "side": "LONG",
            "size": "0.275",
            "maxSize": "0.275",
            "entryPrice": "2446.92727272727272727273",
            "exitPrice": null,
            "realizedPnl": "-0.30197",
            "unrealizedPnl": "23.51436402499999999999925",
            "createdAt": "2023-11-01T09:40:35.893Z",
            "createdAtHeight": "882343",
            "closedAt": null,
            "sumOpen": "0.275",
            "sumClose": "0",
            "netFunding": "-0.30197"
          }
        },
        "assetPositions": {
          "USDC": {
            "size": "189.229062",
            "symbol": "USDC",
            "side": "SHORT",
            "assetId": "0"
          }
        },
        "marginEnabled": true
      }
    ]
  }
}
```

{% endtab %}
{% endtabs %}

### /gettransfers

Users can fetch the details of the transfer transaction corresponding to the specified address and the specified sub-account number.

<mark style="color:blue;">`GET`</mark> `https://api.expand.network/derivative/gettransfers`

#### Query Parameters

| Name                                               | Type   | Description                                                             |
| -------------------------------------------------- | ------ | ----------------------------------------------------------------------- |
| address<mark style="color:red;">\*</mark>          | String | User's dydx address.                                                    |
| derivativeId                                       | String | Refer to the [Derivative Id](/ids/derivatives-ids.md) page for details. |
| subAccountNumber<mark style="color:red;">\*</mark> | String | Number of the sub account.                                              |

{% tabs %}
{% tab title="Sample Request" %}

```url
https://api.expand.network/derivative/gettransfers?derivativeId=1000&address=dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d&subAccountNumber=0
```

{% endtab %}

{% tab title="Sample Response" %}

```json
{
    "status": 200,
        "msg": "success",
        "data": {
        "transfers": [
            {
                "id": "a5a2714e-d9c9-5484-8bc4-8617b0c99954",
                "sender": {
                    "address": "dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d",
                    "subaccountNumber": 0
                },
                "recipient": {
                    "address": "dydx1eym66guf6xet6zl8c7h2h4c5hn4pglc75t2fem"
                },
                "size": "0.007239",
                "createdAt": "2024-01-15T15:42:48.451Z",
                "createdAtHeight": "5309897",
                "symbol": "USDC",
                "type": "WITHDRAWAL",
                "transactionHash": "B61FCF59F5AAE8E7F4D6C72D91B82050A825E41634DF457D9C2B90CDDCA20D2C"
            },
            {
                "id": "64b0c1f2-c8d8-529c-b2ee-f6a9617159b1",
                "sender": {
                    "address": "dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d",
                    "subaccountNumber": 0
                },
                "recipient": {
                    "address": "dydx150c8hssycjem2p9yfs9pa2xm4jjuvkn6t6w3v6"
                },
                "size": "0.007401",
                "createdAt": "2024-01-15T15:39:57.986Z",
                "createdAtHeight": "5309772",
                "symbol": "USDC",
                "type": "WITHDRAWAL",
                "transactionHash": "2F53FEAD71475DDA345AE2566851A6CEA10126047D0BDDC29D147DE47DCCDCB7"
            },
            {
                "id": "bee619f9-04bd-5756-b095-39a35078b6da",
                "sender": {
                    "address": "dydx13uxwm6gyyggdz92z47h0h2xdfqdfa0wrtykp2d",
                    "subaccountNumber": 0
                },
                "recipient": {
                    "address": "dydx1yt75rda3x7zxun8pcf3gtpt7mp5ultg0u9z43e"
                },
                "size": "0.00724",
                "createdAt": "2024-01-09T18:38:12.582Z",
                "createdAtHeight": "4954546",
                "symbol": "USDC",
                "type": "WITHDRAWAL",
                "transactionHash": "B8FCB72797B3C8772478A3B05D1DCE63A1BA96B99B359144A8DA8ADF2739EECE"
            }
        ]
    }
}
```

{% endtab %}
{% endtabs %}


---

# 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://docs.expand.network/integrations/derivatives/dydx-v4.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.
