create-tollstile 0.1.1
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.
- package/LICENSE +21 -0
- package/dist/index.js +200 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Paradigm AI Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { basename, resolve } from "path";
|
|
5
|
+
import process from "process";
|
|
6
|
+
|
|
7
|
+
// src/scaffold.ts
|
|
8
|
+
import { mkdir, readdir, writeFile } from "fs/promises";
|
|
9
|
+
import { dirname, join } from "path";
|
|
10
|
+
|
|
11
|
+
// src/templates.ts
|
|
12
|
+
function honoTemplate(name2) {
|
|
13
|
+
return [
|
|
14
|
+
{
|
|
15
|
+
path: "package.json",
|
|
16
|
+
contents: `${JSON.stringify(
|
|
17
|
+
{
|
|
18
|
+
name: name2,
|
|
19
|
+
private: true,
|
|
20
|
+
type: "module",
|
|
21
|
+
scripts: {
|
|
22
|
+
dev: "tsx watch src/server.ts",
|
|
23
|
+
agent: "tsx src/agent.ts"
|
|
24
|
+
},
|
|
25
|
+
dependencies: {
|
|
26
|
+
"@hono/node-server": "^1.13.0",
|
|
27
|
+
"@tollstile/hono": "^0.1.0",
|
|
28
|
+
hono: "^4.6.0",
|
|
29
|
+
tollstile: "^0.1.0"
|
|
30
|
+
},
|
|
31
|
+
devDependencies: {
|
|
32
|
+
"@types/node": "^22.0.0",
|
|
33
|
+
tsx: "^4.19.0",
|
|
34
|
+
typescript: "^5.9.0"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
null,
|
|
38
|
+
2
|
|
39
|
+
)}
|
|
40
|
+
`
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
path: "tsconfig.json",
|
|
44
|
+
contents: `${JSON.stringify(
|
|
45
|
+
{
|
|
46
|
+
compilerOptions: {
|
|
47
|
+
target: "ES2022",
|
|
48
|
+
module: "NodeNext",
|
|
49
|
+
moduleResolution: "NodeNext",
|
|
50
|
+
strict: true,
|
|
51
|
+
skipLibCheck: true,
|
|
52
|
+
noEmit: true
|
|
53
|
+
},
|
|
54
|
+
include: ["src"]
|
|
55
|
+
},
|
|
56
|
+
null,
|
|
57
|
+
2
|
|
58
|
+
)}
|
|
59
|
+
`
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
path: "src/toll.ts",
|
|
63
|
+
contents: `import { createTollstile, memoryLedger, testRail } from "tollstile";
|
|
64
|
+
|
|
65
|
+
// The test rail runs the whole payment lifecycle locally: no wallet, network, or account.
|
|
66
|
+
// Swap in real rails (x402, MPP, \u2026) and a database ledger when you deploy.
|
|
67
|
+
export const toll = createTollstile({
|
|
68
|
+
rails: [testRail()],
|
|
69
|
+
ledger: memoryLedger(),
|
|
70
|
+
});
|
|
71
|
+
`
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
path: "src/server.ts",
|
|
75
|
+
contents: `import { serve } from "@hono/node-server";
|
|
76
|
+
import { tollstile } from "@tollstile/hono";
|
|
77
|
+
import { Hono } from "hono";
|
|
78
|
+
import { toll } from "./toll.js";
|
|
79
|
+
|
|
80
|
+
const app = new Hono();
|
|
81
|
+
|
|
82
|
+
app.get("/", (c) => c.text("GET /weather costs $0.01. Run \`npm run agent\` to pay for it."));
|
|
83
|
+
|
|
84
|
+
app.get("/weather", tollstile(toll.price("$0.01")), (c) => {
|
|
85
|
+
return c.json({ forecast: "clear", paidWith: c.get("payment").via });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
serve({ fetch: app.fetch, port: 3000 }, ({ port }) => {
|
|
89
|
+
console.log(\`Paid API listening on http://localhost:\${port}/weather\`);
|
|
90
|
+
console.log("In another terminal, run: npm run agent");
|
|
91
|
+
});
|
|
92
|
+
`
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
path: "src/agent.ts",
|
|
96
|
+
contents: `// A tiny agent that meets a 402, pays with the test rail, and gets through.
|
|
97
|
+
const url = process.argv[2] ?? "http://localhost:3000/weather";
|
|
98
|
+
|
|
99
|
+
type Challenge = { price: string; accepts: { rail: string }[] };
|
|
100
|
+
|
|
101
|
+
console.log(\`\u2192 GET \${url}\`);
|
|
102
|
+
const unpaid = await fetch(url);
|
|
103
|
+
console.log(\`\u2190 \${unpaid.status} \${unpaid.statusText}\`);
|
|
104
|
+
|
|
105
|
+
if (unpaid.status !== 402) {
|
|
106
|
+
console.log("Expected 402 Payment Required. Is the server running?");
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const challenge = (await unpaid.json()) as Challenge;
|
|
111
|
+
console.log(\` price \${challenge.price} \xB7 rails \${challenge.accepts.map((offer) => offer.rail).join(", ")}\`);
|
|
112
|
+
|
|
113
|
+
// An Idempotency-Key makes the paid request safe to retry: a retry is never paid or run twice.
|
|
114
|
+
console.log(\`\u2192 GET \${url} (Payment: test, Idempotency-Key)\`);
|
|
115
|
+
const paid = await fetch(url, { headers: { payment: "test", "idempotency-key": crypto.randomUUID() } });
|
|
116
|
+
console.log(\`\u2190 \${paid.status} \${paid.statusText}\`);
|
|
117
|
+
console.log(\` receipt \${paid.headers.get("payment-receipt") ?? "none"}\`);
|
|
118
|
+
console.log(\` \${await paid.text()}\`);
|
|
119
|
+
`
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
path: "README.md",
|
|
123
|
+
contents: `# ${name2}
|
|
124
|
+
|
|
125
|
+
A paid API built with [Tollstile](https://tollstile.com).
|
|
126
|
+
|
|
127
|
+
\`\`\`bash
|
|
128
|
+
npm install
|
|
129
|
+
npm run dev # starts the API on :3000
|
|
130
|
+
npm run agent # in another terminal: 402, pay, 200
|
|
131
|
+
\`\`\`
|
|
132
|
+
|
|
133
|
+
- \`src/toll.ts\` \u2014 rails and ledger.
|
|
134
|
+
- \`src/server.ts\` \u2014 \`GET /weather\` costs $0.01.
|
|
135
|
+
- \`src/agent.ts\` \u2014 a test agent that pays with the test rail.
|
|
136
|
+
|
|
137
|
+
Next: read the [quickstart](https://tollstile.com/docs/quickstart) and replace the test rail with a real one.
|
|
138
|
+
`
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
path: ".gitignore",
|
|
142
|
+
contents: "node_modules\n.env\n"
|
|
143
|
+
}
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/scaffold.ts
|
|
148
|
+
var PACKAGE_NAME = /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
|
|
149
|
+
async function scaffold(directory2, name2) {
|
|
150
|
+
if (!PACKAGE_NAME.test(name2)) {
|
|
151
|
+
return { status: "invalid_name", reason: `"${name2}" is not a valid npm package name. Use lowercase letters, digits, and dashes.` };
|
|
152
|
+
}
|
|
153
|
+
if ((await listDirectory(directory2)).length > 0) return { status: "not_empty" };
|
|
154
|
+
const files = honoTemplate(name2);
|
|
155
|
+
for (const file of files) {
|
|
156
|
+
const target2 = join(directory2, file.path);
|
|
157
|
+
await mkdir(dirname(target2), { recursive: true });
|
|
158
|
+
await writeFile(target2, file.contents, { flag: "wx" });
|
|
159
|
+
}
|
|
160
|
+
return { status: "created", files: files.map((file) => file.path) };
|
|
161
|
+
}
|
|
162
|
+
async function listDirectory(directory2) {
|
|
163
|
+
await mkdir(directory2, { recursive: true });
|
|
164
|
+
return readdir(directory2);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/index.ts
|
|
168
|
+
var target = process.argv[2] ?? "my-paid-api";
|
|
169
|
+
var directory = resolve(process.cwd(), target);
|
|
170
|
+
var name = basename(directory);
|
|
171
|
+
var result = await scaffold(directory, name);
|
|
172
|
+
switch (result.status) {
|
|
173
|
+
case "invalid_name":
|
|
174
|
+
console.error(result.reason);
|
|
175
|
+
process.exitCode = 1;
|
|
176
|
+
break;
|
|
177
|
+
case "not_empty":
|
|
178
|
+
console.error(`${target} already exists and is not empty. Choose another directory name.`);
|
|
179
|
+
process.exitCode = 1;
|
|
180
|
+
break;
|
|
181
|
+
case "created": {
|
|
182
|
+
const runner = packageManager();
|
|
183
|
+
console.log(`
|
|
184
|
+
Created ${name} with a paid route and a test agent.
|
|
185
|
+
`);
|
|
186
|
+
console.log(` cd ${target}`);
|
|
187
|
+
console.log(` ${runner} install`);
|
|
188
|
+
console.log(` ${runner} run dev`);
|
|
189
|
+
console.log(` ${runner} run agent # in another terminal
|
|
190
|
+
`);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function packageManager() {
|
|
195
|
+
const agent = process.env.npm_config_user_agent ?? "";
|
|
196
|
+
if (agent.startsWith("pnpm")) return "pnpm";
|
|
197
|
+
if (agent.startsWith("yarn")) return "yarn";
|
|
198
|
+
if (agent.startsWith("bun")) return "bun";
|
|
199
|
+
return "npm";
|
|
200
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-tollstile",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Create a paid API in one command: a Hono server with pay-per-call pricing, a test payment rail, and an agent that pays for it.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"create",
|
|
7
|
+
"scaffold",
|
|
8
|
+
"starter",
|
|
9
|
+
"paid-api",
|
|
10
|
+
"x402",
|
|
11
|
+
"agent-payments",
|
|
12
|
+
"api-monetization",
|
|
13
|
+
"pay-per-call",
|
|
14
|
+
"mcp"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://tollstile.com",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/tollstile/tollstile.git",
|
|
20
|
+
"directory": "packages/create-tollstile"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/tollstile/tollstile/issues"
|
|
24
|
+
},
|
|
25
|
+
"author": "Paradigm AI Inc.",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"bin": {
|
|
29
|
+
"create-tollstile": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
37
|
+
}
|
|
38
|
+
}
|