JavaScript Client Guide for HyperBEAM
This guide demonstrates how to integrate with HyperBEAM nodes using the an example JavaScript client library.
Prerequisites
- Node.js
- npm or yarn package manager
Project Setup
First, create a new Node.js project:
mkdir my-hyperbeam-project
cd my-hyperbeam-project
npm init -y
Next, manually edit your package.json
file and add the "type": "module"
property after the "main" line. This is required for using ES modules with import/export syntax.
Then, install the required dependencies:
npm install @permaweb/aoconnect
Your final package.json
should look similar to this:
| {
"name": "my-hyperbeam-project",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"dependencies": {
"@permaweb/aoconnect": "^0.0.77"
}
}
|
Starter
| import { connect, createSigner } from '@permaweb/aoconnect/node';
import { promises as fs } from 'fs';
const WALLET_PATH = 'wallet.json'
const NODE_URL = 'http://localhost:10000'
const wallet = JSON.parse(await fs.readFile(WALLET_PATH, 'utf8'));
console.log(wallet);
const main = async () => {
// Create signer
const signer = createSigner(wallet);
// Connect to node
const { request } = connect({
MODE: 'mainnet',
URL: NODE_URL,
device: '',
signer
});
// Get request
const metaInfoPath = `/~meta@1.0/info`;
const getResponse = await fetch(`${NODE_URL}${metaInfoPath}`);
console.log(getResponse);
// Post request
const data = {
hello: 'world',
testValue: 'example data'
};
const requestParams = {
path: metaInfoPath,
method: 'POST',
...data
};
const postResponse = await request(requestParams);
console.log(postResponse);
}
main();
|
Extending Stater Examples
| const metaInfoPath = `/~meta@1.0/info`;
const getResponse = await fetch(`${NODE_URL}${metaInfoPath}`);
console.log(getResponse);
|
Post Request ~meta@1.0/info
| // Post request
const data = {
hello: 'world',
testValue: 'example data'
};
const requestParams = {
path: metaInfoPath,
method: 'POST',
...data
};
const postResponse = await request(requestParams);
console.log(postResponse);
|