Skip to content

Overview

TIP

Not interested in micro-payments and crypto? You can also use the Inference Grid by signing up for Platform and purchasing credits.

In this section, we'll explore connecting to the Inference Grid directly with Spark. This allows developers to build novel experience where users can pay for requests from a Spark wallet that is fully non-custodial.

Register

To connect to the relay, you need to generate a keypair and pay a 100-sat lightning invoice to register it. Note that if you running in a Node environment, you'll need to have Node v22.4.0 or later.

ts
import { InferenceGrid } from "inference-grid-sdk";

const { invoice, publicKey, privateKey } = await InferenceGrid.registerClient();
console.log("Invoice", invoice);

The invoice can be paid from any Lightning-compatible wallet such as Cashapp, Coinbase, or a Spark wallet. Once you have paid the invoice, you can check that your keys have been activated via:

ts
import { InferenceGrid } from "inference-grid-sdk";

const client = new InferenceGrid({
    privateKey: "...",
});

try {
  client.check();
} catch (e) {
  console.log(e);
}

Once your keys are activated, you're ready to go!

Connect

Now that you have a private key, you can start making requests.

ts
import { InferenceGrid, Role } from "inference-grid-sdk";

// Load your private keys.
const client = new InferenceGrid({
  privateKey: "...",
});

// Submit a request.
const { message, invoice } = await client.chat({
    model: {
        modelIds: ['openai/gpt-4o'],
    },
    messages: [{
        role: Role.USER,
        content: 'Hello, world!',
    }]
});

// Don't forget to pay your invoice!
console.log("Invoice:", invoice);

In general, each user should have their own private key that they use to submit requests which is stored locally on their device.