# Get OpenOrders for a User Account

Open orders are pending buy or sell orders that a trader has placed on the platform's perpetual futures market but haven't been filled. We can get openOrders for a particular user Account, by querying the orders field, and checking orders with status "open".

You can directly copy paste this code on <mark style="color:yellow;">replit</mark> and see it in action.

#### Fetch openOrders associated to a user account

{% tabs %}
{% tab title="Code Snippet" %}
{% code overflow="wrap" %}

```javascript
const SHYFT_API_KEY = "YOUR-API-KEY";

async function getOpenOrdersByGraphQl(authorityAddress) {
    const operationsDoc = `
          query MyQuery {
            drift_User(
                where: {authority: {_eq: ${JSON.stringify(authorityAddress)}}}
            ) {
                authority
                orders
                hasOpenOrder
                pubkey
            }
          }
        `; //graphQl query
    const result = await fetch(
      `https://programs.shyft.to/v0/graphql/accounts?api_key=${SHYFT_API_KEY}&network=mainnet-beta`, //SHYFT's GQL endpoint
      {
        method: "POST",
        body: JSON.stringify({
          query: operationsDoc,
          variables: {},
          operationName: "MyQuery",
        }),
      }
    );
  
    return await result.json();
  }
  async function main(authorityPubkey) {
    const { errors, data } = await getOpenOrdersByGraphQl(authorityPubkey);
    if (errors) {
      console.error(errors);
      console.log("Some Error Occured, please check your API key or try again");
    }
    
    let allOpenOrder = [];
    for (let index = 0; index < data.drift_User.length; index++) {
        const currentUserDetails = data.drift_User[index];
        
        const openOrder = currentUserDetails.orders.find(
            (order) => Object.keys(order.status)[0] == 'open'
        );
        
        if (!openOrder) {
            // console.log("Order not found");
            continue;
        }
        allOpenOrder.push(openOrder);
    }
    console.log("Open Orders found: ");
    console.dir(allOpenOrder, { depth: null });
}
main("BgGAVukE1j8JDsvXwcnneuoN8LTpDuiRUtYfQWociQjL")
```

{% endcode %}
{% endtab %}

{% tab title="Response" %}

```json
Open Orders found:
[
  {
    slot: '289092347',
    maxTs: '0',
    price: '0',
    status: { open: {} },
    orderId: 112,
    padding: [ 0, 0, 0 ],
    postOnly: false,
    direction: { long: {} },
    orderType: { triggerMarket: {} },
    marketType: { perp: {} },
    reduceOnly: true,
    marketIndex: 2,
    userOrderId: 0,
    triggerPrice: '2404000000',
    auctionDuration: 0,
    auctionEndPrice: '0',
    baseAssetAmount: '1500000000',
    triggerCondition: { above: {} },
    auctionStartPrice: '0',
    immediateOrCancel: false,
    oraclePriceOffset: 0,
    baseAssetAmountFilled: '0',
    quoteAssetAmountFilled: '0',
    existingPositionDirection: { short: {} }
  },
  {
    slot: '289284312',
    maxTs: '0',
    price: '131090000',
    status: { open: {} },
    orderId: 363,
    padding: [ 0, 0, 0 ],
    postOnly: false,
    direction: { long: {} },
    orderType: { limit: {} },
    marketType: { perp: {} },
    reduceOnly: false,
    marketIndex: 0,
    userOrderId: 0,
    triggerPrice: '0',
    auctionDuration: 0,
    auctionEndPrice: '0',
    baseAssetAmount: '50000000000',
    triggerCondition: { above: {} },
    auctionStartPrice: '0',
    immediateOrCancel: false,
    oraclePriceOffset: 0,
    baseAssetAmountFilled: '0',
    quoteAssetAmountFilled: '0',
    existingPositionDirection: { long: {} }
  }
]
```

{% endtab %}
{% endtabs %}


---

# 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.shyft.to/solana-indexers/case-studies/drift/get-openorders-for-a-user-account.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.
