# Establishing a connection to Chromia · Your first Rell app on Chromia · Learn Chromia

> This lesson walks you through connecting a client to the Chromia blockchain. You will build an example client step by step, beginning with the imports at…

Source: https://chromia.com/learn/book-review/build-client/sign-transaction/

Your first Rell app on ChromiaLesson 23 of 26

1. [01Your first Rell app on Chromia](https://chromia.com/learn/book-review/introduction/)
2. [02Set up your project](https://chromia.com/learn/book-review/setup/)
3. [03Rell project structure](https://chromia.com/learn/book-review/rell-structure/)

Part 1: Define your first entity

1. [04Overview](https://chromia.com/learn/book-review/book-entity/)
2. [05Create your first entity](https://chromia.com/learn/book-review/book-entity/tables/)
3. [06Add your first operation](https://chromia.com/learn/book-review/book-entity/basic-operations/)
4. [07Query every book](https://chromia.com/learn/book-review/book-entity/write-queries/)

Part 2: Add a related entity

1. [08Overview](https://chromia.com/learn/book-review/book-review-entity/)
2. [09Define the book review entity](https://chromia.com/learn/book-review/book-review-entity/tables/)
3. [10Add an operation for creating book reviews](https://chromia.com/learn/book-review/book-review-entity/basic-operations/)
4. [11Query all reviews for a book](https://chromia.com/learn/book-review/book-review-entity/write-queries/)

Part 3: Checking and validating inputs

1. [12Overview](https://chromia.com/learn/book-review/input-verification/)
2. [13Verify inputs](https://chromia.com/learn/book-review/input-verification/input-verification/)
3. [14Return structured results from queries](https://chromia.com/learn/book-review/input-verification/structure/)

Part 4: Signing transactions and filtering queries

1. [15Overview](https://chromia.com/learn/book-review/sign-transaction/)
2. [16Sign a transaction](https://chromia.com/learn/book-review/sign-transaction/sign-transaction/)
3. [17Filter and sort query results](https://chromia.com/learn/book-review/sign-transaction/structure/)

Part 5: Blockchain state and transactions explained

1. [18Overview](https://chromia.com/learn/book-review/blockchain-transactions/)
2. [19How blockchain state and transactions work](https://chromia.com/learn/book-review/blockchain-transactions/query-transaction/)
3. [20Walking through an example](https://chromia.com/learn/book-review/blockchain-transactions/example/)

Part 6: Create the client

1. [21Overview](https://chromia.com/learn/book-review/build-client/)
2. [22Prerequisites](https://chromia.com/learn/book-review/build-client/prerequisites/)
3. [23Establishing a connection to Chromia](https://chromia.com/learn/book-review/build-client/sign-transaction/)
4. [24Query the blockchain using postchain-client](https://chromia.com/learn/book-review/build-client/query-blockchain/)
5. [25Complete the example](https://chromia.com/learn/book-review/build-client/complete-example/)

1. [26What’s next?](https://chromia.com/learn/book-review/what-next/)

[Learn](https://chromia.com/learn/)[Your first Rell app on Chromia](https://chromia.com/learn/book-review/)

23 / 26Part 6: Create the clientBeginner

# Establishing a connection to Chromia

This lesson walks you through connecting a client to the Chromia blockchain. You will build an example client step by step, beginning with the imports at the top of a new file, `book_review.ts`.

typescript

```nolines
import { encryption, createClient, newSignatureProvider, IClient, MERKLE_HASH_VERSIONS } from "postchain-client";
import * as readline from "readline";
```

Then add a main function and a helper function that reads user input:

typescript

```nolines
async function main() {}
```

## Creating a client with `createClient`

Calling `createClient` gives you a client instance for querying the blockchain and submitting transactions. Add the call as shown below. Its parameters are explained afterwards.

typescript

```
let client: IClient;
const blockchainRID = "<Blockchain RID>";

async function main() {
  client = await createClient({
    nodeUrlPool: "http://localhost:7740",
    blockchainRid: blockchainRID,
  });
}
```

### Parameters:

* **nodeUrlPool**: The URL of the node to connect to. During development you usually run a Chromia node locally, commonly at `http://localhost:7740`. On testnet or in production, you point this at a remote node's address instead.
* **blockchainRid**: The blockchain's Referential Identifier (RID). This hexadecimal string is unique to each Chromia blockchain and tells them apart.

The snippet above connects to a node running locally and selects the target blockchain by its RID.

**Why both values matter**

* **Specificity**: One Chromia node can host several blockchains. The RID makes sure your client talks to the right one.
* **Flexibility**: Because the node URL and RID are parameters, you can move between environments such as development, staging and production, or target a different blockchain, without touching your application's core logic.

**Finding the blockchain RID**: With a node running, run the following command to ask it for the RID:

text

```nolines
curl http://localhost:7740/brid/iid_0
```

## Signing a transaction with `postchain-client`

The `postchain-client` library makes signing Chromia transactions straightforward. The steps below cover the full process.

### Generate a keypair

Signing requires a cryptographic keypair: a private key and its matching public key. Add the code below to `book_review.ts`, supplying the private key of the admin bookkeeper configured in `chromia.yml`:

typescript

```nolines
const privKey = Buffer.from("<private key>", "hex");
const bookKeeperKeyPair = encryption.makeKeyPair(privKey);
```

`makeKeyPair`, part of the `encryption` module, builds a keypair from the private key you pass in (`privKey`). The returned `bookKeeperKeyPair` holds the private key, which signs, and the corresponding public key, which verifies.

If you followed the earlier instructions and used the `book_admin` key id, read the private key from the `.chromia` folder as follows:

sh

```nolines
cat ~/.chromia/book_admin
```

### Secure storage and retrieval of keys

Cryptographic keys must be stored securely so that no one else can access them. The [Chromia key generation documentation](https://docs.chromia.com/build/cli/commands/keygen/) describes recommended practices for creating, storing and managing keys, including where to place key files and other security measures.

Warning: Security risk

To keep the example short, `privKey` is hard-coded here. In production you must handle private keys securely: never keep them in plain text and never ship them in client-side code. Doing so is a serious security risk.

### Set up the signature provider

With the keypair ready, you need something that applies it to transactions. Add a SignatureProvider to `book_review.ts`:

typescript

```nolines
const bookKeeperSignatureProvider = newSignatureProvider(MERKLE_HASH_VERSIONS.TWO, bookKeeperKeyPair);
```

`newSignatureProvider` takes your keypair (`bookKeeperKeyPair`) and returns a signature provider. Every transaction is signed by this provider before it goes to the blockchain.

### Sending a transaction with `signAndSendUniqueTransaction`

You can now add the first transaction inside the main function of `book_review.ts`:

typescript

```
  console.log("Creating a new book transaction");
  await client.signAndSendUniqueTransaction(
    { name: "create_book", args: ["ISBN1", "Chromia 101", "John Doe"] },
    bookKeeperSignatureProvider
  );
```

In the `postchain-client` library, `signAndSendUniqueTransaction` handles two jobs in one call:

* **Signing**: The transaction is signed with the given signature provider before it leaves the client. When the network verifies the signature, it confirms who sent the transaction and that nothing changed after signing.
* **Sending**: The signed transaction is then submitted to the Chromia blockchain for processing.

### Parameters explained:

The method accepts two arguments:

1. **Transaction object**:

   * `name`: The operation to run on the blockchain. Here, "create\_book" maps to a Rell operation that creates a new book entity on the blockchain.
   * `args`: An array holding the arguments the operation needs.
2. **Signature provider**: Signs the transaction with the keypair created earlier. The snippet passes `bookKeeperSignatureProvider`.

### How it fits into the overall flow:

A call to `signAndSendUniqueTransaction` tells the client to:

* Build a transaction that calls `create_book` on the blockchain with the given arguments.
* Sign it with `bookKeeperSignatureProvider`.
* Submit the signed transaction to the Chromia blockchain.

### Testing your code

At the bottom of the file, add a call that runs `main`:

typescript

```nolines
main();
```

Run the example:

shell

```nolines
npx tsc
node book_review.js
```

The next step adds a query that fetches books and reviews.

Written for this site from Chromia’s public course. Code samples are the course’s own.

[← Previous**Prerequisites**](https://chromia.com/learn/book-review/build-client/prerequisites/)[Next →**Query the blockchain using postchain-client**](https://chromia.com/learn/book-review/build-client/query-blockchain/)

On this page

* [Using the createClient method](#using-the-createclient-method)
* [Signing a transaction with postchain-client](#signing-a-transaction-with-postchain-client)
