SpaceClient
The SpaceClient is the primary interface for interacting with the DID Space API. It's the main entry point for the SDK, responsible for handling configuration, authentication, and dispatching commands to your DID Space.
Constructor#
To begin, you need to create an instance of the SpaceClient. The constructor accepts a single options object to configure its behavior, including authentication credentials and endpoint details.
Client Initialization
import { SpaceClient } from '@blocklet/did-space-js';
import getWallet from '@blocklet/sdk/lib/wallet';
const wallet = getWallet();
const client = new SpaceClient({
auth: {
wallet: wallet,
endpoint: 'https://www.didspaces.com/app/api/space/...',
},
});Options (SpaceClientOptions)#
The constructor accepts an object with the following properties:
Methods#
send#
The send method is the universal way to execute any command against your DID Space. You pass an instance of a command object, and it returns a promise that resolves with the command's output.
Sending a Command
import { SpaceClient, PutNftObjectCommand } from '@blocklet/did-space-js';
import getWallet from '@blocklet/sdk/lib/wallet';
import { fileURLToPath } from 'url';
import path from 'path';
import fs from 'fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function uploadNft() {
const wallet = getWallet();
const client = new SpaceClient({
auth: {
wallet: wallet,
endpoint: 'https://www.didspaces.com/app/api/space/...',
},
});
const filePath = path.join(__dirname, 'my-nft-image.png');
const fileStream = fs.createReadStream(filePath);
const fileStat = fs.statSync(filePath);
const command = new PutNftObjectCommand({
name: 'My First NFT',
description: 'This is a description of my NFT.',
body: fileStream,See all 14 lines
Parameters
Returns
A Promise that resolves to the command's output object.
Static Methods#
These are utility functions available directly on the SpaceClient class.
getBaseUrl#
Extracts the base URL of a DID Space from a full endpoint URL.
import { SpaceClient } from '@blocklet/did-space-js';
const endpoint = 'https://.../app/api/space/z3T.../app/zNK.../object/';
const baseUrl = await SpaceClient.getBaseUrl(endpoint);
console.log(baseUrl); // 'https://.../app'getDisplayUrl#
Constructs a public-facing URL to view an object within a DID Space.
import { SpaceClient } from '@blocklet/did-space-js';
const endpoint = 'https://.../app/api/space/z3T.../app/zNK.../object/';
const objectDid = 'z3T...'; // The DID of the object
const displayUrl = await SpaceClient.getDisplayUrl(endpoint, objectDid);
console.log(displayUrl);
// 'https://.../app/resolve/z3T.../display'getSpaceEndpointContext#
Parses a full endpoint URL to extract contextual information like the Space DID and Application DID.
import { SpaceClient } from '@blocklet/did-space-js';
const endpoint =
'https://.../api/space/z3T6WZD3dBtaVUgwb3rtBey8BartrJDTLrmQr/app/zNKsSUmUVJntiVdAPpg8vYtEgnFvSppYLf4F/object/';
const context = await SpaceClient.getSpaceEndpointContext(endpoint);
console.log(context.spaceDid); // 'z3T6WZD3dBtaVUgwb3rtBey8BartrJDTLrmQr'
console.log(context.appDid); // 'zNKsSUmUVJntiVdAPpg8vYtEgnFvSppYLf4F'Returns
A Promise resolving to a SpaceEndpointContext object.
Next Steps#
Now that you know how to initialize the client and send commands, you can explore the available commands for interacting with your space: