> For the complete documentation index, see [llms.txt](https://intrinsic-1.gitbook.io/axiomv2-sdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://intrinsic-1.gitbook.io/axiomv2-sdk/sdk-and-repl-reference/axiom-sdk-reference/querybuilderv2.md).

# QueryBuilderV2

The Axiom SDK is an npm library with a collection of APIs and query tools.

To install the package into your dApp as a project dependency, use your favorite package manager (`npm`, `yarn`, or `pnpm`)

{% code title="npm" %}

```bash
npm i @axiom-crypto/core
```

{% endcode %}

## **Building and Submitting Queries**

The `QueryBuilderV2` class which helps users construct queries in the [Axiom Query Format](broken://pages/VPw6bPmoS0ZqscvFySw0) and submit them on-chain. An instance of `QueryBuilderV2` is created by the following call:

```typescript
import { Axiom, QueryV2 } from "@axiom-crypto/core";

const axiom = new Axiom({
  providerUri: process.env.PROVIDER_URI_GOERLI as string,
  privateKey: process.env.PRIVATE_KEY_GOERLI as string,
  version: "v2",
  chainId: 5, // Goerli
  mock: true, // generate Mock proofs for faster development
});
const query = (axiom.query as QueryV2).new();
```

#### **setDataQuery**

`setDataQuery(dataQuery: UnbuiltSubquery[])`

Sets the dataQuery to an array of `UnbuiltSubquery` objects.

#### **setBuiltDataQuery**

`setBuiltDataQuery(dataQuery: AxiomV2DataQuery): void`

Appends an already-built DataQuery. This is used when receiving a DataQuery from a ComputeQuery. Setting this will take precedence over setting any UnbuiltSubqueries via `append()/appendDataSubquery()`.

#### **setComputeQuery**

`setComputeQuery(computeQuery: AxiomV2ComputeQuery)`

Sets a `computeQuery`.

#### **setCallback**

`setCallback(callback: AxiomV2Callback)`

Set the callback you want to pass the proof to.&#x20;

#### **setOptions**

`setOptions(options: AxiomV2QueryOptions): AxiomV2QueryOptions`

Developers can set the following options in their query:

```javascript
{
    // Maximum fee per unit of gas for callback
    maxFeePerGas?: string
    // Gas limit you are willing to set for the callback
    callbackGasLimit?: number
    // Warn if the calldata size is above this threshold
    dataQueryCalldataGasWarningThreshold?: number
    // The address that can claim the refund for any unused Ether
    refundee?: string
}
```

Returns the options, `AxiomV2QueryOptions`.

#### **getDataQuery**

`getDataQuery(): UnbuiltSubquery[] | undefined`

Returns the current set of unbuilt data subqueries or undefined.

**getDataSubqueryCount**

`getDataSubqueryCount(): DataSubqueryCount`

Returns the current count of each type of data subquery

#### **getComputeQuery**

`getComputeQuery(): AxiomV2ComputeQuery | undefined`

Returns the current compute query or undefined.

#### **getCallback**

`getCallback(): AxiomV2Callback | undefined`

Returns the current callback information or undefined.

#### **getOptions**

`getOptions(): AxiomV2QueryOptions`

Returns the current Query options.

Developers can set the following options in their query:

```javascript
{
    maxFeePerGas?: string
    callbackGasLimit?: number
    dataQueryCalldataGasWarningThreshold?: number
    refundee?: string
}
```

These will default to a set value and the `refundee` will default to the current wallet address

#### **getBuiltQuery**

`getBuiltQuery(): BuiltQueryV2 | undefined`

Returns the built `Query` or undefined if `Query` has not been built yet. Built `Query` resets if any data is changed.

#### **validate**

`validate(): Promise<boolean>`

Returns a boolean validating the subqueries. Specifically, it checks that values are in the correct ranges.&#x20;

#### **build**

`async build(): Promise<`BuiltQueryV2`>`

Queries the required subquery data and builds the entire Query object into the format required by the backend/ZK circuit.

Returns A built `Query`.

#### **sendOnchainQuery**

`sendOnchainQuery( paymentAmountWei: string, cb?: (receipt: ethers.ContractTransactionReceipt) => void ): Promise<string>`

Sends the query onchain to Axiom's smart contract.&#x20;

#### **sendQueryWithIpfs**

`sendQueryWithIpfs(paymentAmountWei: string, cb?: (receipt: ethers.ContractTransactionReceipt) => void ): Promise<string>`

Returns an IPFS CID.

Used for pinning the encoded Query data to IPFS. The user still submits a transaction on-chain for the Prover to start the proving process, but instead of sending the encoded Query data as calldata, the user will include an IPFS CID hash which the Prover can then use to get the Query, decode it, and generate a ZK proof of it.

#### **append**

`append(dataSubqueries: UnbuiltSubquery[]): void`

Appends a `UnbuiltSubquery[]` object to the current `dataQuery`

#### **appendDataSubquery**

`appendDataSubquery(dataSubquery: UnbuiltSubquery): void`

Appends a single subquery to the current `dataQuery.` Developers can optionally pass the type of the subquery to append. If one is not provided, the type will be inferred from the keys of the subquery.&#x20;

#### **getQuerySchemaHash**

`getQuerySchema(): string`

Returns the hash of the `querySchema` of the `computeQuery`

#### **getDataQueryHash**

`getDataQueryHash(): string`

Returns the hash of the data query

#### **getQueryId**

`getQueryId(caller?: string): Promise<string>`

Returns a uint256 `queryId` for a built Query (requires \`privateKey\` to be set in AxiomConfig if no \`caller\` is supplied to the function)

#### **calculateFee**

`calculateFee(): string`

Returns the amount of `wei` required to send this query


---

# 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:

```
GET https://intrinsic-1.gitbook.io/axiomv2-sdk/sdk-and-repl-reference/axiom-sdk-reference/querybuilderv2.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.
