-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiddleware.ts
More file actions
81 lines (77 loc) · 2.64 KB
/
middleware.ts
File metadata and controls
81 lines (77 loc) · 2.64 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
import { facilitator } from "@coinbase/x402";
import { paymentMiddleware } from "x402-next";
import { IMAGE_GENERATE_PRICE, SMART_ACCOUNT_ADDRESS } from "./lib/const";
const inputSchema = {
queryParams: {
prompt: "Text prompt describing the image to generate",
files:
"Optional pipe-separated list of files. Format: url1:mediaType1|url2:mediaType2. Example: https://example.com/image.png:image/png|https://example.com/file.jpg:image/jpeg",
},
};
// Match the image generation endpoint schema
const imageGenerateOutputSchema = {
type: "object" as const,
description: "GenerateImageResult containing the generated image and metadata",
properties: {
image: {
type: "object" as const,
description: "Generated image file",
properties: {
base64: { type: "string", description: "Image as base64 encoded string" },
mediaType: { type: "string", description: "IANA media type of the image" },
},
},
usage: {
type: "object" as const,
description: "Token usage information for the image generation",
properties: {
inputTokens: { type: "number", description: "Number of tokens used for the prompt" },
outputTokens: { type: "number", description: "Number of tokens used for the output" },
totalTokens: { type: "number", description: "Number of tokens used for the total" },
reasoningTokens: { type: "number", description: "Number of tokens used for the reasoning" },
},
},
imageUrl: {
type: "string" as const,
description: "Fetchable URL for the uploaded image (nullable)",
},
arweaveResult: {
type: "object" as const,
description: "Arweave transaction result (nullable)",
},
moment: {
type: "object" as const,
description: "In Process moment creation result (nullable)",
},
arweaveError: {
type: "string" as const,
description: "Error message if Arweave upload failed (optional)",
},
},
};
export const middleware = paymentMiddleware(
SMART_ACCOUNT_ADDRESS,
{
"GET /api/x402/image/generate": {
price: `$${IMAGE_GENERATE_PRICE}`,
network: "base",
config: {
discoverable: true, // make endpoint discoverable
description: "Generate an image from a text prompt using AI",
outputSchema: imageGenerateOutputSchema,
inputSchema,
},
},
},
facilitator,
{
appName: "Recoup API",
appLogo: "/images/recoup_logo.png",
sessionTokenEndpoint: "/api/x402/session-token",
},
);
// Configure which paths the middleware should run on
export const config = {
matcher: ["/protected/:path*", "/api/:path*"],
runtime: "nodejs",
};