Skip to content

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.

Full Code⚓︎

import json
import urllib.request

from symbolchain.facade.SymbolFacade import SymbolFacade
from symbolchain.symbol.Network import NetworkTimestamp

facade = SymbolFacade('mainnet')
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 = 'https://001-sai-dual.symboltest.net:3001'
print(f'Using node {NODE_URL}')
try:
    # Fetch current chain information
    info_path = '/chain/info'
    print(f'Fetching chain information from {info_path}')
    with urllib.request.urlopen(
        f'{NODE_URL}{info_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 {
    SymbolFacade,
    NetworkTimestamp
} from 'symbol-sdk/symbol';

const facade = new SymbolFacade('mainnet');
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 = 'https://001-sai-dual.symboltest.net:3001';
console.log(`Using node ${NODE_URL}`);
try {
    // Fetch current chain information
    const infoPath = '/chain/info';
    console.log(`Fetching chain information from ${infoPath}`);
    const response = await fetch(`${NODE_URL}${infoPath}`,
        { 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 = SymbolFacade('mainnet')
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 SymbolFacade('mainnet');
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. It provides most of the methods you will need when working with Symbol: 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 = 'https://001-sai-dual.symboltest.net:3001'
print(f'Using node {NODE_URL}')
try:
    # Fetch current chain information
    info_path = '/chain/info'
    print(f'Fetching chain information from {info_path}')
    with urllib.request.urlopen(
        f'{NODE_URL}{info_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 = 'https://001-sai-dual.symboltest.net:3001';
console.log(`Using node ${NODE_URL}`);
try {
    // Fetch current chain information
    const infoPath = '/chain/info';
    console.log(`Fetching chain information from ${infoPath}`);
    const response = await fetch(`${NODE_URL}${infoPath}`,
        { 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 Symbol blockchain happens through API nodes, which expose a REST interface for querying network state and submitting transactions. Not all nodes provide this interface, so it is important to connect to one labeled as API Node.

This example connects to an API node and retrieves the current blockchain height from the /chain/info 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: mainnet
Network launch date: 2021-03-16 00:06:25+00:00
Using node https://001-sai-dual.symboltest.net:3001
Fetching chain information from /chain/info
  Blockchain height: 2,367,414 blocks

Conclusion⚓︎

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

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

Why not try sending a transaction next?