# Fetching Trees

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

Get a Tree by its ID.

```typescript
const tree = await hatsSubgraphClient.getTree({
    chainId,
    treeId,
    props,
});
```

***Arguments***:

```typescript
{
    chainId: number;
    treeId: number;
    props: TreePropsConfig;
}
```

* `chainId` - ID of the chain to fetch from.
* `treeId` - ID of the Tree to fetch (Tree's top-hat domain - first 4 bytes of the top-hat ID).
* `props` - Tree's properties to fetch, including the ones of nested objects. Check the [TreePropsConfig](/for-developers/v1-sdk/subgraph/types.md#treepropsconfig) type for the available properties and query filters.

***Response***:

```typescript
Tree
```

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

***Example:***

```typescript
// get a tree's top-hat
const res = await client.getTree({
  chainId: 10, // optimism
  treeId: 1, 
  props: {
    hats: { // get the tree's hats
      props: {}, // for each hat, include only its ID
      filters: { first: 1 }, // fetch only the first hat (the top-hat)
    },
  },
});
```

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

Get Trees by their IDs.

```typescript
const trees = await hatsSubgraphClient.getTreesByIds({
    chainId,
    treeIds,
    props,
});
```

***Arguments***:

```typescript
{
    chainId: number;
    treeIds: number[];
    props: TreePropsConfig;
}
```

* `chainId` - ID of the chain to fetch from.
* `treeIds` - IDs of the Trees to fetch (Tree's top-hat domain - first 4 bytes of the top-hat ID).
* `props` - Tree's properties to fetch, including the ones of nested objects. Check the [TreePropsConfig](/for-developers/v1-sdk/subgraph/types.md#treepropsconfig) type for the available properties and query filters.

***Response***:

```typescript
Tree[]
```

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

***Example:***&#x20;

```typescript
const res = await client.getTreesByIds({
  chainId: 10, // optimism
  treeIds: [1, 2],
  props: {
    hats: { // for each tree, fetch its hats
      props: {
        status: true, // for each hat, include its status (active/inactive)
      },
      filters: { first: 200 }, // fetch only the first 200 hats
    },
  },
});
```

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

Paginate over Trees.

```typescript
const trees = await hatsSubgraphClient.getTreesPaginated({
    chainId,
    props,
    page,
    perPage,
});
```

***Arguments***:

```typescript
{
    chainId: number;
    props: TreePropsConfig;
    page: number;
    perPage: number;
}
```

* `chainId` - ID of the chain to fetch from.
* `props` - Tree's properties to fetch, including the ones of nested objects. Check the [TreePropsConfig](/for-developers/v1-sdk/subgraph/types.md#treepropsconfig) type for the available properties and query filters.
* page - Number of page to fetch.
* perPage - Number of Trees to fetch in each page.

***Response***:

```typescript
Tree[]
```

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

***Example:***

```typescript
const res = await client.getTreesPaginated({
  chainId: 10, // optimism
  props: {
    hats: { // for each tree, get its hats
      props: {}, // for each hat, include only its ID 
      filters: { first: 1 }, // fetch only the first hat of every tree (the top-hat)
    },
  },
  page: 3, // get the third page
  perPage: 10, // each page contains 10 trees
});
```


---

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