import { generateKeyPair, issueGDT } from '@grantex/x402';
const principal = generateKeyPair();
const agent = generateKeyPair();
const gdt = await issueGDT({
agentDID: agent.did,
scope: ['weather:read', 'news:read'],
spendLimit: { amount: 10, currency: 'USDC', period: '24h' },
expiry: '24h',
signingKey: principal.privateKey,
});
import { verifyGDT } from '@grantex/x402';
const result = await verifyGDT(gdtToken, {
resource: 'weather:read',
amount: 0.001,
currency: 'USDC',
});
if (result.valid) {
console.log('Agent:', result.agentDID);
console.log('Remaining:', result.remainingLimit);
} else {
console.error('Rejected:', result.error);
}
import { createX402Agent } from '@grantex/x402';
const x402 = createX402Agent({
gdt: gdtToken,
paymentHandler: async (details) => {
// Sign USDC transfer on Base L2
return await payOnBase(details);
},
});
// Automatic: 402 → pay → retry with GDT
const response = await x402.fetch(
'https://api.weather.xyz/forecast'
);
import express from 'express';
import { x402Middleware } from '@grantex/x402';
const app = express();
app.use('/api/weather', x402Middleware({
requiredScopes: ['weather:read'],
currency: 'USDC',
}));
app.get('/api/weather/forecast', (req, res) => {
const { gdt } = req;
res.json({
forecast: 'sunny',
authorizedBy: gdt.principalDID,
});
});