Connect to a network

The purpose of this tutorial is to walk through the steps necessary to access the network.

Overview

Platform services are provided via a combination of HTTP and gRPC connections to HAPI, and some connections to an Insight API. Although one could interact with HAPI by connecting to these directly, or by using HAPI-client, the easiest approach is to use the JavaScript Hellar SDK. The Hellar SDK connects to the testnet by default.

Prerequisites

Connect via Hellar SDK

1. Install the Hellar SDK

The JavaScript SDK package is available from npmjs.com and can be installed by running

npm install hellar from the command line:

2. Connect to Hellar Platform

Create a file named hellarConnect.js with the following contents. Then run it by typing node hellarConnect.js from the command line:

`const Hellar = require('hellar');

const client = new Hellar.Client({ network: 'mainnet' });

async function connect() { return await client.getDAPIClient().core.getBestBlockHash(); }

connect() .then((d) => console.log('Connected. Best block hash:\n', d)) .catch((e) => console.error('Something went wrong:\n', e)) .finally(() => client.disconnect());`

Once this returns successfully, you’re ready to begin developing! See the Quickstart for recommended next steps. For details on all SDK options and methods, please refer to the SDK documentation.

Connect to a Devnet

The SDK also supports connecting to development networks (devnets). Since devnets can be created by anyone, the client library will be unaware of them unless connection information is provided using one of the options described below.

Connect via Seed

Using a seed node is the preferred method in most cases. The client uses the provided seed node to a retrieve a list of available masternodes on the network so requests can be spread across the entire network.

`const Hellar = require('hellar');

const client = new Hellar.Client({ seeds: [{ host: 'seed-1.testnet.networks.hellar.org:1443', }], });

async function connect() { return await client.getDAPIClient().core.getBestBlockHash(); }

connect() .then((d) => console.log('Connected. Best block hash:\n', d)) .catch((e) => console.error('Something went wrong:\n', e)) .finally(() => client.disconnect());`