# Get User account for Delegate

A Delegate is an address that can control an account on the user's behalf. The delegated user (public key) although has limited power, can perform most operations on the account except for withdrawals. To find delegated users on Drift Protocol, we need to query the <mark style="color:yellow;">delegate</mark> field in the <mark style="color:yellow;">drift\_User</mark> account.

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

#### Fetch user account for a particular delegate

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

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

async function getDataByGraphQl(delegateAddress) {
  //get user by delegated account
  const operationsDoc = `
      query MyQuery {
        drift_User(
          limit: 10
          where: {delegate: {_eq: ${JSON.stringify(delegateAddress)}}}
        ) {
          delegate
          authority
          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 getReserveCurrencyDetails(delegatePubkey) {
  const { errors, data } = await getDataByGraphQl(delegatePubkey);

  if (errors) {
    console.error(errors);
    console.log("Some Error Occured, please check your API key or try again");
  }

  console.dir(data, { depth: null });
}
getReserveCurrencyDetails("EBWSHvJmWkg1r2oUPDcKxfBxA1QG2fdZdAqH2EVNTzxs")
```

{% endcode %}
{% endtab %}

{% tab title="Response" %}

```json
{
  drift_User: [
    {
      delegate: "EBWSHvJmWkg1r2oUPDcKxfBxA1QG2fdZdAqH2EVNTzxs",
      authority: "AUYs3A9gMLn5tqySAQ4Sqea8PB5sWAXmgZGz8GBerYE3",
      pubkey: "6r1LX9SnSgG96vob5cyLDMRa2zzmVxR7WWVDCp2mYHmq"
    }
  ]
}
```

{% endtab %}
{% endtabs %}

The response contains `authority` and `delegate` which indicates the account owner and the delegated account, more fields can also be cherrypicked as per your requirement.

You can also query the <mark style="color:yellow;">delegate account</mark> for a particular user account or authority, using the following query

```json
query MyQuery {
  drift_User(
    where: {authority: {_eq: "AUYs3A9gMLn5tqySAQ4Sqea8PB5sWAXmgZGz8GBerYE3"}}
  ) { //replace with the user/authority wallet which you want to query
    status
    pubkey
    authority
    delegate
  }
}
```


---

# 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-user-account-for-delegate.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.
