Getting Started
This guide provides a step-by-step walkthrough to get you up and running with the @blocklet/server-js library. You'll learn how to install the client, connect to a Blocklet Server, and make your first API call.
Installation#
First, you need to add the @blocklet/server-js package to your project. You can use your preferred package manager:
npm
npm install @blocklet/server-jsyarn
yarn add @blocklet/server-jspnpm
pnpm add @blocklet/server-jsUsage#
The library is designed to work seamlessly in both Node.js and browser environments. The first step is to import and instantiate the BlockletServerClient with the GraphQL endpoint of your Blocklet Server.
Node.js#
In a Node.js environment, you can use require or import to get the client class.
Node.js Example
const BlockletServerClient = require('@blocklet/server-js');
// Replace with your Blocklet Server's GraphQL endpoint
const endpoint = 'http://localhost:4000/api';
const client = new BlockletServerClient(endpoint);Browser#
For frontend applications, you can import the client directly into your components or scripts.
Browser Example
import BlockletServerClient from '@blocklet/server-js';
// The endpoint is typically the one your frontend is served from
const endpoint = '/api'; // Or a full URL like 'https://my-node.abtnode.com/api'
const client = new BlockletServerClient(endpoint);Making Your First API Call#
Once the client is initialized, you can use its methods to interact with the Blocklet Server's GraphQL API. Let's make a simple query to fetch basic information about the node.
All API methods return an object containing either a response or an error.
fetch-node-info.js
const BlockletServerClient = require('@blocklet/server-js');
async function fetchNodeInfo() {
try {
// Replace with your Blocklet Server's GraphQL endpoint
const client = new BlockletServerClient('http://localhost:4000/api');
// Fetch the node's information
const { response, error } = await client.getNodeInfo();
if (error) {
console.error('Error fetching node info:', error.message);
return;
}
console.log('Node Info:', response.info);
} catch (err) {
console.error('An unexpected error occurred:', err);
}
}
fetchNodeInfo();Example Response#
If the call is successful, you will receive a response object similar to this:
Response
{
"info": {
"name": "My Development Node",
"version": "1.16.22",
"did": "z8iZge7d3iYy2iZb..."
}
}Next Steps#
Congratulations! You've successfully installed the Blocklet Server Client and made your first API call.
Most API calls require authentication to ensure that only authorized users or applications can access or modify data. The next step is to learn how to authenticate your requests.