LogoLogo
1.3.x
1.3.x
  • Documentation
    • Introduction
    • Overview
      • Authorization
      • Rate limits and quotas
      • Pagination
      • Sorting
      • Filtering
      • Optional properties
      • Errors
    • Getting started
    • Highlights
      • Including related entities
      • Including CSPR rates
      • Including account info
      • Accessing auction data
      • Accessing token data
      • Accessing NFT data
      • De-anonymizing account hashes
      • De-anonymizing account purses
      • Receiving contract-level events
    • Changelog
  • REST API
    • Reference
    • Account
      • Get account
      • Get accounts
    • Account Info
      • Get account info
      • Get account infos
    • Auction metrics
      • Get auction metrics
    • Awaiting deploy
      • Create Awaiting Deploy
      • Add Awaiting Deploy signatures
      • Get Awaiting Deploy
    • Block
      • Get block
      • Get blocks
      • Get validator blocks
    • Bidder
      • Get bidder
      • Get bidders
    • Centralized account info
      • Get centralized account info
      • Get centralized account infos
    • Contract
      • Get contract
      • Get contracts
      • Get contracts by contract package
      • Get contract types
    • Contract entry point
      • Get contract entry points
      • Get contract entry point costs
    • Contract package
      • Get contract package
      • Get contract packages
      • Get account contract packages
    • Delegation
      • Get account delegations
      • Get validator delegations
    • Delegator reward
      • Get account delegator rewards
      • Get total account delegation rewards
      • Get total validator delegators' rewards
    • Deploy
      • Get deploy
      • Get deploys
      • Get account deploys
      • Get block deploys
      • Get deploy execution types
    • Fungible token action
      • Get fungible token actions
      • Get fungible token action types
      • Get account fungible token actions
      • Get contract package fungible token actions
    • Fungible token ownership
      • Get account fungible token ownership
      • Get contract package fungible token ownership
    • Non-fungible token (NFT)
      • Get NFT
      • Get account NFTs
      • Get contract package NFTs
      • Get NFT standards
      • Get off-chain NFT metadata statuses
    • Non-fungible token (NFT) action
      • Get contract package NFT actions for a token
      • Get account NFT actions
      • Get contract package NFT actions
      • Get NFT action types
    • Non-fungible token (NFT) ownership
      • Get contract package NFT ownership
      • Get account NFT ownership
    • CSPR rate
      • Get the current currency rate
      • Get historical currency rates
      • Get currencies
    • CSPR supply
      • Get supply
    • Transfer
      • Get account transfers
      • Get deploy transfers
    • Validator
      • Get validator
      • Get validators
    • Validator performance
      • Get historical validator performance
      • Get historical average validator performance
      • Get historical average validators performance
    • Validator reward
      • Get validator rewards
      • Get validator total rewards
  • Streaming API
    • Reference
    • Account balance
    • Block
    • Contract
    • Contract package
    • Contract-level events
    • Deploy
    • Fungible token action
    • Non-fungible token (NFT)
    • Non-fungible token (NFT) action
    • Transfer
  • Casper Node API
    • Connecting with an SDK
Powered by GitBook
On this page
  • Endpoints
  • SDK examples
  • RPC examples
  • SSE examples
Export as PDF
  1. Casper Node API

Connecting with an SDK

PreviousTransfer

Last updated 8 months ago

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 .

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");
        }
    }
}

here
https://node.cspr.cloud
https://node-sse.cspr.cloud
https://node.testnet.cspr.cloud
https://node-sse.testnet.cspr.cloud