Connecting with an SDK

CSPR.cloud offers Mainnet and Testnet access to Casper nodes. This guide demonstrates how to authenticate using an access token across various programming languages.

Endpoints

Connect to CSPR.cloud nodes using the following endpoints:

Mainnet

Service TypeURL

Casper Node RPC API

Casper Node SSE API

Testnet

Service TypeURL

Casper Node RPC API

Casper Node SSE API

Remember, all endpoints require authorization. Learn how to obtain an access token here.

SDK examples

Replace "Your-Access-Token" with your actual access token in each example.

RPC examples

import { HTTPTransport } from '@open-rpc/client-js';
import casperSDK from 'casper-js-sdk';
const { CasperServiceByJsonRPC } = casperSDK;
const { CasperClient } = casperSDK;

class CustomCasperClient extends CasperServiceByJsonRPC {
    constructor(url, options) {
        super(url);
        const transport = new HTTPTransport(url, options);
        this.client.requestManager.transports = [transport];
    }
}

const customCasperClient = new CustomCasperClient("https://node.testnet.cspr.cloud/rpc", {
    headers: {
        "Authorization": "55f79117-fc4d-4d60-9956-65423f39a06a"
    }
});
const casperClient = new CasperClient("");
casperClient.nodeClient = customCasperClient;

(async function(){
    const result = await casperClient.nodeClient.getStatus();
    console.log({ result });
    const deploy = await casperClient.getDeploy("88461218a5e972fcda1d764d7cc4edb2e0c3a538123b97890d484f43c55935f5");
    console.log({ deploy });
})();

SSE examples

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/make-software/casper-go-sdk/sse"
)

func main() {
	sseClient := sse.NewClient("https://node-sse.testnet.cspr.cloud/events/main")
	sseClient.Streamer.Connection.Headers = map[string]string{"Authorization": "Your-Access-Token"}

	sseClient.RegisterHandler(sse.APIVersionEventType, func(ctx context.Context, event sse.RawEvent) error {
		data, _ := event.ParseAsAPIVersionEvent()
		fmt.Println(data.APIVersion)
		return nil
	})
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()
	sseClient.Start(ctx, -1)
}

Last updated