Build Apps with Node.js
This guide will help you get started with building apps with Autonomi starting from scratch. This guide has 4 parts:
This has guide has been tested on MacOS and Linux, but the commands might be slightly different for Windows (unless you are using WSL).
Prerequisites
First let's install the required tools to get started:
Node.js with its NPM command-line tool to install dependencies and run your code
Once Node.js is installed, let's proceed to create a local testnet.
Create a local testnet
See Local Network Setup.
Connect to the testnet with Node.js
Let's create a Node.js project that interacts with the testnet. First let's setup a working environment and install the @withautonomi/autonomi package.
Then create a new Node.js file in which we will write our app:
Open up the file in your favorite editor and add the following code:
In your terminal, run the following command to execute the script:
You should see the following output:
Congrats! You've just connected to the testnet! 🎉
Upload and retrieve data with Node.js
Next up let's upload some data to the testnet and retrieve it. We will be using the Autonomi data API for this. Expanding upon our previous work, change the main.mjs file to the following:
For private data, use the
dataPutanddataGetmethods instead!
Congrats! If you got this far, you are ready to start building apps that can store data on Autonomi! 🎉
Working with Registers
Registers are mutable data structures that allow you to store updateable content with versioned history. Here's how to use them:
Key Register Features:
Mutable: Can be updated with new content while preserving history
Versioned: All previous versions are permanently accessible
Owned: Only the key holder can update the register
32-byte limit: Each register value is limited to 32 bytes
Paid updates: Both creation and updates require payment
Common Register Use Cases:
Application Configuration: Store app settings that need updates
Status Tracking: Maintain current state with full history
Version Control: Track document versions
Counters: Implement distributed counters
Metadata: Store changeable file or app metadata
Register API Methods:
client.registerCost(publicKey)- Calculate creation costclient.registerCreate(key, content, payment)- Create new registerclient.registerGet(address)- Get current register valueclient.registerUpdate(key, content, payment)- Update register contentclient.registerHistory(address)- Get version history iteratorClient.registerKeyFromName(mainKey, name)- Generate deterministic keyClient.registerValueFromBytes(buffer)- Create RegisterValue from Buffer
Last updated