Starknet SQD Network API
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 atfirstBlock (can be the genesis block) and ending at the highest available block. Proceed as follows:
-
Retrieve the dataset height from the router with
GET /heightand make sure it’s abovefirstBlock. -
Save the value of
firstBlockto some variable, saycurrentBlock. -
Query the router for an URL of a worker that has the data for
currentBlockwithGET /${currentBlock}/worker. -
Retrieve the data from the worker by posting the query (
POST /), setting the"fromBlock"query field to${currentBlock}. -
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.numberfield 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. -
Set
currentBlockto the height from the previous step plus one. - Repeat steps 3-6 until all the required data is retrieved.
Manually with cURL
Manually with cURL
Suppose we want data on all txs sent by
Layerswap/0x19252b1deef483477c4d30cfcc3e5ed9c82fafea44669c182a45a01b4fdb97a starting from block 600_000. The steps are:-
Verify that the dataset has reached the required height:
Output
- Remember that your current height is 600000.
-
Get a worker URL for the current height:
Output
-
Retrieve the data from the current worker
Output:
-
Parse the retrieved data:
- Grab the network data you requested from the list items with non-empty data fields (
transactions,events). For the example above, this data will include the txn0x6a88.... - Observe that we received the data up to and including block 617979.
- Grab the network data you requested from the list items with non-empty data fields (
-
To get the rest of the data, update the current height to 617980 and go to step 3.
- Note how the worker URL you’re getting while repeating step 3 points to a different host than before. This is how data storage and reads are distributed across the SQD Network.
- Repeat steps 3 through 6 until the dataset height of 632494 is reached.
In Python
In Python
Router API
GET /height (get height of the dataset)
GET /height (get height of the dataset)
Example response:
632494.GET ${firstBlock}/worker (get a suitable worker URL)
GET ${firstBlock}/worker (get a suitable worker URL)
The returned worker is capable of processing
POST / requests in which the "fromBlock" field is equal to ${firstBlock}.Example response: https://rb06.sqd-archive.net/worker/query/czM6Ly9zdGFya25ldC1tYWlubmV0.Worker API
POST / (query transactions and events)
POST / (query transactions and events)
Query Fields
- type:
"starknet". - 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.
- transactions: (optional) A list of transaction requests. An empty list requests no data.
- events: (optional) A list of event requests. An empty list requests no data.
fromBlock. The last block of the range is determined by the worker. You can find it by looking at the header.number field of the last element in the response array.The first and the last block in the range are returned even if all data requests return no data for the range.In most cases the returned range will not contain all the range requested by the user (i.e. the last block of the range will not be toBlock). To continue, retrieve a new worker URL for blocks starting at the end of the current range plus one block and repeat the query with an updated value of fromBlock.Example Request
Example Request
Example Response
Example Response
Data requests
Transactions
events is set to true.
See Data fields selector for info on field selection.
Events
transaction is set to true, all parent transactions will be included into the response.
See Data fields selector for info on field selection.
Data fields selector
A selector of fields for the returned data items. Its structure is as follows:Block fields
true as values, e.g. {"status": true, "timestamp": true}.
Transaction fields
true as values, e.g. {"transactionHash": true, "type": true, "calldata": true}.
Event fields
true as values, e.g. {"fromAddress": true, "data": true}.