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
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 });
})();
import { HTTPTransport, Client } from '@open-rpc/client-js';
import casperSDK from 'casper-js-sdk';
const { CasperServiceByJsonRPC } = casperSDK;
const { CasperClient } = casperSDK;
class CustomCasperClient extends CasperServiceByJsonRPC {
protected client: Client;
constructor(url: string, options?: any) {
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 });
})();
package main
import (
"context"
"fmt"
"net/http"
"github.com/make-software/casper-go-sdk/casper"
)
func main() {
handler := casper.NewRPCHandler("https://node.testnet.cspr.cloud/rpc", http.DefaultClient)
handler.CustomHeaders = map[string]string{"Authorization": "Your-Access-Token"}
rpcClient := casper.NewRPCClient(handler)
status, err := rpcClient.GetStatus(context.Background())
fmt.Println(status, err)
}
using NetCasperSDK;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Your-Access-Token");
var casperClient = new NetCasperClient("https://node.testnet.cspr.cloud", httpClient);
// Get data from RPC client
var punksPackageHash = "hash-ad0cd4ef3cfd9e7222706786e51773af771f063ecce4606282999a7a6d6ac495";
var response = await casperClient.QueryGlobalState(punksPackageHash);
var contractPackage = response.Parse().StoredValue.ContractPackage;
Console.Write("Access key: " + contractPackage.AccessKey);
use Casper\Rpc\RpcClient;
$headers = ['Authorization' => 'Your-Access-Token'];
$client = new RpcClient("https://node.testnet.cspr.cloud", $headers);
$latestBlock = $client->getLatestBlock();
$latestBlockHash = $latestBlock->getHash();
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)
}
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Casper.Network.SDK.SSE;
namespace Casper.NET.SDK.Examples
{
public class CSPRCloudSSE : ServerEventsClient
{
private readonly SocketsHttpHandler _httpSocketHandler = new();
private readonly string _csprCloudUrl;
private readonly string _csprCloudAccessToken;
public CSPRCloudSSE(string url, string token)
{
_csprCloudUrl = url;
_csprCloudAccessToken = token;
//set up _httpSocketHandler as per your needs
}
protected override HttpClient _getHttpClient()
{
var client = new HttpClient(_httpSocketHandler);
client.BaseAddress = new Uri(_csprCloudUrl);
client.DefaultRequestHeaders.Add("Authorization", _csprCloudAccessToken);
return client;
}
}
public static class AwaitEvents
{
public static void DeployListenerCb(SSEvent evt)
{
Console.WriteLine(evt.EventType);
try
{
if (evt.EventType == EventType.DeployProcessed)
{
var deploy = evt.Parse<DeployProcessed>();
Console.WriteLine("DeployProcessed: " + deploy.DeployHash);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public static async Task Main(string[] args)
{
const string url = "https://node-sse.testnet.cspr.cloud";
const string token = "55f79117-fc4d-4d60-9956-65423f39a06a";
// instantiate sse client for CSPR.cloud
//
var sse = new CSPRCloudSSE(url, token);
// add a callback to process deploy processed events.
//
sse.AddEventCallback(EventType.DeployProcessed,
"deploy-listener-cb",
DeployListenerCb,
startFrom: 0);
sse.StartListening();
Console.WriteLine("Press Enter to stop listening.");
Console.ReadLine();
Console.WriteLine("Terminating...");
sse.StopListening().Wait();
Console.WriteLine("Terminated");
}
}
}
Last updated