Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
90 lines (85 loc) · 2.61 KB
/
Copy pathmiddleware.ts
File metadata and controls
90 lines (85 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { paymentMiddleware } from 'x402-next';
import { facilitator } from "@coinbase/x402";
// Get configuration from environment variables
const receivingAddress = process.env.NEXT_PUBLIC_RECEIVING_ADDRESS || '0x0000000000000000000000000000000000';
// Configure the payment middleware for protected endpoints
export const middleware = paymentMiddleware(
receivingAddress, // Wallet address to receive payments
{
// API endpoints for paywall content
'/api/paywall/content': {
price: '$0.10', // Price in USDC
network: "base", // Always use mainnet
config: {
description: 'Access paywall protected content',
inputSchema: {
type: "object",
properties: {
contentId: { type: "string", description: "ID of the paywall content" }
},
required: ["contentId"]
},
outputSchema: {
type: "object",
properties: {
content: { type: "object" },
accessToken: { type: "string" }
}
}
}
},
// API endpoint for file downloads
'/api/paywall/download': {
price: '$0.50', // Higher price for file downloads
network: "base", // Always use mainnet
config: {
description: 'Download paywall protected file',
inputSchema: {
type: "object",
properties: {
fileId: { type: "string", description: "ID of the file to download" }
},
required: ["fileId"]
},
outputSchema: {
type: "object",
properties: {
downloadUrl: { type: "string" },
expiresAt: { type: "string" }
}
}
}
},
// Payment profile endpoint
'/api/payment/send': {
price: '$1.00', // Dynamic pricing will be handled in the route
network: "base", // Always use mainnet
config: {
description: 'Send payment to x402.me profile',
inputSchema: {
type: "object",
properties: {
profileId: { type: "string", description: "Profile username" },
amount: { type: "number", description: "Payment amount in USD" }
},
required: ["profileId", "amount"]
},
outputSchema: {
type: "object",
properties: {
success: { type: "boolean" },
transactionId: { type: "string" }
}
}
}
}
},
facilitator // Always use mainnet CDP facilitator
);
// Configure which paths the middleware should run on
export const config = {
matcher: [
'/api/paywall/:path*',
'/api/payment/:path*',
]
};