# 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 | <https://node.cspr.cloud>     |
| Casper Node SSE API | <https://node-sse.cspr.cloud> |

**Testnet**

| Service Type        | URL                                   |
| ------------------- | ------------------------------------- |
| Casper Node RPC API | <https://node.testnet.cspr.cloud>     |
| Casper Node SSE API | <https://node-sse.testnet.cspr.cloud> |

Remember, all endpoints require authorization. Learn how to obtain an access token [here](https://docs.cspr.cloud/documentation/overview/authorization).

## SDK examples

{% hint style="info" %}
Replace "Your-Access-Token" with your actual access token in each example.
{% endhint %}

### RPC examples

{% tabs %}
{% tab title="Typescript" %}

```ts
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 });
})();
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"context"
	"fmt"
	"net/http"

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

{% endtab %}

{% tab title="C#" %}

```csharp
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);
```

{% endtab %}

{% tab title="PHP" %}

```php
use Casper\Rpc\RpcClient;

$headers = ['Authorization' => 'Your-Access-Token']; 
$client = new RpcClient("https://node.testnet.cspr.cloud", $headers);

$latestBlock = $client->getLatestBlock();
$latestBlockHash = $latestBlock->getHash();
```

{% endtab %}

{% tab title="Python" %}

{% endtab %}
{% endtabs %}

### SSE examples

{% tabs %}
{% tab title="Go" %}

```go
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)
}
```

{% endtab %}

{% tab title="C#" %}

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

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cspr.cloud/casper-node-api/connecting-with-an-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
