ModelGlue x402 examples
Starter scripts for AI apps, agents, and connected devices.
Use these examples as integration patterns for requesting payment requirements, sending a user to checkout, checking receipt status, and issuing a device unlock after payment confirmation.
Examples are designed around non-custodial payment confirmation and merchant-owned receiving addresses. Device owners and app operators remain responsible for fulfillment, refunds, safety rules, and local compliance.
Request setup
Back to ModelGlue x402
AI app quote request
const MODELGLUE_X402 = "https://modelglue.ai";
async function requestAiAppQuote() {
const res = await fetch(`${MODELGLUE_X402}/wp-json/immutifi-x402/v1/quote`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Immutifi-App-ID": "your_app_id",
"X-Immutifi-App-Key": "your_app_key"
},
body: JSON.stringify({
app_id: "your_app_id",
product_id: "api_resource_request",
quantity: 1,
return_url: "https://yourapp.example/paid",
cancel_url: "https://yourapp.example/cancel"
})
});
const quote = await res.json();
window.location.href = quote.checkout_url;
}
Device or vending-machine quote
const MODELGLUE_X402 = "https://modelglue.ai";
async function startDevicePayment(deviceId, sku) {
const res = await fetch(`${MODELGLUE_X402}/wp-json/immutifi-x402/v1/device/quote`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Immutifi-Device-ID": deviceId,
"X-Immutifi-Device-Key": "your_device_key"
},
body: JSON.stringify({
device_id: deviceId,
sku,
mode: "one_time",
return_url: "https://device.example/receipt"
})
});
const quote = await res.json();
showQrOrPaymentLink(quote.checkout_url);
return quote.payment_session.request_id;
}
Receipt polling worker
import time
import requests
MODELGLUE_X402 = "https://modelglue.ai"
REQUEST_ID = "replace_with_request_id"
while True:
r = requests.get(f"{MODELGLUE_X402}/wp-json/immutifi-x402/v1/status/{REQUEST_ID}")
status = r.json()
if status.get("unlock_ready"):
print("Payment confirmed. Unlock the product, service, or rental session.")
break
if status.get("status") in ["failed", "expired", "cancelled"]:
print("Payment not ready:", status.get("status"))
break
time.sleep(3)
Signed callback receiver
import express from "express";
import crypto from "crypto";
const app = express();
const SECRET = process.env.MODELGLUE_X402_CALLBACK_SECRET;
app.use(express.json({ type: "application/json" }));
app.post("/modelglue-x402-callback", (req, res) => {
const signature = req.header("X-Immutifi-Signature") || "";
const body = JSON.stringify(req.body);
const expected = crypto
.createHmac("sha256", SECRET)
.update(body)
.digest("hex");
if (!crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected))) {
return res.status(401).json({ ok: false });
}
if (req.body.unlock_ready === true) {
queueUnlock(req.body.request_id, req.body.device_id || req.body.app_id);
}
res.json({ ok: true });
});
app.listen(3000);
Device fulfillment event
await fetch("https://modelglue.ai/wp-json/immutifi-x402/v1/device/event", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Immutifi-Device-ID": "vending_machine_001",
"X-Immutifi-Device-Key": "your_device_key"
},
body: JSON.stringify({
request_id: "replace_with_request_id",
event_type: "fulfillment_confirmed",
message: "Product dispensed successfully"
})
});
Recommended device flow
- Register the device or app inside ModelGlue.
- Request a quote when the user chooses a product, task, rental time, or API action.
- Show the hosted checkout link or QR code.
- Poll status or receive a signed callback.
- Unlock only when
unlock_ready is true.
- Record fulfillment after the product, rental, device session, or API action completes.