> For the complete documentation index, see [llms.txt](https://docs.carbondefi.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.carbondefi.xyz/developer-guides/carbon-defi-subgraph/subgraph-query-in-python.md).

# Subgraph Query in Python

The following example queries the Carbon DeFi Subgraph using Python.

#### 1. Copy the following code snippet into a Python development environment:

{% hint style="info" %}
Note that you must assign the value for **"GRAPH\_API\_KEY"** with your API key. This example acquires the key from an .env file. To get an API key, visit: <https://thegraph.com/studio/apikeys/>&#x20;
{% endhint %}

```python
import requests
import os

GRAPH_API_KEY = os.environ.get("GRT_API_KEY")

# function to use requests.post to make an API call to the subgraph url
def run_query(q):

    # endpoint where you are making the request
    request = requests.post(f'https://gateway-arbitrum.network.thegraph.com/api/{GRAPH_API_KEY}/subgraphs/id/3oYNFcwAGk5mVtzzMTDjanoMRsZRCGk88EFRn75dLRyp'
                            '',
                            json={'query': query})
    if request.status_code == 200:
        return request.json()
    else:
        raise Exception('Query failed. return code is {}.      {}'.format(request.status_code, query))


# The Graph query
query = """

{
  pairs {
    token0 {
      symbol
    }
    token1 {
      symbol
    }
  }
}
"""

result = run_query(query)

pairs = result['data']['pairs']

print(f"number of pairs from subgraph: {len(pairs)}")
print(f"first pair: {pairs[0]}")
```

#### 2. Run the code!

If successful, you will see a printout of:

* The number of pairs found in Carbon DeFi
* The token symbols of the first pair

The printout should look like:

`number of pairs from subgraph: 87`&#x20;

`first pair: {'token0': {'symbol': 'BIAO'}, 'token1': {'symbol': 'ETH'}}`


---

# 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.carbondefi.xyz/developer-guides/carbon-defi-subgraph/subgraph-query-in-python.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.
