> For the complete documentation index, see [llms.txt](https://docs.hatsprotocol.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hatsprotocol.xyz/for-developers/v1-sdk/subgraph/fetching-hats.md).

# Fetching Hats

### <mark style="color:purple;">getHat</mark>

Get a Hat by its ID.

```typescript
const hat = await hatsSubgraphClient.getHat({
    chainId,
    hatId,
    props,
});
```

***Arguments***:

```typescript
{
    chainId: number;
    hatId: bigint;
    props: HatPropsConfig;
}
```

* `chainId` - ID of the chain to fetch from.
* `hatId` - ID of the Hat to fetch.
* `props` - Hat's properties to fetch, including the ones of nested objects. Check the [HatPropsConfig](/for-developers/v1-sdk/subgraph/types.md#hatpropsconfig) type for the available properties and query filters.

***Response***:

```typescript
Hat
```

A [Hat object](/for-developers/v1-sdk/subgraph/types.md#hat), containing the chosen properties.

***Example***:

```typescript
const res = await client.getHat({
  chainId: 10, // optimism
  hatId: BigInt(
    "0x0000000100020001000100000000000000000000000000000000000000000000"
  ),
  props: {
    maxSupply: true, // get the maximum amount of wearers for the hat 
    wearers: { // get the hat's wearers 
      props: {}, // for each wearer, include only its ID (address)
    },
    events: { // get the hat's events
      props: {
        transactionID: true, // for each event, include the transaction ID
      },
      filters: {
        first: 10, // fetch only the latest 10 events
      },
    },
  },
});
```

### <mark style="color:purple;">getHatsByIds</mark>

Get Hats by their IDs.

<pre class="language-typescript"><code class="lang-typescript">const hats = await hatsSubgraphClient.getHatsByIds({
<strong>    chainId,
</strong>    hatIds,
    props,
});
</code></pre>

***Arguments***:

```typescript
{
    chainId: number;
    hatIds: bigint[];
    props: HatPropsConfig;
}
```

* `chainId` - ID of the chain to fetch from.
* `hatIds` - IDs of the Hats to fetch.
* `props` - Hat's properties to fetch, including the ones of nested objects. Check the [HatPropsConfig](/for-developers/v1-sdk/subgraph/types.md#hatpropsconfig) type for the available properties and query filters.

***Response***:

```typescript
Hat[]
```

An array of [Hat objects](/for-developers/v1-sdk/subgraph/types.md#hat), containing the chosen properties.

***Example***:

```typescript
const res = await client.getHatsByIds({
  chainId: 10, // optimism
  hatIds: [
    BigInt("0x0000000100020001000100000000000000000000000000000000000000000000"),
    BigInt("0x0000000100020001000000000000000000000000000000000000000000000000"),
  ],
  props: {
    wearers: { // get each hat's wearers
      props: { 
        currentHats: { // for each wearer, get its hats
          props: {}, // for each hat, include only its ID
        },
      },
    },
  },
});
```


---

# 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.hatsprotocol.xyz/for-developers/v1-sdk/subgraph/fetching-hats.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.
