SDK Quickstart
To get started with the peaq SDK, you'll need to set up the essential tools and components that enable interaction with the peaq blockchain ecosystem.
Prerequisites
- Node.js (latest version)
Supported Programming Languages
- Javascript
Installation
- To begin utilizing the peaq-network SDK, you need to set up your development environment. Follow these simple steps to get started:
- Run command:
npm init -y
- Install SDK with npm or yarn as shown in the box below.
- Inside
package.json
file write"type": "module",
below the"version"
key. - Create a new .js file
- Write Javascript code that interacts with the SDK.
- Run command:
node file_name.js
to execute.
- npm
- yarn
npm install @peaq-network/sdk
yarn add @peaq-network/sdk
Creating an Instance
createInstance(baseUrl, seed)
Parameters :
baseUrl REQUIRED <string>
: network chain url.seed <string>
: seed phrase (from BIP 39 set).
To create the instance of the peaq SDK you'll need to run createSdkInstance
which returns a promise yielding an SDK instance. This function takes 2 parameters: the required baseUrl
parameter and the optional seed
parameter, which will allow it to send transactions (i.e. perform write operations). To ensure robustness, the function incorporates a try-catch block to handle potential errors during the SDK instance creation process. Any encountered errors are logged to the console before being re-thrown to the calling code.
BaseURL parameter examples:
- agung
wss://wsspc1-qa.agung.peaq.network
Code examples
Create instance (read mode)
import { Sdk } from "@peaq-network/sdk";
const createSdkInstance = async (baseUrl) => {
try {
const sdkInstance = await Sdk.createInstance({
baseUrl: baseUrl
});
return sdkInstance;
}
catch (error) {
console.error(`Failed to create SDK instance: ${error}`);
throw error;
}
};
const main = async () => {
const baseUrl = "wss://wsspc1-qa.agung.peaq.network"; // agung base url as given above
const sdk = await createSdkInstance(baseUrl);
await sdk.connect();
await sdk.disconnect();
}
main();
await sdk.connect();
This function, connects a client app to the peaq-network blockchain.
await sdk.disconnect();
This function is used to close and terminate the connection between a client application and the network.
Create instance (read+write mode)
In order to create an instance that can write using the SDK you must use a token supplied wallet to pay for gas fees. It is recommended to use the agung network in order to test your implementation before officially deploying to production. Check out the Get $AGUNG tokens doc to see how to fund a testing account, and how to connect the wallet's seed phrase to a local .env file. If you would like to deploy on peaq/agung you would need to buy the tokens and transfer to your local wallet, while updating the base url upon sdk connection.
import { Sdk } from "@peaq-network/sdk";
import dotenv from 'dotenv';
dotenv.config();
const BASE_URL = "wss://wsspc1-qa.agung.peaq.network"; // AGUNG base url
const MNEMONIC = process.env.MNEMONIC; // .env file's secret variable
const createSdkInstance = async () => {
try {
const sdkInstance = await Sdk.createInstance({
baseUrl: BASE_URL,
seed: MNEMONIC,
});
return sdkInstance;
}
catch (error) {
console.error(`Failed to create SDK write instance: ${error}`);
throw error;
}
};
const main = async () => {
const sdk = await createSdkInstance();
await sdk.connect();
await sdk.disconnect();
}
main();
Support
You can always rely on getting answers to your questions and encountered issues in our Discord server.