snet-sdk
)This guide provides step-by-step instructions for setting up and using the SingularityNET SDK for Node.js. Follow these steps to integrate and interact with SingularityNET services in your application.
Before you start using the snet-sdk, ensure that your system meets the following requirements:
Node.js: Version 18 or higher npm: Version 8 or higher You can check your current versions by running the following commands:
node -v
npm -v
If the versions are lower than required, update Node.js and npm.
Install the snet-sdk package using npm:
npm install snet-sdk
Create a configuration file (e.g., config.js) to store your SingularityNET SDK configuration settings. Below is a sample configuration setup:
{
"web3Provider": "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID",
"privateKey": "0xYOUR_PRIVATE_KEY",
"networkId": "1",
"ipfsEndpoint": "https://ipfs.singularitynet.io",
"defaultGasPrice": "4700000",
"defaultGasLimit": "210000"
}
1
for Mainnet, 11155111
for Sepolia).Replace the placeholders with your actual credentials and network information.
To interact with specific SingularityNET services, you need to generate gRPC client libraries for those services using the SingularityNET CLI.
Install the SingularityNET CLI: If you haven’t installed the CLI yet, you can install it via pip:
pip install snet-cli
For more detailed instructions, you can refer to the SingularityNET CLI documentation.
Generate the gRPC Client Library: Use the CLI to generate the gRPC client library for the service you want to use. Replace
snet sdk generate-client-library nodejs <org_id> <service_id>
The generated client libraries will be located in the default directory ./client_libraries/nodejs/<hash>/<org_id>/<service_id>
unless you specify a different output path.
After setting up the configuration and generating the necessary gRPC client libraries, you can start using the SDK in your application:
import SnetSDK from 'snet-sdk';
import config from './config'; // Path to your config file
const sdk = new SnetSDK(config);
const SnetSDK = require('snet-sdk');
const config = require('./config'); // Adjust path as necessary
const grpc = require('./path_to_generated_grpc_js_file'); // Adjust path as necessary
const sdk = new SnetSDK.default(config);
const client = await sdk.createServiceClient("<org_id>", "<service_id>", grpc.<ClientStub>);
const methodDescriptor = grpc.<ServiceStub>.<methodName>;
const request = new methodDescriptor.requestType();
request.<serviceSetMethod>("<message>");
client.service.<methodName>(request, (err, result) => {
if (err) {
console.error("GRPC call failed", err);
} else {
console.log("Result:", result.toString());
}
});
Replace <methodName>
, <serviceSetMethod>
, and <message>
with the appropriate method name, set method, and message content.
Last modified on : 15-Oct-24