# Handling clusters

## Introduction to TEE clusters in the Ternoa JS SDK

If you are not familiar with Ternoa's TEE architecture for privacy yet, we strongly recommend you look at the following sections of the documentation and understand the key concepts:

* [User workflow to interact with the cluster's enclaves.](/learn/ternoa-fortress/decentralized-kms/user-workflow.md)
* [Secure Environment with the TEE.](/learn/ternoa-fortress/tee-coprocessor/trusted-execution-environments.md)
* [Ternoa privacy architecture.](/learn/introduction/privacy.md)
* [ Enclaves definition](/learn/glossary.md#enclave)

The quickest way to summarize the flow to encrypt and store content could be as follows:

*The Secret or Capsule creation process involves encrypting content before uploading it to IPFS. The user's content is secured using a set of private and public keys, the Ternoa Chain, and the TEE. Content is encrypted with the public key, while the private key is split into 5 Shamir shares and stored in the TEE Cluster, which contains 5 enclaves.*

Now, let's see how to select a cluster.

{% hint style="info" %}
All code snippets below and on the subsequent pages are designed to function within a Node.js environment. If you require information on how it operates in a browser environment, please consult this [section.](/build-1/javascript/privacy-protocols/tee-privacy-and-encryption/encryption-in-browser-with-wallet-and-extension.md)
{% endhint %}

### How to choose a cluster:

Unless specific use cases require it, data must be sent to one of the ***PUBLIC*** clusters registered on the chain. (*Private* clusters are available for specific use cases. *Admin* clusters should not be targeted.)

The selected public cluster ID does not matter since all clusters are synced with each other. However, in case you want to implement a retry function or want to manually handle cluster selection, you can use `getPublicsClusters()`. Otherwise, `getFirstPublicClusterAvailable()` is here for you.

#### Get all the public cluster ids

```typescript
import { getPublicsClusters, initializeApi } from "ternoa-js";

const getClustersList = async () => {
  try {
    await initializeApi();
    const clusters = await getPublicsClusters();
    console.log(clusters); //Expected output is an array of cluster ids (number) ex: [0,2,3]
    console.log(clusters[0]); //Expected output a cluster id (number) ex: 0
    process.exit(0);
  } catch (error) {
    process.exit(1);
  }
};
```

#### Get the first public cluster available

```typescript
import { getFirstPublicClusterAvailable, initializeApi } from "ternoa-js";

const getClusterId = async () => {
  try {
    await initializeApi();
    const cluster = await getFirstPublicClusterAvailable();
    console.log(cluster); //Expected output a cluster id (number) ex: 0
    process.exit(0);
  } catch (error) {
    process.exit(1);
  }
};
```

Now the cluster is selected, it is necessary to verify the *health status* of the cluster.

### Cluster health

Before moving on to creation, the good practice is to verify the status of the selected cluster. An enclave in a cluster may not be available because it's in maintenance mode. Therefore, it's important to check the status of the selected cluster.

In most cases, it's not necessary to use or store the data in a variable. Just run the promise `getEnclaveHealthStatus()` to check if any errors are occurring. In case you need to use the *cluster health* data, use `getEnclaveDataAndHealth()`.

```typescript
import {
  getEnclaveHealthStatus,
  getFirstPublicClusterAvailable,
  initializeApi,
} from "ternoa-js";

const checkClusterHealth = async () => {
  try {
    await initializeApi();
    const cluster = await getFirstPublicClusterAvailable();
    await getEnclaveHealthStatus(cluster); // No output expected
    //...
    process.exit(0);
  } catch (error) {
    process.exit(1);
  }
};
```

```typescript
import {
  getEnclaveDataAndHealth,
  getFirstPublicClusterAvailable,
  initializeApi,
} from "ternoa-js";

const getClusterHealth = async () => {
  try {
    await initializeApi();
    const cluster = await getFirstPublicClusterAvailable();
    const healthData = await getEnclaveDataAndHealth(cluster); // Expected output is Promise<EnclaveDataAndHealthType[]>
    console.log(healthData);
    //...
    process.exit(0);
  } catch (error) {
    process.exit(1);
  }
};
```


---

# 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.ternoa.network/build-1/javascript/privacy-protocols/tee-privacy-and-encryption/handling-clusters.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.
