Skip to content

Quickstart, testnet

The same flow as the local quickstart, on the real chain. A server account (the gateway) charges 0.002 fUSD for POST /v1/answer and pays the gas of every settlement. A client account (the payer) deposits a little fUSD into the escrow and pays from it. Read the prerequisites first: each step below is one of them.

You use the deployed PQ402Escrow at 0xb7384fe171670fba5af74fc8f5aa99ef084025cd. It holds each payer’s deposit separately and pays whoever the payer signs for, so you do not need your own.

  1. Copy the sample files from the repository’s docs-site/examples into your project: account.ts, register-keys.ts, window.ts, escrow.ts, gate.ts, server-node.ts and pay.ts. Install the server, client and key packages.

  2. Make two phrases, one for each account, and keep them out of version control:

    Terminal window
    node --input-type=module -e "import { generateMnemonic } from '@qorechain/sdk'; console.log(generateMnemonic())"
    Terminal window
    export GATEWAY_MNEMONIC="..." # the server
    export PAYER_MNEMONIC="..." # the client
    export ESCROW_ADDRESS=0xb7384fe171670fba5af74fc8f5aa99ef084025cd
  3. Claim QOR and fUSD for both qor1… addresses at the faucet, and convert the payer’s fUSD to an ERC-20 balance (see prerequisites, item 3).

  4. Register both accounts’ keys:

    Terminal window
    QORE_MNEMONIC="$GATEWAY_MNEMONIC" npx tsx register-keys.ts
    QORE_MNEMONIC="$PAYER_MNEMONIC" npx tsx register-keys.ts
  5. Open an EVM window for each account. The gateway’s lets it submit settlements; the payer’s lets it deposit:

    Terminal window
    QORE_MNEMONIC="$GATEWAY_MNEMONIC" npx tsx window.ts open
    QORE_MNEMONIC="$PAYER_MNEMONIC" npx tsx window.ts open
  6. Deposit 0.01 fUSD into the escrow: five calls’ worth.

    Terminal window
    npx tsx escrow.ts deposit 0.01
  7. Start the server in one terminal:

    server-node.ts
    /** Charge for a route with plain node:http. */
    import { createServer } from "node:http";
    import { nodeListener } from "@pq402/server";
    import { gate, handler } from "./gate.js";
    // Every request goes through the gate; unpriced routes reach the handler for free.
    createServer(nodeListener(gate, handler)).listen(4021, () => {
    console.log("paid endpoint on http://localhost:4021/v1/answer");
    });
    Terminal window
    npx tsx server-node.ts
  8. Pay in another:

    pay.ts
    /**
    * Pay for an API call from a script. The fetch this returns answers a 402 by
    * signing a payment with your ML-DSA-87 key, seals the body to the server's
    * registered ML-KEM-1024 key, checks the signed receipt and opens the answer.
    *
    * PAYER_MNEMONIC="..." npx tsx pay.ts http://localhost:4021/v1/answer
    */
    import { createPq402Fetch } from "@pq402/client";
    import { EvmChainReader, qorechainTestnet } from "@pq402/scheme-qorechain";
    import { accountFromMnemonic, env } from "./account.js";
    const payer = await accountFromMnemonic(env("PAYER_MNEMONIC"));
    const pay = createPq402Fetch({
    payer: payer.address,
    mldsaSecretKey: payer.mldsa.secretKey,
    sealKeys: payer.seal,
    keys: new EvmChainReader({ network: qorechainTestnet }), // the server's keys come from the chain
    chainId: qorechainTestnet.chainId,
    maxAmount: 10_000n, // refuse any price above 0.01 fUSD
    });
    const url = process.argv[2] ?? "http://localhost:4021/v1/answer";
    const res = await pay(url, {
    method: "POST",
    headers: { "content-type": "text/plain" },
    body: "What is 6 x 7?",
    });
    console.log(res.status, await res.text());
    console.log(`settled in ${res.payment?.settlement.transaction}`);
    console.log(`https://explore.qore.network/tx/${res.payment?.settlement.transaction}`);
    Terminal window
    npx tsx pay.ts http://localhost:4021/v1/answer

    The answer is printed with the settlement transaction and its explorer link.

  9. Take the rest of the deposit back and close both windows:

    Terminal window
    npx tsx escrow.ts withdraw
    QORE_MNEMONIC="$PAYER_MNEMONIC" npx tsx window.ts close
    QORE_MNEMONIC="$GATEWAY_MNEMONIC" npx tsx window.ts close

Open the settlement hash on explore.qore.network with the network set to Testnet. It is a call to PQ402Escrow.settle from the gateway’s address, and its logs show the fUSD moving from the escrow to the gateway. You can also read the receipt over JSON-RPC:

Terminal window
curl -s https://evm-testnet.qore.host -H 'content-type: application/json' \
-d '{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionReceipt","params":["0x<hash>"]}'

A recorded run of this flow, with every hash, is on the recorded run page. Its escrow settlement used 253,160 gas.