# Getting OrderId and userOrderId

Orders are instructions which decide how trades should execute on a decentralized trading platform. The following snippet illustrates how we can query order data related to particular user account, based on a particular order Id.&#x20;

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

#### Fetch orders associated to a user account based orderId and userOrderId&#x20;

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

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

async function getOrderDataByGraphQl(authorityAddress) {
    const operationsDoc = `
          query MyQuery {
            drift_User(
                where: {authority: {_eq: ${JSON.stringify(authorityAddress)}}}
            ) {
                authority
                orders
                delegate
                hasOpenOrder
                subAccountId
                status
                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, orderId) {
    const { errors, data } = await getOrderDataByGraphQl(authorityPubkey);
    if (errors) {
      console.error(errors);
      console.log("Some Error Occured, please check your API key or try again");
    }
    
    let allMatchedOrder = [];
    for (let index = 0; index < data.drift_User.length; index++) {
        const currentUserDetails = data.drift_User[index];
        
        const matchedOrder = currentUserDetails.orders.find(
            (order) => order.orderId == orderId
        );
        
        if (!matchedOrder) {
            // console.log("Order not found");
            continue;
        }
        allMatchedOrder.push(matchedOrder);
    }
    console.log("Order found: ");
    console.dir(allMatchedOrder, { depth: null });
}
main("BgGAVukE1j8JDsvXwcnneuoN8LTpDuiRUtYfQWociQjL", 112)

```

{% endcode %}
{% endtab %}

{% tab title="Response" %}

```json
Order 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: {} }
  }
]
```

{% endtab %}
{% endtabs %}

In the above case, we first get the particular userAccount based on Authority (user wallet), and then filter the order field to get the order of a particular orderId. Alternatively, we can also fetch orders based on userOrderId, just by filter the order using the userOrderId field, instead of the orderId field.

```javascript
 const matchedOrder = currentUserDetails.orders.find(
     (order) => order.userorderId == orderId 
 ); //userOrderId to be taken as input from the user
```
