Quickstart - Node.js

This page will introduce the primary operations provided by Request Network’s SDK while using the EthereumPrivateKeySignatureProvider to sign requests with a private key that is managed outside of a wallet.

This approach works well for Node.js environments without access to a Web3 wallet.

You will learn:

  • How to create a request

  • How to update a request (coming soon...)

  • How to pay a request

  • How to detect a payment

  • How to retrieve a user’s requests

Repository

All of the following examples can be found in this repository https://github.com/RequestNetwork/quickstart-node.js

Create a request

To create an unencrypted ERC-20 request, first construct an EthereumPrivateKeySignatureProvider with a private key.

const {
  EthereumPrivateKeySignatureProvider,
} = require("@requestnetwork/epk-signature");
const { Types } = require("@requestnetwork/request-client.js");

const epkSignatureProvider = new EthereumPrivateKeySignatureProvider({
  method: Types.Signature.METHOD.ECDSA,
  privateKey: process.env.PAYEE_PRIVATE_KEY, // Must include 0x prefix
});

Then, first construct a RequestNetwork, passing in the:

  • Request Node URL. In this example, we use the Sepolia Request Node Gateway.

  • EthereumPrivateKeySignatureProvider constructed in the previous step.

Prepare the Request creation parameters:

Then, call createRequest() to create the request and waitForConfirmation() to wait until the request is persisted in IPFS and the CID hash is stored on-chain.

Altogether it looks like this:

https://github.com/RequestNetwork/quickstart-node-js/blob/main/src/createRequest.js

Pay a request / Detect a payment

First, construct a RequestNetwork object and connect it to a Request Node. In this example, we use the Sepolia Request Node Gateway:

Then, retrieve the request and get the request data. Take note of the current request balance, to be used later for payment detection.

Then, construct an ethers v5 Provider and Wallet using a private key. These allow you to read and write to the chain, respectively.

Then, check that the payer has sufficient funds using hasSufficientFunds()

Then, in the case of an ERC-20 request, check that the payer has granted sufficient approval using hasErc20Approval(). If not, submit an approval transaction using approveErc20. Wait for an appropriate number of block confirmations. On Sepolia or Ethereum, 2 block confirmations should suffice. Other chains may require more.

Finally, pay the request using payRequest()

Detect that the payment was successful by polling the request and waiting until the request balance is greater than or equal to the expected amount.

Altogether it looks like this:

https://github.com/RequestNetwork/quickstart-node-js/blob/main/src/payRequest.js

Retrieve a user's requests

First, construct a RequestNetwork object and connect it to a Request Node. In this example, we use the Sepolia Request Node Gateway:

Then, call fromIdentity() to get an array of Request objects or fromRequestId() to get a single Request object. This function retrieves the Requests stored in IPFS and queries on-chain events to determine the balances paid so far. Finally, call getData() on each Request to get the request contents.

Altogether it looks like this:

https://github.com/RequestNetwork/quickstart-node-js/blob/main/src/retrieveRequest.js

Last updated

Was this helpful?