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 Type
URL

Casper Node RPC API

Casper Node SSE API

Testnet

Service Type
URL

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 { HttpHandler, RpcClient } from 'casper-js-sdk';

const rpcHandler = new HttpHandler("https://node.testnet.cspr.cloud/rpc");
rpcHandler.setCustomHeaders({
  "Authorization": "55f79117-fc4d-4d60-9956-65423f39a06a"
})
const rpcClient = new RpcClient(rpcHandler);

void (async function main () {
  const transaction = await rpcClient.getTransactionByTransactionHash("ef6a3f8f6c6412b9b2a6eb392d1ca6822e32b791ed7da3236dd0d062b2759bb5");
  console.log({ transaction });
})();

SSE examples

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/make-software/casper-go-sdk/v2/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