create-meterkit 0.0.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +25 -6
  3. package/dist/cli.d.ts +28 -0
  4. package/dist/cli.js +173 -0
  5. package/dist/cli.js.map +1 -0
  6. package/dist/cli.test.d.ts +1 -0
  7. package/dist/cli.test.js +105 -0
  8. package/dist/cli.test.js.map +1 -0
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.js +4 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/plan.d.ts +12 -0
  13. package/dist/plan.js +90 -0
  14. package/dist/plan.js.map +1 -0
  15. package/dist/security.test.d.ts +1 -0
  16. package/dist/security.test.js +63 -0
  17. package/dist/security.test.js.map +1 -0
  18. package/dist/templates.integration.test.d.ts +1 -0
  19. package/dist/templates.integration.test.js +107 -0
  20. package/dist/templates.integration.test.js.map +1 -0
  21. package/dist/write.d.ts +16 -0
  22. package/dist/write.js +159 -0
  23. package/dist/write.js.map +1 -0
  24. package/dist/write.test.d.ts +1 -0
  25. package/dist/write.test.js +48 -0
  26. package/dist/write.test.js.map +1 -0
  27. package/package.json +40 -8
  28. package/templates/express/.env.example +5 -0
  29. package/templates/express/README.md +59 -0
  30. package/templates/express/package.json +22 -0
  31. package/templates/express/src/payment-store.ts +20 -0
  32. package/templates/express/src/server.ts +31 -0
  33. package/templates/hono/.env.example +5 -0
  34. package/templates/hono/README.md +37 -0
  35. package/templates/hono/package.json +24 -0
  36. package/templates/hono/src/payment-store.ts +20 -0
  37. package/templates/hono/src/server.ts +48 -0
  38. package/templates/mcp/.env.example +5 -0
  39. package/templates/mcp/README.md +30 -0
  40. package/templates/mcp/package.json +24 -0
  41. package/templates/mcp/src/payment-store.ts +20 -0
  42. package/templates/mcp/src/server.ts +94 -0
  43. package/templates/next-route/.env.example +5 -0
  44. package/templates/next-route/README.md +37 -0
  45. package/templates/next-route/app/api/premium/route.ts +52 -0
  46. package/templates/next-route/app/payment-store.ts +22 -0
  47. package/templates/next-route/next-env.d.ts +4 -0
  48. package/templates/next-route/package.json +27 -0
  49. package/templates/next-route/tsconfig.json +20 -0
@@ -0,0 +1,59 @@
1
+ # MeterKit Express API — devnet beta
2
+
3
+ This generated project protects one Express route with x402 on Solana devnet.
4
+ It never needs a seed phrase or server-side payer key.
5
+
6
+ ## Start and inspect the challenge
7
+
8
+ The initializer already writes `.env` with your public disposable recipient.
9
+ If installation was skipped or failed, recover with:
10
+
11
+ ```bash
12
+ {{INSTALL_COMMAND}}
13
+ ```
14
+
15
+ Start the provider:
16
+
17
+ ```bash
18
+ {{DEV_COMMAND}}
19
+ ```
20
+
21
+ In a second terminal:
22
+
23
+ ```bash
24
+ {{CHECK_COMMAND}}
25
+ ```
26
+
27
+ Expected: `HTTP 402` followed by x402 version 2, Solana devnet, test-USDC,
28
+ amount `10000`, your recipient and `http://localhost:3000/premium`.
29
+
30
+ Validate the exact policy with `{{VERIFY_COMMAND}}`. Payment is a separate,
31
+ state-changing test.
32
+ Create a disposable funded devnet keypair at `.local-wallets/payer.json`, keep
33
+ the file mode at `0600`, then run `{{PAY_COMMAND}}`. It authorizes at most
34
+ `10000` atomic test-USDC for the request and session, pays once, and proves the
35
+ same payment is rejected on replay. Never use a mainnet or valuable wallet.
36
+
37
+ ## Important durability boundary
38
+
39
+ This project currently uses `MemoryPaymentStore`. It is suitable only for local
40
+ challenge discovery: restart erases replay history. Do not accept paid requests
41
+ or deploy it until the documented PostgreSQL mode is configured and its
42
+ restart/replay test passes.
43
+
44
+ ## Troubleshooting and reset
45
+
46
+ Run `{{DOCTOR_COMMAND}}` while the server is active. It checks configuration,
47
+ devnet, token-account, facilitator, endpoint and active durability without
48
+ funding, migrating or changing anything.
49
+
50
+ - Missing wallet: inspect `.env`; it must contain a public devnet address only.
51
+ - Port busy: set a different public `PORT` value in `.env` and use that port in
52
+ the check URL.
53
+ - RPC/facilitator unavailable: retry later; do not interpret it as settlement.
54
+ - Reset: stop the process and remove `node_modules`. Delete the entire generated
55
+ directory only if it contains no work you need.
56
+
57
+ Never commit `.env`, a keypair, seed phrase, payment header or full proof.
58
+ If the reported remediation still fails, open a sanitized report at
59
+ https://github.com/JuanTorchia/meterkit/issues (never attach `.env` or keys).
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "meterkit-express-api",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "node --env-file=.env --import tsx src/server.ts",
7
+ "check:unpaid": "meterkit check http://localhost:3000/premium --allow-localhost",
8
+ "doctor": "meterkit doctor --project . --url http://localhost:3000/premium --allow-localhost",
9
+ "verify": "meterkit verify http://localhost:3000/premium --network solana-devnet --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --allow-localhost",
10
+ "pay:test": "meterkit pay http://localhost:3000/premium --keypair .local-wallets/payer.json --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --max-session 10000 --rpc-url https://api.devnet.solana.com --replay --yes"
11
+ },
12
+ "dependencies": {
13
+ "@usemeterkit/database": "0.3.0",
14
+ "@usemeterkit/sdk": "0.3.0",
15
+ "express": "5.1.0"
16
+ },
17
+ "devDependencies": {
18
+ "@usemeterkit/cli": "0.3.0",
19
+ "tsx": "^4.23.12",
20
+ "typescript": "^5.9.3"
21
+ }
22
+ }
@@ -0,0 +1,20 @@
1
+ import { StandalonePostgresPaymentStore } from "@usemeterkit/database";
2
+ import { MemoryPaymentStore, type PaymentStore } from "@usemeterkit/sdk";
3
+
4
+ export async function createPaymentStore(): Promise<PaymentStore> {
5
+ const mode = process.env.DURABILITY_MODE ?? "memory";
6
+ if (mode === "memory") {
7
+ process.stderr.write(
8
+ "WARNING: memory payment store is non-durable; do not serve paid production traffic.\n",
9
+ );
10
+ return new MemoryPaymentStore();
11
+ }
12
+ if (mode !== "postgres")
13
+ throw new Error("DURABILITY_MODE must be memory or postgres");
14
+ const url = process.env.DATABASE_URL;
15
+ if (!url)
16
+ throw new Error("DATABASE_URL is required when DURABILITY_MODE=postgres");
17
+ const store = StandalonePostgresPaymentStore.connect(url);
18
+ await store.migrate();
19
+ return store;
20
+ }
@@ -0,0 +1,31 @@
1
+ import express from "express";
2
+ import { protect, SOLANA_DEVNET } from "@usemeterkit/sdk";
3
+ import { createPaymentStore } from "./payment-store.js";
4
+
5
+ const wallet = process.env.MERCHANT_WALLET;
6
+ if (!wallet)
7
+ throw new Error("Set MERCHANT_WALLET to a disposable devnet address");
8
+ const port = Number(process.env.PORT ?? "3000");
9
+ const app = express();
10
+ const store = await createPaymentStore();
11
+ app.get(
12
+ "/premium",
13
+ protect({
14
+ product: {
15
+ id: "premium",
16
+ name: "Premium API",
17
+ description: "Protected data",
18
+ resource: `http://localhost:${port}/premium`,
19
+ priceAtomic: "10000",
20
+ assetMint: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
21
+ payTo: wallet,
22
+ network: SOLANA_DEVNET,
23
+ },
24
+ store,
25
+ rpcUrl: process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com",
26
+ }),
27
+ (_request, response) => response.json({ protected: true }),
28
+ );
29
+ app.listen(port, () =>
30
+ process.stdout.write(`MeterKit: http://localhost:${port}/premium\n`),
31
+ );
@@ -0,0 +1,5 @@
1
+ SOLANA_NETWORK=solana-devnet
2
+ SOLANA_RPC_URL=https://api.devnet.solana.com
3
+ MERCHANT_WALLET=replace_with_a_disposable_devnet_address
4
+ DURABILITY_MODE=memory
5
+ # DATABASE_URL=postgresql://user:password@localhost:5432/meterkit
@@ -0,0 +1,37 @@
1
+ # MeterKit Hono API — devnet beta
2
+
3
+ This generated project protects one Hono route with x402 on Solana devnet.
4
+
5
+ ```bash
6
+ {{INSTALL_COMMAND}}
7
+ {{DEV_COMMAND}}
8
+ ```
9
+
10
+ In a second terminal run:
11
+
12
+ ```bash
13
+ {{CHECK_COMMAND}}
14
+ ```
15
+
16
+ Expected: `HTTP 402` with x402 v2, Solana devnet, test-USDC, amount `10000`,
17
+ your public recipient and `http://localhost:3000/premium`.
18
+
19
+ Run `{{VERIFY_COMMAND}}` to enforce the exact expected policy. Only when you have a
20
+ disposable funded devnet keypair at `.local-wallets/payer.json` with mode `0600`,
21
+ run `{{PAY_COMMAND}}`; it caps both request and session at `10000` atomic
22
+ test-USDC, pays once, then verifies replay rejection. Never use valuable keys.
23
+
24
+ The initializer writes `.env`; no shell export is required. The included
25
+ `MemoryPaymentStore` loses replay history on restart and is for local discovery
26
+ only. Do not accept payments or deploy until PostgreSQL durability and the
27
+ restart/replay test are configured.
28
+
29
+ If setup fails, rerun the install command above. Check `.env` for a public
30
+ recipient, change `PORT` if occupied, and treat RPC/facilitator outages as
31
+ unavailable—not failed settlement. To reset, stop the server and remove
32
+ `node_modules`; remove the project directory only after preserving your work.
33
+ Run `{{DOCTOR_COMMAND}}` for a non-mutating, sanitized prerequisite report. If its
34
+ remediation does not recover the project, share only that sanitized output at
35
+ https://github.com/JuanTorchia/meterkit/issues.
36
+
37
+ Never commit `.env`, signing material, payment headers or full proofs.
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "meterkit-hono-api",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "node --env-file=.env --import tsx src/server.ts",
7
+ "check:unpaid": "meterkit check http://localhost:3000/premium --allow-localhost",
8
+ "doctor": "meterkit doctor --project . --url http://localhost:3000/premium --allow-localhost",
9
+ "verify": "meterkit verify http://localhost:3000/premium --network solana-devnet --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --allow-localhost",
10
+ "pay:test": "meterkit pay http://localhost:3000/premium --keypair .local-wallets/payer.json --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --max-session 10000 --rpc-url https://api.devnet.solana.com --replay --yes"
11
+ },
12
+ "dependencies": {
13
+ "@usemeterkit/database": "0.3.0",
14
+ "@usemeterkit/sdk": "0.3.0",
15
+ "@x402/hono": "2.22.0",
16
+ "hono": "4.13.0",
17
+ "@hono/node-server": "2.1.0"
18
+ },
19
+ "devDependencies": {
20
+ "@usemeterkit/cli": "0.3.0",
21
+ "tsx": "^4.23.12",
22
+ "typescript": "^5.9.3"
23
+ }
24
+ }
@@ -0,0 +1,20 @@
1
+ import { StandalonePostgresPaymentStore } from "@usemeterkit/database";
2
+ import { MemoryPaymentStore, type PaymentStore } from "@usemeterkit/sdk";
3
+
4
+ export async function createPaymentStore(): Promise<PaymentStore> {
5
+ const mode = process.env.DURABILITY_MODE ?? "memory";
6
+ if (mode === "memory") {
7
+ process.stderr.write(
8
+ "WARNING: memory payment store is non-durable; do not serve paid production traffic.\n",
9
+ );
10
+ return new MemoryPaymentStore();
11
+ }
12
+ if (mode !== "postgres")
13
+ throw new Error("DURABILITY_MODE must be memory or postgres");
14
+ const url = process.env.DATABASE_URL;
15
+ if (!url)
16
+ throw new Error("DATABASE_URL is required when DURABILITY_MODE=postgres");
17
+ const store = StandalonePostgresPaymentStore.connect(url);
18
+ await store.migrate();
19
+ return store;
20
+ }
@@ -0,0 +1,48 @@
1
+ import { serve } from "@hono/node-server";
2
+ import { createMeterKitResourceServer, SOLANA_DEVNET } from "@usemeterkit/sdk";
3
+ import { paymentMiddleware } from "@x402/hono";
4
+ import { Hono } from "hono";
5
+ import { createPaymentStore } from "./payment-store.js";
6
+
7
+ const wallet = process.env.MERCHANT_WALLET;
8
+ if (!wallet)
9
+ throw new Error("Set MERCHANT_WALLET to a disposable devnet address");
10
+ const port = Number(process.env.PORT ?? "3000");
11
+ const product = {
12
+ id: "premium",
13
+ name: "Premium API",
14
+ description: "Protected data",
15
+ resource: `http://localhost:${port}/premium`,
16
+ priceAtomic: "10000",
17
+ assetMint: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
18
+ payTo: wallet,
19
+ network: SOLANA_DEVNET,
20
+ } as const;
21
+ const store = await createPaymentStore();
22
+ const server = createMeterKitResourceServer({
23
+ product,
24
+ store,
25
+ rpcUrl: process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com",
26
+ });
27
+ const app = new Hono();
28
+ app.use(
29
+ paymentMiddleware(
30
+ {
31
+ "GET /premium": {
32
+ accepts: {
33
+ scheme: "exact",
34
+ price: { amount: product.priceAtomic, asset: product.assetMint },
35
+ network: SOLANA_DEVNET,
36
+ payTo: product.payTo,
37
+ maxTimeoutSeconds: 300,
38
+ },
39
+ description: product.description,
40
+ mimeType: "application/json",
41
+ },
42
+ },
43
+ server,
44
+ ),
45
+ );
46
+ app.get("/premium", (context) => context.json({ protected: true }));
47
+ serve({ fetch: app.fetch, port });
48
+ process.stdout.write(`MeterKit: http://localhost:${port}/premium\n`);
@@ -0,0 +1,5 @@
1
+ SOLANA_NETWORK=solana-devnet
2
+ SOLANA_RPC_URL=https://api.devnet.solana.com
3
+ MERCHANT_WALLET=replace_with_a_disposable_devnet_address
4
+ DURABILITY_MODE=memory
5
+ # DATABASE_URL=postgresql://user:password@localhost:5432/meterkit
@@ -0,0 +1,30 @@
1
+ # MeterKit paid MCP tool — experimental devnet surface
2
+
3
+ MCP currently uses stdio, not an HTTP URL. Its unpaid probe prints native x402
4
+ payment requirements and must not be reported as an HTTP 402 activation.
5
+
6
+ ```bash
7
+ {{INSTALL_COMMAND}}
8
+ {{CHECK_COMMAND}}
9
+ ```
10
+
11
+ Expected: JSON containing `accepts` with exact Solana devnet, test-USDC, amount
12
+ `10000` and your recipient. To run the stdio server afterward:
13
+
14
+ ```bash
15
+ {{DEV_COMMAND}}
16
+ ```
17
+
18
+ This experimental template keeps replay state only in memory. Do not use it for
19
+ paid deployment until a dedicated MCP verification contract and durable store
20
+ pass the full payment/replay lifecycle.
21
+
22
+ The HTTP-only `meterkit verify` and `meterkit pay` commands do not apply to this
23
+ stdio surface. Do not reinterpret `check:unpaid` as payment or settlement proof.
24
+
25
+ If it fails, inspect `.env`, rerun installation and treat upstream outages as
26
+ unknown. Reset by stopping the process and removing `node_modules`. Never commit
27
+ `.env`, signing material, payment headers or full proofs.
28
+ Run `{{DOCTOR_COMMAND}}` for non-mutating configuration and devnet diagnostics. For
29
+ unresolved failures, share only sanitized output at
30
+ https://github.com/JuanTorchia/meterkit/issues.
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "meterkit-paid-mcp",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "node --env-file=.env --import tsx src/server.ts",
7
+ "check:unpaid": "node --env-file=.env --import tsx src/server.ts --unpaid",
8
+ "doctor": "meterkit doctor --project ."
9
+ },
10
+ "dependencies": {
11
+ "@modelcontextprotocol/sdk": "1.30.0",
12
+ "@usemeterkit/database": "0.3.0",
13
+ "@usemeterkit/sdk": "0.3.0",
14
+ "@x402/core": "2.22.0",
15
+ "@x402/mcp": "2.22.0",
16
+ "@x402/svm": "2.22.0",
17
+ "zod": "^4.0.17"
18
+ },
19
+ "devDependencies": {
20
+ "@usemeterkit/cli": "0.3.0",
21
+ "tsx": "^4.23.12",
22
+ "typescript": "^5.9.3"
23
+ }
24
+ }
@@ -0,0 +1,20 @@
1
+ import { StandalonePostgresPaymentStore } from "@usemeterkit/database";
2
+ import { MemoryPaymentStore, type PaymentStore } from "@usemeterkit/sdk";
3
+
4
+ export async function createPaymentStore(): Promise<PaymentStore> {
5
+ const mode = process.env.DURABILITY_MODE ?? "memory";
6
+ if (mode === "memory") {
7
+ process.stderr.write(
8
+ "WARNING: memory payment store is non-durable; do not serve paid production traffic.\n",
9
+ );
10
+ return new MemoryPaymentStore();
11
+ }
12
+ if (mode !== "postgres")
13
+ throw new Error("DURABILITY_MODE must be memory or postgres");
14
+ const url = process.env.DATABASE_URL;
15
+ if (!url)
16
+ throw new Error("DATABASE_URL is required when DURABILITY_MODE=postgres");
17
+ const store = StandalonePostgresPaymentStore.connect(url);
18
+ await store.migrate();
19
+ return store;
20
+ }
@@ -0,0 +1,94 @@
1
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
+ import { SOLANA_DEVNET, SolanaSettlementValidator } from "@usemeterkit/sdk";
4
+ import { HTTPFacilitatorClient } from "@x402/core/server";
5
+ import { createPaymentWrapper, x402ResourceServer } from "@x402/mcp";
6
+ import { ExactSvmScheme } from "@x402/svm/exact/server";
7
+ import { z } from "zod";
8
+ import { createPaymentStore } from "./payment-store.js";
9
+
10
+ const wallet = process.env.MERCHANT_WALLET;
11
+ if (!wallet)
12
+ throw new Error("Set MERCHANT_WALLET to a disposable devnet address");
13
+ const USDC_DEVNET = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
14
+ const rpcUrl = process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com";
15
+ const settlementValidator = new SolanaSettlementValidator(rpcUrl);
16
+ // One process keeps one set of spent signatures. Persist this in a database
17
+ // before serving anything that matters: an in-memory set forgets every replay
18
+ // it has already rejected as soon as the process restarts.
19
+ const paymentStore = await createPaymentStore();
20
+
21
+ const resourceServer = new x402ResourceServer(
22
+ new HTTPFacilitatorClient({
23
+ url: process.env.X402_FACILITATOR_URL ?? "https://x402.org/facilitator",
24
+ }),
25
+ )
26
+ .register(SOLANA_DEVNET, new ExactSvmScheme({}))
27
+ .onAfterSettle(async ({ result, requirements }) => {
28
+ if (!result.success || !result.transaction || !result.payer)
29
+ throw new Error("PAYMENT_SETTLEMENT_INCOMPLETE");
30
+ if (await paymentStore.has(result.transaction))
31
+ throw new Error("PAYMENT_PROOF_ALREADY_USED");
32
+ await settlementValidator.validate({
33
+ signature: result.transaction,
34
+ payer: result.payer,
35
+ payTo: requirements.payTo,
36
+ mint: requirements.asset,
37
+ amountAtomic: requirements.amount,
38
+ });
39
+ await paymentStore.save({
40
+ id: crypto.randomUUID(),
41
+ productId: "premium_tool",
42
+ payer: result.payer,
43
+ payTo: requirements.payTo,
44
+ mint: requirements.asset,
45
+ amountAtomic: requirements.amount,
46
+ network: SOLANA_DEVNET,
47
+ signature: result.transaction,
48
+ settledAt: new Date().toISOString(),
49
+ status: "confirmed",
50
+ });
51
+ });
52
+ await resourceServer.initialize();
53
+
54
+ const accepts = await resourceServer.buildPaymentRequirements({
55
+ scheme: "exact",
56
+ network: SOLANA_DEVNET,
57
+ payTo: wallet,
58
+ price: { amount: "10000", asset: USDC_DEVNET },
59
+ maxTimeoutSeconds: 300,
60
+ });
61
+
62
+ if (process.argv.includes("--unpaid")) {
63
+ process.stdout.write(`${JSON.stringify({ accepts }, null, 2)}\n`);
64
+ process.exit(0);
65
+ }
66
+
67
+ const paid = createPaymentWrapper(resourceServer, {
68
+ accepts,
69
+ resource: {
70
+ url: "mcp://tool/premium_tool",
71
+ description: "A protected MCP tool result",
72
+ mimeType: "application/json",
73
+ serviceName: "MeterKit paid MCP tool",
74
+ tags: ["meterkit"],
75
+ },
76
+ });
77
+
78
+ const server = new McpServer({
79
+ name: "meterkit-paid-mcp",
80
+ version: "0.1.0",
81
+ });
82
+ server.registerTool(
83
+ "premium_tool",
84
+ {
85
+ title: "Premium tool",
86
+ description: "Returns a protected result for 0.01 devnet USDC.",
87
+ inputSchema: { query: z.string().describe("What to look up") },
88
+ },
89
+ paid(async ({ query }) => ({
90
+ content: [{ type: "text", text: `Protected result for: ${query}` }],
91
+ })),
92
+ );
93
+
94
+ await server.connect(new StdioServerTransport());
@@ -0,0 +1,5 @@
1
+ SOLANA_NETWORK=solana-devnet
2
+ SOLANA_RPC_URL=https://api.devnet.solana.com
3
+ MERCHANT_WALLET=replace_with_a_disposable_devnet_address
4
+ DURABILITY_MODE=memory
5
+ # DATABASE_URL=postgresql://user:password@localhost:5432/meterkit
@@ -0,0 +1,37 @@
1
+ # MeterKit Next route — devnet beta
2
+
3
+ The initializer writes the public provider configuration to `.env.local`.
4
+
5
+ ```bash
6
+ {{INSTALL_COMMAND}}
7
+ {{DEV_COMMAND}}
8
+ ```
9
+
10
+ In a second terminal:
11
+
12
+ ```bash
13
+ {{CHECK_COMMAND}}
14
+ ```
15
+
16
+ Expected: `HTTP 402` with x402 v2, Solana devnet, test-USDC, amount `10000`,
17
+ your recipient and `http://localhost:3000/api/premium`.
18
+
19
+ Run `{{VERIFY_COMMAND}}` to enforce the exact policy. Payment remains an explicit
20
+ state-changing step: place only a disposable funded devnet keypair (mode `0600`)
21
+ at `.local-wallets/payer.json`, then run `{{PAY_COMMAND}}`. The command caps the
22
+ request and session at `10000` atomic test-USDC and checks replay rejection.
23
+
24
+ This route uses `MemoryPaymentStore`; restart erases replay history. It is a
25
+ local discovery example, not a deployment-ready paid endpoint. Configure the
26
+ documented PostgreSQL mode and pass restart/replay validation before accepting
27
+ payments.
28
+
29
+ For recovery, rerun the install command, check `.env.local`, and confirm port
30
+ 3000 is available. External RPC/facilitator outages remain unavailable or
31
+ unknown. Reset by stopping Next and removing `.next`/`node_modules`; preserve
32
+ your source before removing the generated directory.
33
+ Run `{{DOCTOR_COMMAND}}` for the non-mutating diagnostic report. If remediation
34
+ still fails, share only sanitized output at
35
+ https://github.com/JuanTorchia/meterkit/issues.
36
+
37
+ Never commit `.env.local`, signing material, payment headers or full proofs.
@@ -0,0 +1,52 @@
1
+ import { createMeterKitResourceServer, SOLANA_DEVNET } from "@usemeterkit/sdk";
2
+ import { withX402 } from "@x402/next";
3
+ import { NextResponse, type NextRequest } from "next/server";
4
+ import { paymentStore } from "../../payment-store";
5
+
6
+ async function createPremiumRoute(wallet: string) {
7
+ const port = process.env.PORT ?? "3000";
8
+ const product = {
9
+ id: "premium",
10
+ name: "Premium API",
11
+ description: "Protected data",
12
+ resource: `http://localhost:${port}/api/premium`,
13
+ priceAtomic: "10000",
14
+ assetMint: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
15
+ payTo: wallet,
16
+ network: SOLANA_DEVNET,
17
+ } as const;
18
+ const server = createMeterKitResourceServer({
19
+ product,
20
+ store: await paymentStore,
21
+ rpcUrl: process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com",
22
+ });
23
+ const handler = async () => NextResponse.json({ protected: true });
24
+ return withX402(
25
+ handler,
26
+ {
27
+ accepts: {
28
+ scheme: "exact",
29
+ price: { amount: product.priceAtomic, asset: product.assetMint },
30
+ network: SOLANA_DEVNET,
31
+ payTo: product.payTo,
32
+ maxTimeoutSeconds: 300,
33
+ },
34
+ description: product.description,
35
+ mimeType: "application/json",
36
+ },
37
+ server,
38
+ );
39
+ }
40
+
41
+ let configuredRoute: Awaited<ReturnType<typeof createPremiumRoute>> | undefined;
42
+
43
+ export async function GET(request: NextRequest) {
44
+ const wallet = process.env.MERCHANT_WALLET;
45
+ if (!wallet)
46
+ return NextResponse.json(
47
+ { error: "server_not_configured", required: "MERCHANT_WALLET" },
48
+ { status: 503 },
49
+ );
50
+ configuredRoute ??= await createPremiumRoute(wallet);
51
+ return configuredRoute(request);
52
+ }
@@ -0,0 +1,22 @@
1
+ import { StandalonePostgresPaymentStore } from "@usemeterkit/database";
2
+ import { MemoryPaymentStore, type PaymentStore } from "@usemeterkit/sdk";
3
+
4
+ async function initialize(): Promise<PaymentStore> {
5
+ const mode = process.env.DURABILITY_MODE ?? "memory";
6
+ if (mode === "memory") {
7
+ process.stderr.write(
8
+ "WARNING: memory payment store is non-durable; do not serve paid production traffic.\n",
9
+ );
10
+ return new MemoryPaymentStore();
11
+ }
12
+ if (mode !== "postgres")
13
+ throw new Error("DURABILITY_MODE must be memory or postgres");
14
+ const url = process.env.DATABASE_URL;
15
+ if (!url)
16
+ throw new Error("DATABASE_URL is required when DURABILITY_MODE=postgres");
17
+ const store = StandalonePostgresPaymentStore.connect(url);
18
+ await store.migrate();
19
+ return store;
20
+ }
21
+
22
+ export const paymentStore = initialize();
@@ -0,0 +1,4 @@
1
+ /// <reference types="next" />
2
+ /// <reference types="next/image-types/global" />
3
+
4
+ // This file is generated-compatible and should not be edited manually.
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "meterkit-next-api",
3
+ "private": true,
4
+ "scripts": {
5
+ "dev": "next dev",
6
+ "build": "next build",
7
+ "check:unpaid": "meterkit check http://localhost:3000/api/premium --allow-localhost",
8
+ "doctor": "meterkit doctor --project . --url http://localhost:3000/api/premium --allow-localhost",
9
+ "verify": "meterkit verify http://localhost:3000/api/premium --network solana-devnet --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --allow-localhost",
10
+ "pay:test": "meterkit pay http://localhost:3000/api/premium --keypair .local-wallets/payer.json --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --max-session 10000 --rpc-url https://api.devnet.solana.com --replay --yes"
11
+ },
12
+ "dependencies": {
13
+ "@usemeterkit/database": "0.3.0",
14
+ "@usemeterkit/sdk": "0.3.0",
15
+ "@x402/next": "2.22.0",
16
+ "next": "16.3.1",
17
+ "react": "19.2.8",
18
+ "react-dom": "19.2.8"
19
+ },
20
+ "devDependencies": {
21
+ "@usemeterkit/cli": "0.3.0",
22
+ "@types/node": "^24.3.0",
23
+ "@types/react": "^19.1.10",
24
+ "@types/react-dom": "^19.1.7",
25
+ "typescript": "^5.9.3"
26
+ }
27
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["dom", "dom.iterable", "esnext"],
5
+ "allowJs": false,
6
+ "skipLibCheck": true,
7
+ "strict": true,
8
+ "noEmit": true,
9
+ "esModuleInterop": true,
10
+ "module": "esnext",
11
+ "moduleResolution": "bundler",
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "jsx": "react-jsx",
15
+ "incremental": true,
16
+ "plugins": [{ "name": "next" }]
17
+ },
18
+ "include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
19
+ "exclude": ["node_modules"]
20
+ }