# Clique Query

Clique supports JSON-RPC-styled queries.&#x20;

<table><thead><tr><th width="188">Field</th><th width="270">description</th><th>required</th></tr></thead><tbody><tr><td>id</td><td>Unique query ID</td><td>Yes</td></tr><tr><td>method</td><td>Task name</td><td>Yes</td></tr><tr><td>params</td><td>Task input</td><td>Yes</td></tr><tr><td>input_types</td><td>Task input types</td><td>Only required by Clique Client SDK</td></tr><tr><td>custom_types</td><td>Custom types used in task input types</td><td>Only required by Clique Client SDK</td></tr></tbody></table>

Clique Network supports both on-chain and off-chain queries. Although they both follow our JSON-RPC-styled queries, the specific types naturally differ due to the different programming languages.

{% hint style="warning" %}
Note that you cannot create an identical Clique Query. If you attempt to create an identical Clique Query, you will encounter a `QueryAlreadyExists` error. If you do not need to rerun the Clique Query, you can simply retrieve the previous results instead of creating the same query again. If you indeed wish to rerun the query, specify a different `id` to create a new query.
{% endhint %}

### On-Chain Query

For example, this is the manifest file for the `clique_fibonacci` sample task:

```toml
spec-version = "1"
name = "clique_fibonacci"
type = "Dynamic"

proof-type = ["TEE"]

[input]
n = { type = "u256", description = "n" }

[output]
result = { type = "u256", description = "fibonacci sequence for n" }
```

To create a query for running the `clique_fibonacci` task, you can define the task input using a struct, such as `Params`, and construct the query via `ICliqueTaskManager.Task`:

```solidity
import {ICliqueTaskManager} from "clique-contracts/ICliqueTaskManager.sol";

struct Params {
    uint256 n;
}

contract MyContract {
    function run(uint256 n) public {
        uint32 id = 0;
        bytes memory method = "clique_fibonacci";
        Params memory params = Params(n);
        
        ICliqueTaskManager.Task memory task = ICliqueTaskManager.Task(
            id,
            method,
            abi.encode(params)
        );
        
        // use ICliqueTaskManager to create new query
        // ...
    }
}
```

The above example is quite straightforward. Next, we will demonstrate a slightly more complex example. Below is the manifest file for our `clique_httpsRequest` task, which can be used to provide TLS calls for smart contracts:

```toml
spec-version = "1"
name = "clique_httpsRequest"
type = "BuiltIn"

proof-type = ["TEE"]

[types.Transformation]
from = { type = "string", description = "json pointer to field in response" }
soltype = { type = "string", description = "solidity type" }

[input]
url = { type = "string", description = "Request url" }
encoding = { type = "Transformation[]", description = "encoding format" }

[output]
response = { type = "bytes", description = "ABI encoded" }
```

Note that this task includes a custom type `Transformation`. Next, we will demonstrate how to create a query for this task:

```solidity
import {ICliqueTaskManager} from "clique-contracts/ICliqueTaskManager.sol";

struct Transformation {
    string from;
    string soltype;
}

struct Params {
    string url;
    Transformation[] encoding;
}

contract MyContract {
    function run() public {
        Transformation[] memory transformations = new Transformation[](2);
        transformations[0] = Transformation({from: "/0/id", soltype: "int16"});
        transformations[1] = Transformation({from: "/0/name", soltype: "string"});

        uint32 id = 0;
        bytes memory method = "clique_httpsRequest";
        Params memory params = Params({
            url: "https://jsonplaceholder.typicode.com/users",
            encoding: transformations
        });
        
        ICliqueTaskManager.Task memory task = ICliqueTaskManager.Task(
            id,
            method,
            abi.encode(params)
        );
        
        // use ICliqueTaskManager to create new query
        // ...
    }
}
```

For further details, please refer to [smart-contract-integration](https://docs.clique.tech/build-with-clique/smart-contract-sdk/smart-contract-integration "mention").

### Off-Chain Query

For off-chain tasks, we use JSON format to create queries. Users can create queries via the [clique-client-sdk](https://docs.clique.tech/build-with-clique/clique-client-sdk "mention"), or you can directly access the Clique Network to create queries. However, we strongly recommend using the Clique Client SDK, as it helps you validate the query, eliminating the need for you to manually perform complex validation.

Creating off-chain queries is straightforward and supports the creation of batch queries.

#### Single Query

If use our [clique-client-sdk](https://docs.clique.tech/build-with-clique/clique-client-sdk "mention"), here we use the Rust SDK as an example:

```rust
use serde_json::json;

// Create a single query for running clique_fibonacci task
let json_query = json!({
    "id": 1,
    "method": "clique_fibonacci",
    "params": {"n": "10"}
    "input_types": {"n": "u256"},
    "custom_types": {}
});


// Create a single query for running clique_httpsRequest task
let url = "https://jsonplaceholder.typicode.com/users";
let encoding = json!([
    {"from": "/0/id", "soltype": "int16"},
    {"from": "/0/name", "soltype": "string"},
]);
let json_query = json!({
    "id": 1,
    "method": "clique_httpsRequest",
    "params": {"url": url, "encoding": encoding },
    "input_types": {"url": "string", "encoding": "Transformation[]"},
    "custom_types": {"Transformation": {"from": "string", "soltype": "string"}}
});
```

{% hint style="info" %}
When using the Clique Client SDK, you need to specify `input_types` and `custom_types`. These type information must be filled according to the `input` section of the task's manifest and will be used by the client SDK to validate the query.
{% endhint %}

If you wish to directly access the Clique Network, you can construct the JSON query in the following format, but you will be responsible for validating the entire process yourself:

```json
// JSON query for clique_fibonacci task
{
    "id": 1, 
    "method": "clique_fibonacci", 
    "params": {"n": "10"}
}

// JSON query for clique_httpsRequest task
{
    "id": 1,
    "method": "clique_httpsRequest",
    "params": {
        "url": "https://jsonplaceholder.typicode.com/users", 
        "encoding": [
            {"from": "/0/id", "soltype": "int16"},
            {"from": "/0/name", "soltype": "string"},
        ]
    },
}
```

#### Batch Query

If use our [clique-client-sdk](https://docs.clique.tech/build-with-clique/clique-client-sdk "mention"):&#x20;

```rust
use serde_json::json;

// Create batch query
let json_query = json!([
    {"id": 2, "method": "clique_fibonacci", "params": {"n": "10"}, "input_types": {"n": "u256"}, "custom_types": {}},
    {"id": 3, "method": "clique_fibonacci", "params": {"n": "11"}, "input_types": {"n": "u256"}, "custom_types": {}},
    {"id": 4, "method": "clique_fibonacci", "params": {"n": "12"}, "input_types": {"n": "u256"}, "custom_types": {}}
]);
```

If accessing the Clique Network directly:

```json
[
    {"id": 2, "method": "clique_fibonacci", "params": {"n": "10"}},
    {"id": 3, "method": "clique_fibonacci", "params": {"n": "11"}},
    {"id": 4, "method": "clique_fibonacci", "params": {"n": "12"}}
]
```
