Quickstart

Golem JS Quickstart

Introduction

In this article, we'll show you how to run a simple task on the Golem Network.

As a Quickstart, we will perform a simple task: running a basic shell command on a rented provider.

It should take just a few minutes to complete, and you will see the command output printed in your terminal.

Installing and running Yagna

Yagna is a service that communicates and performs operations on the Golem Network. Let's get started by installing it.

Install Yagna

On Linux/ MacOS, you can install it using our installation script like this:

curl -sSf https://join.golem.network/as-requestor | bash -

You might be asked to modify your PATH afterward.

info

Should you encounter any problems, please reach out to us via our Discord channel or consult the following resource for troubleshooting.

Start the Yagna service

Open a terminal (command line window) and define the app-key that will allow our script to use the Yagna API:

export YAGNA_AUTOCONF_APPKEY=try_golem

Then start the yagna service:

yagna service run

Get test GLM tokens

Requesting tasks on the Golem Network requires GLM tokens. As this example will run on a test network, you can use test GLM.

Open another terminal and run the following command to complete the configuration:

yagna payment fund

It will top up your account with test GLM tokens. These tokens can only be used on the testnet.

Building your first Golem Network App

Create a new Node.js project and install the Golem SDK by entering the following commands in your terminal:

mkdir try_golem
cd try_golem
npm init
npm install @golem-sdk/golem-js
npm install @golem-sdk/pino-logger
warning

Please note: This application requires Node.js version 18.0.0 or higher.

Create a file named requestor.mjs and copy the following content into it. The code engages 5 providers (up to 3 at the same time) to run a command on each of them. The command checks the CPU of the provider and prints it together with the node's name to your terminal.

/**
 * This example demonstrates how easily lease multiple machines at once.
 */

import { GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const order = {
  demand: {
    workload: { imageTag: "golem/alpine:latest" },
  },
  market: {
    rentHours: 0.5,
    pricing: {
      model: "linear",
      maxStartPrice: 0.5,
      maxCpuPerHourPrice: 1.0,
      maxEnvPerHourPrice: 0.5,
    },
  },
};

(async () => {
  const glm = new GolemNetwork({
    logger: pinoPrettyLogger({
      level: "info",
    }),
    api: { key: "try_golem" },
  });

  try {
    await glm.connect();
    const pool = await glm.manyOf({
      // I want to have a minimum of one machine in the pool,
      // but only a maximum of 3 machines can work at the same time
      poolSize: { min: 1, max: 3 },
      order,
    });
    // I have 5 parts of the task to perform in parallel
    const data = [...Array(5).keys()];
    const results = await Promise.allSettled(
      data.map((i) =>
        pool.withRental((rental) =>
          rental
            .getExeUnit()
            .then((exe) =>
              exe.run(
                `echo "Part #${i} computed on provider ${exe.provider.name} with CPU:" && cat /proc/cpuinfo | grep 'model name'`,
              ),
            ),
        ),
      ),
    );
    results.forEach((result) => {
      if (result.status === "fulfilled") {
        console.log("Success:", result.value.stdout);
      } else {
        console.log("Failure:", result.reason);
      }
    });
  } catch (err) {
    console.error("Failed to run the example", err);
  } finally {
    await glm.disconnect();
  }
})().catch(console.error);
info

You can find a detailed explanation of the above code here

Running the script on the Golem Network

Run the command:

node requestor.mjs

The output of the script should look very similar to the one below:

Output logs

Summary

You've installed the Yagna service and executed a simple task on the Golem Network. However, you can accomplish much more. Here are some suggested next steps to explore the Golem Network world:

See also