Skip to content
BEGINNER

Hello World⚓︎

This tutorial shows how to verify that your Symbol SDK installation is working correctly by writing a minimal program that:

  • Retrieves the network name and launch date using the SDK.
  • Connects to a node and prints the current blockchain height.

No accounts, keys, or transactions are required, just a basic SDK call and a REST request.

Prerequisites⚓︎

If you have not done so already, start with Setting Up a Development Environment.

Full Code⚓︎

import json
import urllib.request

from symbolchain.facade.NemFacade import NemFacade
from symbolchain.nem.Network import NetworkTimestamp


facade = NemFacade('testnet')
print(f"Network name: {facade.network.name}")
# NetworkTimestamp(0) is the genesis block timestamp
launch_date = facade.network.to_datetime(NetworkTimestamp(0))
print(f"Network launch date: {launch_date}")

NODE_URL = 'http://libertalia.nemtest.net:7890'
print(f'Using node {NODE_URL}')
try:
    # Fetch current chain height
    height_path = '/chain/height'
    print(f'Fetching chain height from {height_path}')
    with urllib.request.urlopen(
        f'{NODE_URL}{height_path}', timeout=10
    ) as response:
        response_json = json.loads(response.read().decode())
        height = int(response_json['height'])
        print(f"  Blockchain height: {height:,} blocks")

except urllib.error.URLError as e:
    print(e.reason)

Download source

import {
    NemFacade,
    NetworkTimestamp
} from 'symbol-sdk/nem';

const facade = new NemFacade('testnet');
console.log(`Network name: ${facade.network.name}`);
// NetworkTimestamp(0) is the genesis block timestamp (network launch)
const launchDate = facade.network.toDatetime(new NetworkTimestamp(0));
console.log(`Network launch date: ${launchDate.toISOString()}`);

const NODE_URL = 'http://libertalia.nemtest.net:7890';
console.log(`Using node ${NODE_URL}`);
try {
    // Fetch current chain height
    const heightPath = '/chain/height';
    console.log(`Fetching chain height from ${heightPath}`);
    const response = await fetch(`${NODE_URL}${heightPath}`,
        { timeout: 10000 });
    if (!response.ok)
        throw new Error(`HTTP error! status: ${response.status}`);
    const responseJson = await response.json();
    const height = parseInt(responseJson.height, 10);
    console.log(`  Blockchain height: ${height.toLocaleString()} blocks`);
} catch (e) {
    console.error(e.message, '| Cause:', e.cause?.code ?? 'unknown');
}

Download source

Making SDK Calls⚓︎

facade = NemFacade('testnet')
print(f"Network name: {facade.network.name}")
# NetworkTimestamp(0) is the genesis block timestamp
launch_date = facade.network.to_datetime(NetworkTimestamp(0))
print(f"Network launch date: {launch_date}")
const facade = new NemFacade('testnet');
console.log(`Network name: ${facade.network.name}`);
// NetworkTimestamp(0) is the genesis block timestamp (network launch)
const launchDate = facade.network.toDatetime(new NetworkTimestamp(0));
console.log(`Network launch date: ${launchDate.toISOString()}`);

The class is the main entry point to the Symbol SDK when working with the NEM blockchain. It provides most of the methods you will need, from building and signing transactions to retrieving network-related information.

To create a facade, simply specify the name of the network you want to work with, either mainnet or testnet.

This example then demonstrates how to retrieve the network launch date. The method converts a network timestamp into a UTC datetime. By passing 0 (the genesis timestamp) you can obtain the moment the genesis block was produced, that is, the network's launch date.

Retrieving Information From a Node⚓︎

NODE_URL = 'http://libertalia.nemtest.net:7890'
print(f'Using node {NODE_URL}')
try:
    # Fetch current chain height
    height_path = '/chain/height'
    print(f'Fetching chain height from {height_path}')
    with urllib.request.urlopen(
        f'{NODE_URL}{height_path}', timeout=10
    ) as response:
        response_json = json.loads(response.read().decode())
        height = int(response_json['height'])
        print(f"  Blockchain height: {height:,} blocks")

except urllib.error.URLError as e:
    print(e.reason)
const NODE_URL = 'http://libertalia.nemtest.net:7890';
console.log(`Using node ${NODE_URL}`);
try {
    // Fetch current chain height
    const heightPath = '/chain/height';
    console.log(`Fetching chain height from ${heightPath}`);
    const response = await fetch(`${NODE_URL}${heightPath}`,
        { timeout: 10000 });
    if (!response.ok)
        throw new Error(`HTTP error! status: ${response.status}`);
    const responseJson = await response.json();
    const height = parseInt(responseJson.height, 10);
    console.log(`  Blockchain height: ${height.toLocaleString()} blocks`);
} catch (e) {
    console.error(e.message, '| Cause:', e.cause?.code ?? 'unknown');
}

Interaction with the NEM blockchain happens through a node, which exposes a REST interface for querying network state and submitting transactions.

This example connects to a testnet node and retrieves the current blockchain height from the /chain/height GET endpoint.

This request does not require any private keys or authorization, making it a simple and effective test to confirm that the environment is set up correctly and can reach the network.

Output⚓︎

The output shown below corresponds to a typical run of the program.

Network name: testnet
Network launch date: 2015-03-29 00:06:25+00:00
Using node http://libertalia.nemtest.net:7890
Fetching chain height from /chain/height
  Blockchain height: 625,079 blocks

Conclusion⚓︎

If you got the output shown above, you're all set! You have access to the Symbol SDK and successfully reached a NEM node.

That's all you need to start your NEM adventure.

Why not try creating an account next?