Reading Twine Data
Use the javascript library to access twine data from an HTTP api
We are going to walk through a basic way of interacting with a Twine HTTP api. For this example we'll use the api hosted by the CURBy project.
Prerequisites
You need a javascript runtime such as node.js, or bun to begin.
Create a project
Follow your package manager's instructions for creating a project. For node.js this looks like:
This tutorial will use ES Module syntax. To enable it we need to edit the package.json
file and set the module type as shown on line 6:
Install twine libraries
To read twine data we need the twine-core package and to interact with an HTTP api we need the twine-http-store package.
Code
Open up the index.js
file in a code editor and input the following:
Explanation:
Line 1: imports the HttpStore
Line 5: Creates an instance of the store and points it to the API
Line 8: Loops through the AsyncIterator returned by the call to
.chains()
Line 9: Prints the CID of each chain
By default, all twine data is verified upon retrieval. So if this succeeds without error, then both the hashes and signatures are valid.
Now you can run this script and see the output.
Let's now modify the code to fetch the last 10 pulses on a specific chain. We'll use node.js commandline arguments to specify the chain.
We create a new function to fetch the last n pulses from a specified chain:
Explanation:
Line 1: declares a function to fetch a specified number of pulses from a chain. With a default of 10 pulses
Line 3: Loops through the AsyncIterable of pulses on the chain
Line 6 & 7: keep track of the number of listed pulses and breaks out of the loop
Notice that on line 5 we access the .value.content.payload
property. The .value
property holds the twine data and the .content.payload
is the pulse payload as a javascript object.
All that is left is to detect when a command line argument is present:
Now we can again run our script:
And you should see a list of pulse CIDs with their payloads.
Whole script
Last updated