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:
- npm
- yarn
npm install @peaq-network/sdk
yarn add @peaq-network/sdk
Note
In case you don't have an existing project, you'll need to complete a few steps, before and after installing an SDK.
- Run command:
npm init -y
- Install SDK
- Inside
package.json
file write underscripts
"start":"node <js file_name>"
and add"type": "module"
below"version"
key.- Then write or paste code.
- Run command:
npm start
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 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
- krest
- agung
wss://wss-krest.peaq.network
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 });
return sdkInstance;
} catch (error) {
console.error(`Failed to create SDK instance: ${error}`);
throw error;
}
};
Create instance (read+write mode)
import { mnemonicGenerate, cryptoWaitReady } from "@polkadot/util-crypto";
import { Sdk } from "@peaq-network/sdk";
/*
if @polkadot/util-crypto package not installed in your system then you have to install it first.
npm install @polkadot/util-crypto
*/
const generateMnemonicSeed = async () => {
const mnemonicSeed = mnemonicGenerate();
return mnemonicSeed;
};
const createSDKInstance = async () => {
const mnemonicSeed = await generateMnemonicSeed();
await cryptoWaitReady();
const sdkInstance = await Sdk.createInstance({
baseUrl: "peaq-network-chain-url",
seed: mnemonicSeed,
});
return sdkInstance;
};
Connecting to the network
connect() This function, connects a client app to the peaq-network blockchain.
Code example
import { mnemonicGenerate } from "@polkadot/util-crypto";
import { Sdk } from "@peaq-network/sdk";
const generateMnemonicSeed = () => {
const mnemonicSeed = mnemonicGenerate();
return mnemonicSeed;
};
const connectToPeaqTestnet = async () => {
const mnemonicSeed = generateMnemonicSeed();
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed: mnemonicSeed,
});
await sdkInstance.connect();
};
Disconnecting from the network
disconnect()
This function is used to close and terminate the connection between a client application and the network.
Code example
import { mnemonicGenerate } from "@polkadot/util-crypto";
import { Sdk } from "@peaq-network/sdk";
const generateMnemonicSeed = () => {
const mnemonicSeed = mnemonicGenerate();
return mnemonicSeed;
};
const connectToPeaqTestnet = async () => {
const mnemonicSeed = generateMnemonicSeed();
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed: mnemonicSeed,
});
await sdkInstance.connect();
// Some code that interacts with the peaq-network blockchain
// Disconnect from the peaq-network blockchain when no longer needed
await sdkInstance.disconnect();
};
Support
You can always rely on getting answers to your questions and encountered issues in our Discord server.