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
9 changes: 6 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ REDIS_URL=your_vercel_redis_url
TRIGGER_SECRET_KEY=tr_dev_xxxxxxxxxx

# Coinbase
CDP_APP_KEY_ID=your_coinbase_developer_platform_app_key_id
CDP_SECRET=your_coinbase_developer_platform_secret
CDP_PAYMASTER_KEY=your_coinbase_paymaster_key
CDP_API_KEY_ID=your_coinbase_developer_platform_app_key_id
CDP_API_KEY_SECRET=your_coinbase_developer_platform_secret
CDP_PAYMASTER_KEY=your_coinbase_paymaster_key

# Environment
NODE_ENV=production
6 changes: 3 additions & 3 deletions lib/coinbase/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Coinbase } from "@coinbase/coinbase-sdk";

const apiKeyName = process.env.CDP_APP_KEY_ID;
const privateKey = process.env.CDP_SECRET;
const apiKeyName = process.env.CDP_API_KEY_ID;
const privateKey = process.env.CDP_API_KEY_SECRET;

if (!apiKeyName || !privateKey) {
throw new Error("CDP_APP_KEY_ID and CDP_SECRET must be set");
throw new Error("CDP_API_KEY_ID and CDP_API_KEY_SECRET must be set");
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const CHAT_POINT_SYSTEM_ID = 4172;
export const AGENT_RUN = "agent_run";
export const MESSAGE_SENT_POINT = 1;
export const MESSAGE_SENT_EVENT = "message_sent";
export const IS_PROD = process.env.NODE_ENV === "production";

export const ENTERPRISE_DOMAINS: ReadonlySet<string> = new Set([
"recoupable.com",
Expand Down
12 changes: 12 additions & 0 deletions lib/x402/getFacilitator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { facilitator as coinbaseHostedFacilitator } from "@coinbase/x402";
import type { FacilitatorConfig, Resource } from "x402/types";
import { IS_PROD } from "../consts";

const FACILITATOR_URL: Resource = "https://x402.org/facilitator";

export const getFacilitator = (): FacilitatorConfig =>
IS_PROD
? coinbaseHostedFacilitator
: {
url: FACILITATOR_URL,
};
25 changes: 14 additions & 11 deletions lib/x402/paymentMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import { paymentMiddleware } from "x402-express";
import type { RequestHandler } from "express";
import { IS_PROD } from "../consts";
import { getFacilitator } from "./getFacilitator";

const RECEIVING_WALLET_ADDRESS = "0x749B7b7A6944d72266Be9500FC8C221B6A7554Ce";
const FACILITATOR_URL = "https://x402.org/facilitator";

type RoutesConfig = Parameters<typeof paymentMiddleware>[1];

const price = IS_PROD ? "$0.01" : "$0.0001";
const network = IS_PROD ? "base" : "base-sepolia";
const facilitator = getFacilitator();

const routeConfig = {
"GET /api/image/generate": {
price: "$0.001",
network: "base-sepolia" as const,
price,
network,
config: {
description: "Generate images using AI",
inputSchema: {
type: "object" as const,
type: "object",
properties: {
location: { type: "string" as const, description: "City name" },
location: { type: "string", description: "City name" },
},
},
outputSchema: {
type: "object" as const,
type: "object",
properties: {
weather: { type: "string" as const },
temperature: { type: "number" as const },
weather: { type: "string" },
temperature: { type: "number" },
},
},
},
},
} as RoutesConfig;

export const createPaymentMiddleware = (): RequestHandler => {
return paymentMiddleware(RECEIVING_WALLET_ADDRESS, routeConfig, {
url: FACILITATOR_URL,
});
return paymentMiddleware(RECEIVING_WALLET_ADDRESS, routeConfig, facilitator);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"tsconfig-paths": "^4.2.0",
"uuid": "^11.0.5",
"viem": "^2.37.5",
"x402": "^0.7.3",
"x402-express": "^0.7.3",
"zod": "^4.1.12"
},
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

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