# COUNT\_INSTRUCTIONS

## Overview

*This method allows you to aggregate and count instructions with a robust filter object. There are many types of filters you can apply and you can find information regarding those in the* [*Filters*](/carpool/api-reference/filters.md) *section of the docs. The endpoint example here shows off one of the many filters.*

{% openapi src="/files/A7WYSpafXTDWWRbIhWib" path="/query/solana" method="post" %}
[Carpool\_dev-COUNT\_INSTRUCTIONS-0.1-resolved.json](https://2513171703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE1gzfwcjB542lI2VR9XI%2Fuploads%2FRJrkD8Qkan4klelcorSX%2FCarpool_dev-COUNT_INSTRUCTIONS-0.1-resolved.json?alt=media\&token=9a97dc5c-287c-4dfe-84a2-584257de53d7)
{% endopenapi %}

## Filters

As mentioned above, the api definition here illustrates one of Carpool's many filter types. All queries that accept a filter object in the POST body can make use of **any** of the potential filters. Please see the Instruction Filters section of the documentation to better understand how they are implemented for instruction queries.

{% content-ref url="/pages/uYgjTokygYFjAlLVuwUH" %}
[Instruction Filters](/carpool/api-reference/filters/instruction-filters.md)
{% endcontent-ref %}

## Example - Basic

**Program** - *Cypher Protocol*

**Description** *- Count all failed instructions that have an error code of 7.*

#### Javascript

```javascript
var myHeaders = new Headers();
myHeaders.append("x-api-key", "<API_KEY>");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  "type": "COUNT_INSTRUCTIONS",
  "query": {
    "programId": "CYPH3o83JX6jY6NkbproSpdmQ5VWJtxjfJ5P8veyYVu3",
    "filter": {
      "type": "INSTRUCTION_ERROR_CODE",
      "errorCode": 7
    },
    "failures": true
  }
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://mainnet.carpool.dev/query/solana", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
```

#### cURL

```
curl --location 'https://mainnet.carpool.dev/query/solana' \
--header 'x-api-key: <API_KEY>' \
--header 'Content-Type: application/json' \
--data '{
    "type": "COUNT_INSTRUCTIONS",
    "query": {
        "programId": "CYPH3o83JX6jY6NkbproSpdmQ5VWJtxjfJ5P8veyYVu3",
        "filter": {
            "type": "INSTRUCTION_ERROR_CODE",
            "errorCode": 7
        },
        "failures": true
    }
}'
```

## Example - Advanced

**Program** - *Candy Machine*

**Description** *- Count all instructions that satisfy both filter conditions: The instructions must have an argument at path data.creators.address and the percentage share should be set to 100.*

#### Javascript

```javascript
var myHeaders = new Headers();
myHeaders.append("x-api-key", "<API_KEY>");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  "type": "COUNT_INSTRUCTIONS",
  "query": {
    "programId": "CndyV3LdqHUfDLmE5naZjVN8rBZz4tqhdefbAnjHG3JR",
    "filter": {
      "and": [
        {
          "type": "INSTRUCTION_ARG_EXISTS",
          "path": "data.creators.address"
        },
        {
          "type": "INSTRUCTION_ARG_NUMERIC",
          "path": "data.creators.percentageShare",
          "range": {
            "eq": 100
          }
        }
      ]
    },
    "timeRange": {
      "before": "2023-07-12T22:53:09+0000",
      "after": "2023-07-11T22:53:09+0000"
    },
    "failures": false
  }
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://mainnet.carpool.dev/query/solana", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
```

#### cURL

```
curl --location 'https://mainnet.carpool.dev/query/solana' \
--header 'x-api-key: <API_KEY>' \
--header 'Content-Type: application/json' \
--data '{
    "type": "COUNT_INSTRUCTIONS",
    "query": {
        "programId": "CndyV3LdqHUfDLmE5naZjVN8rBZz4tqhdefbAnjHG3JR",
        "filter": {
            "and": [
                {
                    "type": "INSTRUCTION_ARG_EXISTS",
                    "path": "data.creators.address"
                },
                {
                    "type": "INSTRUCTION_ARG_NUMERIC",
                    "path": "data.creators.percentageShare",
                    "range": {
                        "eq": 100
                    }
                }
            ]
        },
        "timeRange": {
            "before": "2023-07-12T22:53:09+0000",
            "after": "2023-07-11T22:53:09+0000"
        },
        "failures": false
    }
}'
```


---

# 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://carpool.gitbook.io/carpool/api-reference/instructions/count_instructions.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.
