Skip to main content

Fuel SQD Network API

warning

The Fuel API of SQD Network is currently in beta. Breaking changes may be introduced in the future releases.

SQD Network API distributes the requests over a (potentially decentralized) network of workers. The main gateway URL points at a router that provides URLs of workers that do the heavy lifting. Each worker has its own range of blocks on each dataset it serves.

Suppose you want to retrieve an output of some query on a block range starting at firstBlock (can be the genesis block) and ending at the highest available block. Proceed as follows:

  1. Retrieve the dataset height from the router with GET /height and make sure it's above firstBlock.

  2. Save the value of firstBlock to some variable, say currentBlock.

  3. Query the router for an URL of a worker that has the data for currentBlock with GET /${currentBlock}/worker.

  4. Retrieve the data from the worker by posting the query (POST /), setting the "fromBlock" query field to ${currentBlock}.

  5. Parse the retrieved data to get a batch of query data plus the height of the last block available from the current worker. Take the header.number field of the last element of the retrieved JSON array - it is the height you want. Even if your query returns no data, you'll still get the block data for the last block in the range, so this procedure is safe.

  6. Set currentBlock to the height from the previous step plus one.

  7. Repeat steps 3-6 until all the required data is retrieved.

The main gateway URL for Fuel Testnet is

https://v2.archive.subsquid.io/network/fuel-testnet
tip

Consult the Beta 5 page for gateway URL and info on differences in data fields if you want data for that network.

Implementation examples:

Manually with cURL

Suppose we want data on Fuel receipts from block 1000000. We begin by finding the main URL for the Fuel Testnet dataset. Then we have to:

  1. Retrieve the dataset height from the router with

    curl https://v2.archive.subsquid.io/network/fuel-testnet/height

    Output

    1211611
  2. Save the value 1000000 to some variable, say currentBlock.

  3. Query the router for an URL of a worker that has the data forcurrentBlock

    curl https://v2.archive.subsquid.io/network/fuel-testnet/1000000/worker

    Output

    https://rb03.sqd-archive.net/worker/query/czM6Ly9mdWVsLXRlc3RuZXQ
  4. Retrieve the data from the worker

    curl https://rb03.sqd-archive.net/worker/query/czM6Ly9mdWVsLXRlc3RuZXQ \
    -X 'POST' -H 'content-type: application/json' -H 'accept: application/json' \
    -d '{
    "type": "fuel",
    "fromBlock":1000000,
    "fields":{"receipt":{"contract":true, "receiptType": true}},
    "receipts":[ {"type": ["LOG_DATA"]} ]
    }' | jq

    Output:

    [
    {
    "header": {
    "number": 1000000,
    "hash": "0xdc31db7fa3c1fb4f3e0910dc5abf927e64cc985eb2eb13418a9f2e00c4b7ad23"
    },
    "receipts": []
    },
    {
    "header": {
    "number": 1002527,
    "hash": "0x649f045675405f9d4ee34bb19479d0e5706ed14615e8f97da9f34dd166e37f35"
    },
    "receipts": [
    {
    "transactionIndex": 0,
    "index": 1,
    "contract": "0xe637b4c254aa07baa9845eb9f8c7ad0965fad5c5b1194cb37193f956be0ce6f3",
    "receiptType": "LOG_DATA"
    }
    ]
    },
    ...
    {
    "header": {
    "number": 1211611,
    "hash": "0x04c9ef60f2b54d32569a410477c136f11b692c1d3eadaa5b946a0295b526223e"
    },
    "receipts": []
    }
    ]
  5. Parse the retrieved data to get a batch of query data plus the height of the last block available from the current worker. Take the header.number field of the last element of the retrieved JSON array - it is the height you want. Even if your query returns no data, you'll still get the block data for the last block in the range, so this procedure is safe.

  6. Set currentBlock to the height from the previous step plus one.

  7. Repeat steps 3-6 until all the required data is retrieved.

In Python
def get_text(url: str) -> str:
res = requests.get(url)
res.raise_for_status()
return res.text

def dump(
gateway_url: str,
query: Query,
first_block: int,
last_block: int
) -> None:
assert 0 <= first_block <= last_block
query = dict(query) # copy query to mess with it later

dataset_height = int(get_text(f'{gateway_url}/height'))
next_block = first_block
last_block = min(last_block, dataset_height)

while next_block <= last_block:
worker_url = get_text(f'{gateway_url}/{next_block}/worker')

query['fromBlock'] = next_block
query['toBlock'] = last_block
res = requests.post(worker_url, json=query)
res.raise_for_status()
blocks = res.json()

last_processed_block = blocks[-1]['header']['number']
next_block = last_processed_block + 1
for block in blocks:
print(json.dumps(block))

Full code here.

Router API

GET /height (get height of the dataset)

Example response: 16576911.

GET ${firstBlock}/worker (get a suitable worker URL)

The returned worker will be capable of processing POST / requests in which the "fromBlock" field is equal to ${firstBlock}.

Example response: https://rb03.sqd-archive.net/worker/query/czM6Ly9mdWVsLXRlc3RuZXQ.

Worker API

POST / (query inputs and receipts)
Query Fields
  • fromBlock: Block number to start from (inclusive).
  • toBlock: (optional) Block number to end on (inclusive). If this is not given, the query will go on for a fixed amount of time or until it reaches the height of the dataset.
  • includeAllBlocks: (optional) If true, the Network will include blocks that contain no data selected by data requests into its response.
  • fields: (optional) A selector of data fields to retrieve. Common for all data items.
  • receipts: (optional) A list of receipts requests. An empty list requests no data.
  • inputs: (optional) A list of inputs requests. An empty list requests no data.
  • outputs: (optional) A list of outputs requests. An empty list requests no data.
  • transactions: (optional) A list of transactions requests. An empty list requests no data.
Example Request
{
"type": "fuel",
"fromBlock": 1000000,
"toBlock": 1100000,
"fields": { "receipt": { "contract": true, "receiptType": true } },
"receipts": [{ "type": ["LOG_DATA"] }],
"inputs": [{ "type": ["InputCoin"] }]
}
Example Response
[
{
"header": {
"number": 1000000,
"hash": "0xdc31db7fa3c1fb4f3e0910dc5abf927e64cc985eb2eb13418a9f2e00c4b7ad23"
},
"receipts": [],
"inputs": []
},
{
"header": {
"number": 1002527,
"hash": "0x649f045675405f9d4ee34bb19479d0e5706ed14615e8f97da9f34dd166e37f35"
},
"receipts": [
{
"transactionIndex": 0,
"index": 1,
"contract": "0xe637b4c254aa07baa9845eb9f8c7ad0965fad5c5b1194cb37193f956be0ce6f3",
"receiptType": "LOG_DATA"
}
],
"inputs": [
{
"transactionIndex": 0,
"index": 1
}
]
},
...
{
"header": {
"number": 1100000,
"hash": "0x4e1420d7c2cd973842ef1ce919560f15e5461376b7533c371fb895034c85dfd3"
},
"receipts": [],
"inputs": []
}
]

Data requests

Receipts

{
type?: ReceiptType[]
contract?: string[]
transaction?: boolean
}

Receipts will be included in the response if it matches all the requests. An empty array matches no instructions; omit all requests to match all receipts.

See addReceipt() SDK data request method for details on this request; also see Receipt fields.

Transactions

{
type?: TransactionType[]
receipts?: boolean
inputs?: boolean
outputs?: boolean
}

A transaction will be included in the response if it matches all the requests. An empty array matches no transactions; omit all requests to match all transactions.

See addTransaction() SDK data request method for details on this request; also see Transaction fields.

Inputs

{
type?: InputType[]
coinOwner?: Bytes[]
coinAssetId?: Bytes[]
contractContract?: Bytes[]
messageSender?: Bytes[]
messageRecipient?: Bytes[]
transaction?: boolean
}

An input will be included in the response if it matches all the requests. An empty array matches no inputs; omit all requests to match all inputs.

See addInput() SDK data request method for details on this request; also see Input fields.

Outputs

{
type?: OutputType[]
transaction?: boolean
}

An output will be included in the response if it matches all the requests. An empty array matches no outputs; omit all requests to match all outputs.

See addOutput() SDK data request method for details on this request; also see Output fields.

Data fields selector

A JSON selector of fields for the returned data items. Documented in the Field selectors section.