Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
383 changes: 383 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EVM_PRIVATE_KEY=0xYourEvmPrivateKey
34 changes: 34 additions & 0 deletions examples/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# dependencies (bun install)
node_modules

# output
out
dist
*.tgz

# code coverage
coverage
*.lcov

# logs
logs
_.log
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# caches
.eslintcache
.cache
*.tsbuildinfo

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
65 changes: 65 additions & 0 deletions examples/client/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions examples/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { wrapFetchWithPayment } from "@x402/fetch";
import { privateKeyToAccount } from "viem/accounts";

// Create signer
const signer = privateKeyToAccount(
process.env.EVM_PRIVATE_KEY as `0x${string}`
);
Comment thread
lucky-ivanius marked this conversation as resolved.

// Create x402 client and register EVM scheme
const client = new x402Client();
client.register("eip155:16661", new ExactEvmScheme(signer));

// Wrap fetch with payment handling
const fetchWithPayment = wrapFetchWithPayment(fetch, client);

// Make request - payment is handled automatically
const response = await fetchWithPayment("http://localhost:3000/weather", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

const data = await response.json();
console.log("Response:", data);

// Get payment receipt from response headers
if (response.ok) {
const httpClient = new x402HTTPClient(client);
const paymentResponse = httpClient.getPaymentSettleResponse((name) =>
response.headers.get(name)
);
console.log("Payment settled:", paymentResponse);
}
17 changes: 17 additions & 0 deletions examples/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "newt0n-client-example",
"module": "index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
Comment thread
lucky-ivanius marked this conversation as resolved.
},
"dependencies": {
"@x402/evm": "^2.10.0",
"@x402/fetch": "^2.10.0",
"viem": "^2.48.0"
}
}
30 changes: 30 additions & 0 deletions examples/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
// Environment setup & latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"types": ["bun"],

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
34 changes: 34 additions & 0 deletions examples/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# dependencies (bun install)
node_modules

# output
out
dist
*.tgz

# code coverage
coverage
*.lcov

# logs
logs
_.log
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# caches
.eslintcache
.cache
*.tsbuildinfo

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
90 changes: 90 additions & 0 deletions examples/server/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions examples/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { HTTPFacilitatorClient } from "@x402/core/server";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { paymentMiddleware, x402ResourceServer } from "@x402/hono";
import { Hono } from "hono";
import { logger } from "hono/logger";

const app = new Hono();
const evmAddress = "0xYourEvmAddress";

const facilitatorClient = new HTTPFacilitatorClient({
url: "http://facilitator.newt0n.ai",
});
Comment thread
lucky-ivanius marked this conversation as resolved.

app.use(logger());

const zeroGExactScheme = new ExactEvmScheme();
zeroGExactScheme.registerMoneyParser(
async (amount: number, _network: string) => {
const tokenAmount = Math.floor(amount * 1_000_000).toString();

return {
amount: tokenAmount,
asset: "0x1f3aa82227281ca364bfb3d253b0f1af1da6473e",
Comment thread
lucky-ivanius marked this conversation as resolved.
extra: {
name: "Bridged USDC",
version: "2",
},
};
}
);

app.use(
paymentMiddleware(
{
"GET /weather": {
accepts: [
{
scheme: "exact",
price: "$0.001",
network: "eip155:16661",
payTo: evmAddress,
},
],
description: "Weather data",
mimeType: "application/json",
},
},
new x402ResourceServer(facilitatorClient).register(
"eip155:16661",
zeroGExactScheme
)
)
);

app.get("/weather", (c) => {
return c.json({
report: {
weather: "sunny",
temperature: 70,
},
});
});

export default app;
18 changes: 18 additions & 0 deletions examples/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "newt0n-server-example",
"module": "index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
},
"dependencies": {
"@x402/core": "^2.10.0",
"@x402/evm": "^2.10.0",
"@x402/hono": "^2.10.0",
"hono": "^4.12.14"
}
}
Loading