Getting Started
This guide provides a step-by-step walkthrough for installing the @blocklet/server-js
client and making your first API call to a Blocklet Server instance. Following these steps will help you quickly connect your application to the server.
Prerequisites#
Before you begin, ensure you have a running Blocklet Server instance. The examples in this guide assume the server is accessible at http://localhost:3030/api/gql
.
Step 1: Install the Client#
You can install the client library from the npm registry using either npm
or yarn
.
Using npm:
npm i @blocklet/server-js -S
Using yarn:
yarn add @blocklet/server-js
Step 2: Make Your First API Call#
Once the client is installed, you can import it into your project, create an instance, and start making requests. The following example demonstrates how to initialize the client and fetch a list of all installed blocklets from the server.
const BlockletServerClient = require('@blocklet/server-js');
// Initialize the client, pointing to your Blocklet Server's GraphQL endpoint
const client = new BlockletServerClient('http://localhost:3030/api/gql');
// You can inspect the available methods provided by the client
console.log('Available Methods:', {
queries: client.getQueries(),
subscriptions: client.getSubscriptions(),
mutations: client.getMutations(),
});
// Example: Define an async function to fetch the list of installed blocklets
async function fetchBlocklets() {
try {
const response = await client.listBlocklets();
console.log('Installed Blocklets:', response);
} catch (error) {
console.error('Failed to fetch blocklets:', error.message);
}
}
// Execute the function
fetchBlocklets();
In this example:
- We import the
BlockletServerClient
from the@blocklet/server-js
package. - A new client instance is created, configured with the GraphQL endpoint of your Blocklet Server.
- We call the
listBlocklets()
method, which sends a GraphQL query to the server. - The response containing the list of blocklets is logged to the console.
Next Steps#
Now that you have successfully installed the client and made a basic API call, the next step is to learn how to authenticate your requests to access protected data and perform restricted actions. Proceed to the Authentication guide to learn more.